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

The following examples show how to use java.lang.reflect.Executable#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: MethodParameter.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected static int findParameterIndex(Parameter parameter) {
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] allParams = executable.getParameters();
	// Try first with identity checks for greater performance.
	for (int i = 0; i < allParams.length; i++) {
		if (parameter == allParams[i]) {
			return i;
		}
	}
	// Potentially try again with object equality checks in order to avoid race
	// conditions while invoking java.lang.reflect.Executable.getParameters().
	for (int i = 0; i < allParams.length; i++) {
		if (parameter.equals(allParams[i])) {
			return i;
		}
	}
	throw new IllegalArgumentException("Given parameter [" + parameter +
			"] does not match any parameter in the declaring executable");
}
 
Example 2
Source File: HotSpotResolvedJavaMethodImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Parameter[] getParameters() {
    Executable javaMethod = toJava();
    if (javaMethod == null) {
        return null;
    }

    java.lang.reflect.Parameter[] javaParameters = javaMethod.getParameters();
    Parameter[] res = new Parameter[javaParameters.length];
    for (int i = 0; i < res.length; i++) {
        java.lang.reflect.Parameter src = javaParameters[i];
        String paramName = src.isNamePresent() ? src.getName() : null;
        res[i] = new Parameter(paramName, src.getModifiers(), this, i);
    }
    return res;
}
 
Example 3
Source File: CTVMUtilities.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,
                             Executable target) {
    super(Opcodes.ASM5, cw);
    this.lineNumbers = lines;

    StringBuilder builder = new StringBuilder("(");
    for (Parameter parameter : target.getParameters()) {
        builder.append(Utils.toJVMTypeSignature(parameter.getType()));
    }
    builder.append(")");
    if (target instanceof Constructor) {
        targetName = "<init>";
        builder.append("V");
    } else {
        targetName = target.getName();
        builder.append(Utils.toJVMTypeSignature(
                ((Method) target).getReturnType()));
    }
    targetDesc = builder.toString();
}
 
Example 4
Source File: MethodParameter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected static int findParameterIndex(Parameter parameter) {
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] allParams = executable.getParameters();
	// Try first with identity checks for greater performance.
	for (int i = 0; i < allParams.length; i++) {
		if (parameter == allParams[i]) {
			return i;
		}
	}
	// Potentially try again with object equality checks in order to avoid race
	// conditions while invoking java.lang.reflect.Executable.getParameters().
	for (int i = 0; i < allParams.length; i++) {
		if (parameter.equals(allParams[i])) {
			return i;
		}
	}
	throw new IllegalArgumentException("Given parameter [" + parameter +
			"] does not match any parameter in the declaring executable");
}
 
Example 5
Source File: InjectorProcessor.java    From panda with Apache License 2.0 6 votes vote down vote up
protected InjectorResourceBind<Annotation>[] fetchBinds(Annotation[] annotations, Executable executable) {
    InjectorResources resources = injector.getResources();
    Parameter[] parameters = executable.getParameters();
    InjectorResourceBind<Annotation>[] binds = ObjectUtils.cast(new InjectorResourceBind[parameters.length]);

    for (int index = 0; index < annotations.length; index++) {
        Annotation annotation = annotations[index];
        Parameter parameter = parameters[index];

        Class<?> requiredType = annotation != null ? annotation.annotationType() : parameter.getType();
        Option<InjectorResourceBind<Annotation>> bindValue = resources.getBind(requiredType);

        binds[index] = bindValue.getOrElseThrow(() -> {
            throw new DependencyInjectionException("Missing bind for " + parameter + " parameter");
        });
    }

    return binds;
}
 
Example 6
Source File: MethodParameterFactory.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
private static int getIndex(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] parameters = executable.getParameters();
	for (int i = 0; i < parameters.length; i++) {
		if (parameters[i] == parameter) {
			return i;
		}
	}
	throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]",
		parameter, executable.toGenericString()));
}
 
Example 7
Source File: ReflectionPredicates.java    From raistlic-lib-commons-core with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test(Executable executable) {

  for (Parameter parameter : executable.getParameters()) {
    if (parameterPredicate.test(parameter)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: ReflectionPredicates.java    From raistlic-lib-commons-core with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test(Executable executable) {

  for (Parameter parameter : executable.getParameters()) {
    if (!parameterPredicate.test(parameter)) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source File: MethodParameterFactory.java    From tutorials with MIT License 5 votes vote down vote up
private static int getIndex(Parameter parameter) {
    Assert.notNull(parameter, "Parameter must not be null");
    Executable executable = parameter.getDeclaringExecutable();
    Parameter[] parameters = executable.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i] == parameter) {
            return i;
        }
    }
    throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
}
 
Example 10
Source File: AnnotatedElementParameterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static void checkParameter0GetAnnotationsByType(
        Executable executable, Class<? extends Annotation> annotationType,
        String... expectedAnnotationStrings) throws Exception {
    Parameter parameter = executable.getParameters()[0];
    AnnotatedElementTestSupport.assertGetAnnotationsByType(
            parameter, annotationType, expectedAnnotationStrings);
}
 
Example 11
Source File: AnnotatedElementParameterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static void checkParameter0GetDeclaredAnnotationsByType(
        Executable executable, Class<? extends Annotation> annotationType,
        String... expectedAnnotationStrings) throws Exception {
    Parameter parameter = executable.getParameters()[0];
    AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
            parameter, annotationType, expectedAnnotationStrings);
}
 
Example 12
Source File: AnnotatedElementParameterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static void checkParameter0DeclaredAnnotation(
        Executable executable, Class<? extends Annotation> annotationType,
        String expectedAnnotationString) throws Exception {
    Parameter parameter = executable.getParameters()[0];

    // isAnnotationPresent
    assertIsAnnotationPresent(parameter, annotationType, expectedAnnotationString != null);

    // getDeclaredAnnotation
    assertGetDeclaredAnnotation(parameter, annotationType, expectedAnnotationString);
}
 
Example 13
Source File: InjectorProcessor.java    From panda with Apache License 2.0 5 votes vote down vote up
protected Collection<InjectorResourceHandler<Annotation, Object, ?>>[] fetchHandlers(Executable executable) {
    Collection<InjectorResourceHandler<Annotation, Object, ?>>[] handlers = ObjectUtils.cast(new Collection[executable.getParameterCount()]);
    Parameter[] parameters = executable.getParameters();

    for (int index = 0; index < parameters.length; index++) {
        handlers[index] = injector.getResources().getHandler(parameters[index]);
    }

    return handlers;
}
 
Example 14
Source File: InjectorProcessor.java    From panda with Apache License 2.0 5 votes vote down vote up
protected Object[] fetchValues(InjectorCache cache, Executable executable, Object... injectorArgs) throws Exception {
    Parameter[] parameters = executable.getParameters();
    Object[] values = new Object[cache.getInjectable().length];

    for (int index = 0; index < values.length; index++) {
        values[index] = fetchValue(cache, parameters[index], index, injectorArgs);
    }

    return values;
}
 
Example 15
Source File: DefaultParameterNameProvider.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private List<String> getParameterNamesEx(Executable methodOrConstructor) {
  Parameter[] parameters = methodOrConstructor.getParameters();
  List<String> parameterNames = new ArrayList<>(parameters.length);
  for (int idx = 0; idx < parameters.length; idx++) {
    Parameter parameter = parameters[idx];
    String parameterName = parameter.isNamePresent() ? parameter.getName() : "arg" + idx;
    parameterNames.add(parameterName);
  }
  return Collections.unmodifiableList(parameterNames);
}
 
Example 16
Source File: ParameterResolutionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertAutowirableParameters(Executable executable) {
	int startIndex = (executable instanceof Constructor)
			&& ClassUtils.isInnerClass(executable.getDeclaringClass()) ? 1 : 0;
	Parameter[] parameters = executable.getParameters();
	for (int parameterIndex = startIndex; parameterIndex < parameters.length; parameterIndex++) {
		Parameter parameter = parameters[parameterIndex];
		assertTrue("Parameter " + parameter + " must be autowirable",
				ParameterResolutionDelegate.isAutowirable(parameter, parameterIndex));
	}
}
 
Example 17
Source File: JacksonValueMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
static Parameter getParameter(AnnotatedParameter ctorParam) {
    Executable constructor = (Executable) ctorParam.getOwner().getMember();
    return constructor.getParameters()[ctorParam.getIndex()];
}
 
Example 18
Source File: JacksonValueMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
TypedElement fromConstructorParameter(AnnotatedParameter ctorParam) {
    Executable constructor = (Executable) ctorParam.getOwner().getMember();
    Parameter parameter = constructor.getParameters()[ctorParam.getIndex()];
    AnnotatedType fieldType = transform(ClassUtils.getParameterTypes(constructor, type)[ctorParam.getIndex()], parameter);
    return new TypedElement(fieldType, parameter);
}
 
Example 19
Source File: ParameterAutowireUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
 * annotations directly on a {@link Parameter} will fail for inner class
 * constructors.
 * <h4>Bug in javac in JDK &lt; 9</h4>
 * <p>The parameter annotations array in the compiled byte code excludes an entry
 * for the implicit <em>enclosing instance</em> parameter for an inner class
 * constructor.
 * <h4>Workaround</h4>
 * <p>This method provides a workaround for this off-by-one error by allowing the
 * caller to access annotations on the preceding {@link Parameter} object (i.e.,
 * {@code index - 1}). If the supplied {@code index} is zero, this method returns
 * an empty {@code AnnotatedElement}.
 * <h4>WARNING</h4>
 * <p>The {@code AnnotatedElement} returned by this method should never be cast and
 * treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
 * {@link Parameter#getType()}, etc.) will not match those for the declared parameter
 * at the given index in an inner class constructor.
 * @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
 * if the aforementioned bug is in effect
 */
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
			executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
		// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
		// for inner classes, so access it with the actual parameter index lowered by 1
		return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
	}
	return parameter;
}
 
Example 20
Source File: ParameterResolutionDelegate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
 * annotations directly on a {@link Parameter} will fail for inner class
 * constructors.
 * <h4>Bug in javac in JDK &lt; 9</h4>
 * <p>The parameter annotations array in the compiled byte code excludes an entry
 * for the implicit <em>enclosing instance</em> parameter for an inner class
 * constructor.
 * <h4>Workaround</h4>
 * <p>This method provides a workaround for this off-by-one error by allowing the
 * caller to access annotations on the preceding {@link Parameter} object (i.e.,
 * {@code index - 1}). If the supplied {@code index} is zero, this method returns
 * an empty {@code AnnotatedElement}.
 * <h4>WARNING</h4>
 * <p>The {@code AnnotatedElement} returned by this method should never be cast and
 * treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
 * {@link Parameter#getType()}, etc.) will not match those for the declared parameter
 * at the given index in an inner class constructor.
 * @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
 * if the aforementioned bug is in effect
 */
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
			executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
		// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
		// for inner classes, so access it with the actual parameter index lowered by 1
		return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
	}
	return parameter;
}