@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { return getParameterNames(ctor.getParameters()); }
@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { return getParameterNames(ctor.getParameters()); }
@Override public String[] getParameterNames(Constructor<?> ctor) { Parameter[] parameters = ctor.getParameters(); String[] parameterNames = new String[parameters.length]; for (int i = 0; i < parameters.length; i++) { Parameter param = parameters[i]; if (!param.isNamePresent()) { return null; } parameterNames[i] = param.getName(); } return parameterNames; }
public Object instantiateModule(Class<?> moduleClass) { try { Constructor<?>[] constructors = moduleClass.getConstructors(); if (constructors.length == 0) { return moduleClass.newInstance(); } else { // instantiate the module passing null or a test field to constructor Parameter[] parameters = constructors[0].getParameters(); Object[] args = new Object[parameters.length]; for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; Class<?> type = parameter.getType(); args[i] = getValueOfClass(type); } return constructors[0].newInstance(args); } } catch (Exception e) { throw new RuntimeException("Error instantiating module " + moduleClass, e); } } }
static <T> Optional<FactoryAccessor<T>> findConstructorWithParameters(Class<T> type) { AtomicReference<Constructor<?>> foundConstructor = new AtomicReference<>(); for (Constructor<?> constructor : type.getConstructors()) { if (checkConstructor(constructor, true)) { if (foundConstructor.get() != null) { LOGGER.log(Level.WARNING, () -> "Class " + type.getName() + " contains more than one constructor with parameters." + " Any will be used to initialize the type."); return Optional.empty(); } foundConstructor.set(constructor); } } if (foundConstructor.get() == null) { return Optional.empty(); } else { return findConstructor(type, foundConstructor.get().getParameterTypes()) .map(handle -> new FactoryAccessor<>(type, handle, foundConstructor.get().getParameters())); } }
@Override public Set<Attribute<T,?>> describe() { final Set<Attribute<T,?>> attributes = super.describe(); final Constructor constructor = getDataBoundConstructor(target); if (constructor != null) { final Parameter[] parameters = constructor.getParameters(); final String[] names = ClassDescriptor.loadParameterNames(constructor); for (int i = 0; i < parameters.length; i++) { final Parameter p = parameters[i]; final Attribute a = createAttribute(names[i], TypePair.of(p)); if (a == null) continue; attributes.add(a); } } return attributes; }
for (final Parameter parameter : creator.getParameters()) { final JsonProperty anno = parameter.getAnnotation(JsonProperty.class); if (null != anno && propName.equals(anno.value())) {
private T tryConstructor(Constructor<T> constructor, Mapping config, ConfigurationContext context) throws ConfiguratorException { final Parameter[] parameters = constructor.getParameters(); final String[] names = ClassDescriptor.loadParameterNames(constructor); Object[] args = new Object[names.length];
final Parameter[] parameters = constructor.getParameters(); final String[] names = ClassDescriptor.loadParameterNames(constructor); final Attribute[] attributes = new Attribute[parameters.length];
@Test public void getQualifiers() throws Exception { assertThat(foo(1)) .isEqualTo(foo(1)) .isNotEqualTo(foo(2)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredField("qualifiedField"))) .containsExactly(foo(1)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredMethod("qualifiedMethod"))) .containsExactly(foo(2)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredMethod("qualifiedParameter", String.class).getParameters()[0])) .containsExactly(foo(3)); assertThat(Qualifiers.getQualifiers(QualifiedClass.class)) .containsExactly(foo(4)); assertThat(Qualifiers.getQualifiers(QualifiedClass.class.getDeclaredConstructor(String.class).getParameters()[0])) .containsExactly(foo(5)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredField("twoQualifiers"))) .containsExactlyInAnyOrder(foo(6), bar("six")); }
@Test public void passAnnotations() throws Exception { check(ImmutableValForPass.class.getAnnotation(A1.class)).notNull(); check(ImmutableValForPass.class.getAnnotation(A2.class)).notNull(); check(ImmutableValForPass.class.getAnnotation(B1.class)).isNull(); Parameter parameter = ImmutableValForPass.class.getConstructor(int.class).getParameters()[0]; check(parameter.getAnnotation(A1.class)).notNull(); check(parameter.getAnnotation(A2.class)).notNull(); }
/** * Uses the given constructor to generate values of the declaring type. * * @param ctor the constructor to reflect on to generate constructor * parameter values */ public Ctor(Constructor<T> ctor) { super(ctor.getDeclaringClass()); this.ctor = ctor; this.parameters = ctor.getParameters(); }
public DefaultResourceConstructor(final ResourceClass resourceClass, final Constructor constructor) { this.resourceClass = resourceClass; this.constructor = constructor; if (constructor.getParameterTypes() != null) { this.params = new ConstructorParameter[constructor.getParameterTypes().length]; Parameter[] reflectionParameters = constructor.getParameters(); for (int i = 0; i < constructor.getParameterTypes().length; i++) { this.params[i] = new ConstructorParameter(this, reflectionParameters[i].getName(), constructor.getParameterTypes()[i], constructor.getGenericParameterTypes()[i], constructor.getParameterAnnotations()[i]); } } }
public ConstructorInjectorImpl(final Constructor constructor, final ResteasyProviderFactory factory) { this.constructor = constructor; params = new ValueInjector[constructor.getParameterTypes().length]; Parameter[] reflectionParameters = constructor.getParameters(); for (int i = 0; i < constructor.getParameterTypes().length; i++) { Class type = constructor.getParameterTypes()[i]; Type genericType = constructor.getGenericParameterTypes()[i]; Annotation[] annotations = constructor.getParameterAnnotations()[i]; String name = reflectionParameters[i].getName(); params[i] = factory.getInjectorFactory().createParameterExtractor(constructor.getDeclaringClass(), constructor, name, type, genericType, annotations, factory); } }
private List<FieldSpec> buildFieldSpecs( final Constructor<?> constructor, final FakeValueProvider valueProvider ) { final Parameter[] parameters = constructor.getParameters();
private <T extends PageObject> boolean hasOuterClassConstructor(Class<T> pageObjectClass) { return Arrays.stream(pageObjectClass.getConstructors()) .anyMatch( constructor -> constructor.getParameters().length == 1 && constructor.getParameters()[0].getType() == pageObjectClass.getEnclosingClass() ); }
@Override public List<String> getParameterNames(Constructor<?> constructor) { return getParameterNames( constructor.getParameters() ); }
public static <T> String[] getParametersName(Constructor<T> ctor) { List<String> parametersNames = new ArrayList<String>(); for (Parameter parameter : ctor.getParameters()) { if (!parameter.isNamePresent()) { throw new UnsupportedOperationException("Enable the Reflection API to retrieve formal parameter names by compiling the source of your POJO with the -parameters option of the javac compiler"); } parametersNames.add(parameter.getName()); } return parametersNames.toArray(new String[0]); }
public Set<Class> findConsumedTypes(Class clazz) { Constructor constructor = clazz.getConstructors()[0]; Set<Class> consumedType = new HashSet<>(); for (int i = 0; i < constructor.getParameterTypes().length; i++) { Class<?> paramClass = constructor.getParameterTypes()[i]; if (constructor.getParameters()[i].isAnnotationPresent(Config.class)) { continue; } consumedType.add(unwrapParameterType(constructor, paramClass, i)); } return consumedType; }
public static <T> T newInstance(Class<T> clazz, Object parameter) { if (parameter != null) { for (Constructor<T> constructor : (Constructor<T>[]) clazz.getConstructors()) { if (constructor.getParameterCount() == 1 && constructor.getParameters()[0].getType().isAssignableFrom(parameter.getClass())) { return constructNewInstance(constructor, parameter); } } } return null; } }