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

The following examples show how to use java.lang.reflect.Constructor#getParameters() . 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 check out the related API usage on the sidebar.
Example 1
Source File: ComponentEventBusUtil.java    From flow with 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 2
Source File: NewInstanceMutationOperator.java    From pitest-descartes with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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 3
Source File: DataBoundConfigurator.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@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 4
Source File: AnnotatedElementParameterTest.java    From j2objc with Apache License 2.0 6 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);

    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 5
Source File: StandardReflectionParameterNameDiscoverer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: ParameterResolutionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 7
Source File: CaptureTest.java    From openjdk-8-source with 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 8
Source File: CaptureTest.java    From hottub with 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 9
Source File: StandardReflectionParameterNameDiscoverer.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: CaptureTest.java    From openjdk-jdk8u-backup with 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 11
Source File: ConstructorParameterNameConstraint.java    From morphia with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: StandardReflectionParameterNameDiscoverer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 13
Source File: ExecutableParameterTest.java    From j2objc with 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 14
Source File: Injector.java    From elepy with 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 15
Source File: CaptureTest.java    From openjdk-jdk8u with 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 16
Source File: IocPlugin.java    From oxygen with 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 17
Source File: CaptureTest.java    From jdk8u60 with 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
Source File: CaptureTest.java    From TencentKona-8 with 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 19
Source File: CaptureTest.java    From openjdk-8 with 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 20
Source File: MessageParser.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@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);
  }
}