Java Code Examples for java.lang.reflect.Constructor#getParameters()

The following examples show how to use java.lang.reflect.Constructor#getParameters() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Returns a value indicating whether the operator can transform the given method.
 */
@Override
public boolean canMutate(Method method) {
    try {
        Class<?> returnClass = getAppropriateReturnClass(method);

        if(returnClass.equals(String.class)
                || !belongsToJavaPackages(returnClass)
                || Modifier.isAbstract(returnClass.getModifiers())) {
            return false;
        }
        for (Constructor<?> publicConstructor : returnClass.getConstructors()) {
            if (publicConstructor.getParameters().length == 0) {
                return true;
            }
        }
        return false;
    }
    catch (ClassNotFoundException e) {
        return false;
    }
}
 
Example 2
@NonNull
@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;
}
 
Example 3
/** Special case testing for a compiler-generated constructor parameter. */
public void testImplicitConstructorParameters_staticInnerClass() throws Exception {
    Constructor<StaticInnerClass> constructor =
            StaticInnerClass.class.getDeclaredConstructor(String.class);
    Parameter[] parameters = constructor.getParameters();
    assertEquals(1, parameters.length);

    Parameter parameter0 = parameters[0];
    AnnotatedElementTestSupport.assertGetAnnotationsByType(
            parameter0, AnnotationA.class, new String[] {"@AnnotationA"});
    AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
            parameter0, AnnotationA.class, new String[] {"@AnnotationA"});
    AnnotatedElementTestSupport.assertGetDeclaredAnnotation(
            parameter0, AnnotationA.class, "@AnnotationA");
    AnnotatedElementTestSupport.assertIsAnnotationPresent(
            parameter0, AnnotationA.class, true);
}
 
Example 4
Source Project: flow   File: ComponentEventBusUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Scans the event type and forms a map of event data expression (for
 * {@link com.vaadin.flow.dom.DomListenerRegistration#addEventData(String)}
 * ) to Java type, with the same order as the parameters for the event
 * constructor (as returned by {@link #getEventConstructor(Class)}).
 *
 * @return a map of event data expressions, in the order defined by the
 *         component event constructor parameters
 */
private static LinkedHashMap<String, Class<?>> findEventDataExpressions(
        Constructor<? extends ComponentEvent<?>> eventConstructor) {
    LinkedHashMap<String, Class<?>> eventDataExpressions = new LinkedHashMap<>();
    // Parameter 0 is always "Component source"
    // Parameter 1 is always "boolean fromClient"
    for (int i = 2; i < eventConstructor.getParameterCount(); i++) {
        Parameter p = eventConstructor.getParameters()[i];
        EventData eventData = p.getAnnotation(EventData.class);
        if (eventData == null || eventData.value().isEmpty()) {
            // The parameter foo of the constructor Bar(Foo foo) has no
            // @DomEvent, or its value is empty."
            throw new IllegalArgumentException(String.format(
                    "The parameter %s of the constructor %s has no @%s, or the annotation value is empty",
                    p.getName(), eventConstructor.toString(),
                    EventData.class.getSimpleName()));
        }
        eventDataExpressions.put(eventData.value(),p.getType());
    }
    return eventDataExpressions;
}
 
Example 5
@Test
public void nonAnnotatedParametersInTopLevelClassConstructorAreNotCandidatesForAutowiring() throws Exception {
	Constructor<?> notAutowirableConstructor = AutowirableClass.class.getConstructor(String.class);

	Parameter[] parameters = notAutowirableConstructor.getParameters();
	for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
		Parameter parameter = parameters[parameterIndex];
		assertFalse("Parameter " + parameter + " must not be autowirable",
				ParameterResolutionDelegate.isAutowirable(parameter, parameterIndex));
	}
}
 
Example 6
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 7
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 8
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 9
Source Project: oxygen   File: IocPlugin.java    License: Apache License 2.0 5 votes vote down vote up
private Object dependencyInstance(Class<?> clazz, Constructor<?> constructor) {
  Parameter[] params = constructor.getParameters();
  Object[] args = new Object[params.length];
  for (int i = 0; i < params.length; i++) {
    if (params[i].isAnnotationPresent(Named.class)) {
      args[i] = Singleton.get(params[i].getAnnotation(Named.class).value());
    } else {
      args[i] = Singleton.get(params[i].getType());
    }
    if (args[i] == null) {
      return null;
    }
  }
  return beanProxy.proxy(clazz, args);
}
 
Example 10
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 11
Source Project: elepy   File: Injector.java    License: Apache License 2.0 5 votes vote down vote up
private <T> T initializeObjectViaConstructor(Class<? extends T> cls) throws IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<? extends T> constructor = ReflectionUtils.getElepyConstructor(cls)
            .orElseThrow(() -> new ElepyConfigException(String.format("Can't initialize %s. It has no empty constructor or a constructor with just one ElepyContext.", cls.getName())));

    Parameter[] parameters = constructor.getParameters();
    Object[] dependencies = new Object[parameters.length];

    for (int i = 0; i < parameters.length; i++) {
        dependencies[i] = getDependencyForAnnotatedElement(parameters[i]);
    }

    return constructor.newInstance(dependencies);
}
 
Example 12
Source Project: j2objc   File: ExecutableParameterTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Special case testing for a compiler-generated constructor parameter. */
public void testImplicitConstructorParameters_staticInnerClass() throws Exception {
    Constructor<StaticInnerClass> constructor =
            StaticInnerClass.class.getDeclaredConstructor(String.class);
    Parameter[] parameters = constructor.getParameters();
    assertEquals(1, parameters.length);

    Annotation[][] annotations = getParameterAnnotations(constructor, 1);
    assertAnnotationsMatch(annotations[0], new String[] { "@AnnotationA" });
}
 
Example 13
@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;
}
 
Example 14
@Override
public void check(final Mapper mapper, final MappedClass mc, final Set<ConstraintViolation> ve) {
    EntityModel<?> model = mc.getEntityModel();
    Constructor<?> fullConstructor = ConstructorCreator.getFullConstructor(model);
    if (fullConstructor != null) {
        for (final Parameter parameter : fullConstructor.getParameters()) {
            String name = ConstructorCreator.getParameterName(parameter);
            if (model.getFieldModelByName(name) == null) {
                throw new ConstraintViolationException(
                    new ConstraintViolation(Level.FATAL, mc, getClass(), Sofia.misnamedConstructorParameter(model.getType(), name)));
            }
        }
    }
}
 
Example 15
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 16
@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;
}
 
Example 17
Source Project: hottub   File: CaptureTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 18
public void check() {
    final Class<?> cls = this.getClass();
    final Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    final Parameter[] params = ctor.getParameters();
    final String[] names = names();
    final int[] modifiers = modifiers();
    final Class[] types = types();

    System.err.println("Testing class " + cls);

    if (params.length == names.length) {
        for (int i = 0; i < names.length; i++) {
            System.err.println("Testing parameter " + params[i].getName());
            if (!params[i].getName().equals(names[i]))
                error("Expected parameter name " + names[i] +
                      " got " + params[i].getName());
            if (params[i].getModifiers() != modifiers[i])
                error("Expected parameter modifiers " +
                      modifiers[i] + " got " +
                      params[i].getModifiers());
            if (!params[i].getType().equals(types[i]))
                error("Expected parameter type " + types[i] +
                      " got " + params[i].getType());
        }
    } else
        error("Expected " + names.length + " parameters");

}
 
Example 19
@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;
}
 
Example 20
@SuppressWarnings("unchecked")
@NotNull
private static <T> T parseObject(@NotNull Class<T> type, @Nullable SvnServerParser tokenParser) throws IOException {
  if (tokenParser != null && tokenParser.readItem(ListBeginToken.class) == null)
    tokenParser = null;

  final int depth = getDepth(tokenParser);

  if (type.isArray()) {
    final List<Object> result = new ArrayList<>();

    if (tokenParser != null) {
      while (true) {
        final Object element = parse(type.getComponentType(), tokenParser);
        if (getDepth(tokenParser) < depth)
          break;

        result.add(element);
      }
    }

    return (T) result.toArray((Object[]) Array.newInstance(type.getComponentType(), result.size()));
  }

  final Constructor<?>[] ctors = type.getDeclaredConstructors();
  if (ctors.length != 1) {
    throw new IllegalStateException("Can't find parser ctor for object: " + type.getName());
  }
  final Constructor<?> ctor = ctors[0];
  final Parameter[] ctorParams = ctor.getParameters();
  Object[] params = new Object[ctorParams.length];
  for (int i = 0; i < params.length; ++i) {
    params[i] = parse(ctorParams[i].getType(), getDepth(tokenParser) == depth ? tokenParser : null);
  }
  while (tokenParser != null && getDepth(tokenParser) >= depth) {
      tokenParser.readToken();
  }

  try {
    if (!ctor.isAccessible())
      ctor.setAccessible(true);

    return (T) ctor.newInstance(params);
  } catch (ReflectiveOperationException e) {
    throw new IllegalStateException(e);
  }
}