List of usage examples for java.lang.reflect Constructor getGenericParameterTypes
@Override
public Type[] getGenericParameterTypes()
From source file:io.neba.core.resourcemodels.factory.ModelInstantiator.java
/** * @return either the default or an @Inject constructor, if present. Fails if neither a public default constructor nor a public @Inject constructor is * present, of if multiple @Inject constructors exist. */// w w w . ja va 2 s. c om @SuppressWarnings("unchecked") @Nonnull private static <T> ModelConstructor<T> resolveConstructor(@Nonnull Class<T> modelType) { ModelConstructor<T> constructor; Constructor<T> injectionConstructor = null, defaultConstructor = null; for (Constructor c : modelType.getConstructors()) { if (c.getParameterCount() == 0) { defaultConstructor = c; } if (!annotations(c).containsName(INJECT_ANNOTATION_NAME)) { continue; } if (injectionConstructor != null) { throw new InvalidModelException("Unable to instantiate model " + modelType + ". " + "Found more than one constructor annotated with @Inject: " + injectionConstructor + ", " + c); } injectionConstructor = c; } if (injectionConstructor != null) { Type[] parameters = injectionConstructor.getGenericParameterTypes(); ServiceDependency[] serviceDependencies = new ServiceDependency[parameters.length]; Annotation[][] parameterAnnotations = injectionConstructor.getParameterAnnotations(); for (int i = 0; i < parameters.length; ++i) { Filter filter = findFilterAnnotation(parameterAnnotations[i]); serviceDependencies[i] = new ServiceDependency(parameters[i], modelType, filter); } constructor = new ModelConstructor<>(injectionConstructor, serviceDependencies); } else if (defaultConstructor != null) { constructor = new ModelConstructor<>(defaultConstructor); } else { throw new InvalidModelException("The model " + modelType + " has neither a public default constructor nor a public constructor annotated with @Inject."); } return constructor; }
From source file:org.getspout.spoutapi.packet.PacketAddonData.java
@SuppressWarnings("unchecked") @Override//from ww w . j a v a 2 s . c o m public void readData(SpoutInputStream input) throws IOException { String packetName = input.readString(); try { Class<? extends AddonPacket> packetClass = AddonPacket.getPacketFromId(packetName); Constructor<? extends AddonPacket> constructor = null; Constructor<? extends AddonPacket>[] constructors = (Constructor<? extends AddonPacket>[]) packetClass .getConstructors(); for (Constructor<? extends AddonPacket> c : constructors) { if (c.getGenericParameterTypes().length == 0) { constructor = c; break; } } packet = constructor.newInstance(); } catch (Exception e) { e.printStackTrace(); } int size = input.readInt(); compressed = input.readBoolean(); data = new byte[size]; input.read(data); }
From source file:com.adaptris.util.TestURLString.java
Constructor noArg(Constructor[] ctors) { Constructor result = null;//from w w w.j a v a 2 s. c o m for (Constructor ctor : ctors) { if (ctor.getGenericParameterTypes().length == 0) { result = ctor; break; } } return result; }
From source file:org.getspout.spout.packet.PacketAddonData.java
@SuppressWarnings("unchecked") public void readData(DataInputStream input) throws IOException { String id = PacketUtil.readString(input); boolean sandboxed = SpoutClient.isSandboxed(); SpoutClient.enableSandbox();/* ww w .j av a2 s.c o m*/ try { Class<? extends AddonPacket> packetClass = AddonPacket.getPacketFromId(id); Constructor<? extends AddonPacket> constructor = null; Constructor<? extends AddonPacket>[] constructors = (Constructor<? extends AddonPacket>[]) packetClass .getConstructors(); for (Constructor<? extends AddonPacket> c : constructors) { if (c.getGenericParameterTypes().length == 0) { constructor = c; break; } } packet = constructor.newInstance(); } catch (Exception e) { e.printStackTrace(); } if (!sandboxed) { SpoutClient.disableSandbox(); } int size = input.readInt(); compressed = input.readBoolean(); data = new byte[size]; input.readFully(data); }
From source file:com.cloudera.livy.client.common.TestHttpMessages.java
/** * Tests that all defined messages can be serialized and deserialized using Jackson. *//* w w w .j a va2 s. c o m*/ @Test public void testMessageSerialization() throws Exception { ObjectMapper mapper = new ObjectMapper(); for (Class<?> msg : HttpMessages.class.getClasses()) { if (msg.isInterface()) { continue; } String name = msg.getSimpleName(); Constructor c = msg.getConstructors()[0]; Object[] params = new Object[c.getParameterTypes().length]; Type[] genericTypes = c.getGenericParameterTypes(); for (int i = 0; i < params.length; i++) { params[i] = dummyValue(c.getParameterTypes()[i], genericTypes[i]); } Object o1 = c.newInstance(params); byte[] serialized = mapper.writeValueAsBytes(o1); Object o2 = mapper.readValue(serialized, msg); assertNotNull("could not deserialize " + name, o2); for (Field f : msg.getFields()) { checkEquals(name, f, o1, o2); } } }
From source file:com.vertica.hadoop.VerticaOutputFormat.java
/** * Given a class, initializes it and returns it. If the class is a subclass of * @{link AbstractVerticaOutputCommitter}, then the constructor that takes a * @{link VerticaOutputFormat} object will be used. Alternatively, the class can implement * @{link OutputCommitter} directly./*from w ww . j a va 2 s . c om*/ * @param outputCommitterClass * @param configuration * @return * @throws IOException */ @SuppressWarnings("unchecked") private OutputCommitter initializeOutputCommitter(Class outputCommitterClass, Configuration configuration) throws IOException { // If class extends AbstractVerticaOutputCommitter, create one of those // else it needs to be an instance of OutputCommitter // else throw exception if (AbstractVerticaOutputCommitter.class.isAssignableFrom(outputCommitterClass)) { // iterate through the constructors to find the one we're looking for for (Constructor constructor : outputCommitterClass.getDeclaredConstructors()) { if (constructor.getGenericParameterTypes().length == 1 && constructor.getGenericParameterTypes()[0].equals(VerticaOutputFormat.class)) { try { return (OutputCommitter) constructor.newInstance(this); } catch (Exception e) { throw new IOException("Could not initialize OutputCommitter class " + outputCommitterClass.getCanonicalName(), e); } } } throw new IOException("Implementation of AbstractVerticaOutputCommitter must " + "have a constructor that takes a VerticaOutputFormat object"); } else if (OutputCommitter.class.isAssignableFrom(outputCommitterClass)) { return (OutputCommitter) ReflectionUtils.newInstance(outputCommitterClass, configuration); } throw new IOException(String.format("Configured output committer class %s must implement %s", outputCommitterClass.getCanonicalName(), OutputCommitter.class.getCanonicalName())); }
From source file:com.basistech.rosette.apimodel.ModelTest.java
private Object createObject(Class clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException { if (Enum.class.isAssignableFrom(clazz)) { // pick a value, any value. return clazz.getEnumConstants()[0]; } else {/* ww w . j av a2 s.co m*/ Constructor<?>[] ctors = clazz.newInstance().getClass().getDeclaredConstructors(); Object o = null; for (Constructor ctor : ctors) { if (ctor.getGenericParameterTypes().length == 1) { Object objectOfType = createObjectOfType(ctor.getGenericParameterTypes()[0]); if (objectOfType != null) { o = ctor.newInstance(objectOfType); break; } } } return o; } }
From source file:com.vertica.hivestoragehandler.VerticaOutputFormat.java
/** * Given a class, initializes it and returns it. If the class is a subclass of * @{link AbstractVerticaOutputCommitter}, then the constructor that takes a * @{link VerticaOutputFormat} object will be used. Alternatively, the class can implement * @{link OutputCommitter} directly./*w ww . j ava 2 s. c o m*/ * @param outputCommitterClass * @param configuration * @return * @throws IOException */ @SuppressWarnings("unchecked") private OutputCommitter initializeOutputCommitter(Class outputCommitterClass, Configuration configuration) throws IOException { // If class extends AbstractVerticaOutputCommitter, create one of those // else it needs to be an instance of OutputCommitter // else throw exception if (VerticaTaskOutputCommitter.class.isAssignableFrom(outputCommitterClass)) { // iterate through the constructors to find the one we're looking for for (Constructor constructor : outputCommitterClass.getDeclaredConstructors()) { if (constructor.getGenericParameterTypes().length == 1 && constructor.getGenericParameterTypes()[0].equals(VerticaOutputFormat.class)) { try { return (OutputCommitter) constructor.newInstance(this); } catch (Exception e) { throw new IOException("Could not initialize OutputCommitter class " + outputCommitterClass.getCanonicalName(), e); } } } throw new IOException("Implementation of AbstractVerticaOutputCommitter must " + "have a constructor that takes a VerticaOutputFormat object"); } else if (OutputCommitter.class.isAssignableFrom(outputCommitterClass)) { return (OutputCommitter) ReflectionUtils.newInstance(outputCommitterClass, configuration); } throw new IOException(String.format("Configured output committer class %s must implement %s", outputCommitterClass.getCanonicalName(), OutputCommitter.class.getCanonicalName())); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * @return generic signature for given {@link Constructor}. This signature is not same signature * as in JVM or JDT, just some string that unique identifies constructor in its * {@link Class}. It uses {@link Constructor#getGenericParameterTypes()}, so for each * {@link TypeVariable} its name will be used in signature. *//* w w w . j a v a 2 s . com*/ public static String getConstructorGenericSignature(Constructor<?> constructor) { Assert.isNotNull(constructor); return getConstructorSignature(constructor.getGenericParameterTypes()); }
From source file:org.powertac.logtool.common.DomainObjectReader.java
private Object constructInstance(Class<?> clazz, String[] args) throws MissingDomainObject { Constructor<?>[] potentials = clazz.getDeclaredConstructors(); Constructor<?> target = null; Object[] params = null;/*from w ww . j av a 2 s . c o m*/ for (Constructor<?> cons : potentials) { Type[] types = cons.getGenericParameterTypes(); if (types.length != args.length) // not this one continue; // correct length of parameter list - // now try to resolve the types. // If we get a MissingDomainObject exception, keep going. try { params = resolveArgs(types, args); } catch (MissingDomainObject mdo) { // ignore } if (null == params) // no match continue; else { target = cons; break; } } // if we found one, use it, then update the id value if (null != target) { Object result = null; try { target.setAccessible(true); result = target.newInstance(params); } catch (InvocationTargetException ite) { // arg-constructor mismatch return restoreInstance(clazz, args); } catch (Exception e) { log.error("could not construct instance of " + clazz.getName() + ": " + e.toString()); return null; } return result; } else { // otherwise, try to use the readResolve method return restoreInstance(clazz, args); } }