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

The following examples show how to use java.lang.reflect.Executable#getParameterTypes() . 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: NamingUtils.java    From CrossMobile with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String execSignature(Executable exec, boolean fullname) {
    StringBuilder out = new StringBuilder();
    if (fullname)
        out.append(getBareClass(exec.getDeclaringClass()).getName()).append(".");
    if (Constructor.class.isAssignableFrom(exec.getClass()))
        out.append(getClassNameSimple(exec.getDeclaringClass()));
    else
        out.append(exec.getName());
    out.append("(");
    Class<?>[] types = exec.getParameterTypes();
    for (int i = 0; i < types.length; i++) {
        if (i != 0)
            out.append(",");
        out.append(getClassNameFull(types[i]));
    }
    out.append(")");
    return out.toString();
}
 
Example 2
Source File: InjectorProcessor.java    From panda with Apache License 2.0 6 votes vote down vote up
protected Annotation[] fetchAnnotations(Executable executable) {
    Annotation[] injectorAnnotations = injectableCache.get(executable);

    if (injectorAnnotations != null) {
        return injectorAnnotations;
    }

    injectorAnnotations = new Annotation[executable.getParameterTypes().length];
    Annotation[][] parameterAnnotations = injector.getResources().fetchAnnotations(executable);

    for (int index = 0; index < parameterAnnotations.length; index++) {
        for (Annotation annotation : parameterAnnotations[index]) {
            if (annotation.annotationType().isAnnotationPresent(Injectable.class)) {
                injectorAnnotations[index] = annotation;
            }
        }
    }

    injectableCache.put(executable, injectorAnnotations);
    return injectorAnnotations;
}
 
Example 3
Source File: Compiler.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 4
Source File: Compiler.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 5
Source File: Compiler.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 6
Source File: SignatureType.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public SignatureType(Executable method) {
    super(MethodDescriptor.Separator.NONE);
    // Get parameters
    Class<?>[] types = method.getParameterTypes();
    String[] parameterTypes = new String[types.length];
    for (int i = 0; i < types.length; i++) {
        parameterTypes[i] = Utils.toJVMTypeSignature(types[i]);
    }
    // Get return value
    String returnType;
    if (method instanceof Method) {
        returnType = Utils.toJVMTypeSignature(((Method) method)
                .getReturnType());
    } else if (method instanceof Constructor) {
        // Constructor returns void in VM
        returnType = Utils.toJVMTypeSignature(void.class);
    } else {
        throw new Error(String.format("TESTBUG: wrong type of executable "
                + "%s of class %s", method, method.getClass()));
    }
    // Create signature
    setElement("(" + String.join("", parameterTypes)+ ")" + returnType);
    regexp = element;
    setPattern(MethodDescriptor.PatternType.EXACT);
    separator = MethodDescriptor.Separator.NONE;
}
 
Example 7
Source File: Compiler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 8
Source File: ClassScanning.java    From weld-junit with Apache License 2.0 6 votes vote down vote up
private static List<Class<?>> getExecutableParameterTypes(Executable executable, boolean explicitInjection) {

        List<Class<?>> types = new ArrayList<>();

        if (explicitInjection) {
            Annotation[][] paramAnns = executable.getParameterAnnotations();
            Class<?>[] paramTypes = executable.getParameterTypes();
            for (int c = 0; c < paramTypes.length; ++c) {
                if (stream(paramAnns[c]).anyMatch(ClassScanning::isBeanParameterAnnotation)) {
                    types.add(paramTypes[c]);
                }
            }
        } else {
            types.addAll(asList(executable.getParameterTypes()));
        }

        return types;
    }
 
Example 9
Source File: Compiler.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 10
Source File: Compiler.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 11
Source File: Compiler.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void logMethod(Executable method, String message) {
    StringBuilder builder = new StringBuilder("[");
    builder.append(classId);
    builder.append("]\t");
    builder.append(className);
    builder.append("::");
    builder.append(method.getName());
    builder.append('(');
    Class[] params = method.getParameterTypes();
    for (int i = 0, n = params.length - 1; i < n; ++i) {
        builder.append(params[i].getName());
        builder.append(", ");
    }
    if (params.length != 0) {
        builder.append(params[params.length - 1].getName());
    }
    builder.append(')');
    if (message != null) {
        builder.append('\t');
        builder.append(message);
    }
    System.err.println(builder);
}
 
Example 12
Source File: MethodDescriptorCollector.java    From AVM with MIT License 5 votes vote down vote up
private static boolean hasValidParameterType(Executable method) {
    for (Class<?> c : method.getParameterTypes()) {
        if (!isShadowClass(c.getName()) &&
                !isArrayWrapperClass(c.getName()) &&
                !isPrimitive(c) &&
                !isSupportedInternalType(c.getName())) {
            if (method instanceof Method) {
                throw RuntimeAssertionError.unreachable("Transformed method " + method.getDeclaringClass() + "." + method.getName() + " should not have an unsupported parameter type: " + c.getName());
            }
            return false;
        }
    }
    return true;
}
 
Example 13
Source File: AvmDetails.java    From AVM with MIT License 5 votes vote down vote up
private static boolean hasValidParamTypes(Executable method) {
    for (Class<?> c : method.getParameterTypes()) {
        if (!isShadowClass(c.getName()) &&
                !isArrayWrapperClass(c.getName()) &&
                !isPrimitive(c) &&
                !isSupportedInternalType(c.getName())) {
            if (method instanceof Method) {
                throw new AssertionError("transformed method " + method.getDeclaringClass() + "." + method.getName() + " should not have an unsupported parameter type: " + c.getName());
            }
            return false;
        }
    }
    return true;
}
 
Example 14
Source File: ConstructorResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
		Executable executable, Object[] argsToResolve, boolean fallback) {

	TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
	TypeConverter converter = (customConverter != null ? customConverter : bw);
	BeanDefinitionValueResolver valueResolver =
			new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
	Class<?>[] paramTypes = executable.getParameterTypes();

	Object[] resolvedArgs = new Object[argsToResolve.length];
	for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
		Object argValue = argsToResolve[argIndex];
		MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
		GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass());
		if (argValue instanceof AutowiredArgumentMarker) {
			argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
		}
		else if (argValue instanceof BeanMetadataElement) {
			argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
		}
		else if (argValue instanceof String) {
			argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
		}
		Class<?> paramType = paramTypes[argIndex];
		try {
			resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
		}
		catch (TypeMismatchException ex) {
			throw new UnsatisfiedDependencyException(
					mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam),
					"Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) +
					"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
		}
	}
	return resolvedArgs;
}
 
Example 15
Source File: ReflectionUtil.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
static float calculateDistance(Executable exec, Class<?>[] parameterTypes) {
	float cost = 0;

	Class<?>[] execTypes = exec.getParameterTypes();
	for (int i = 0; i < exec.getParameterCount(); i++) {
		if (i >= parameterTypes.length && exec.isVarArgs())
			break;

		Class<?> a = parameterTypes[i];
		Class<?> b = execTypes[i];

		if (i == exec.getParameterCount() - 1 && exec.isVarArgs()) {
			if (isAssignmentCompatible(a, b)) {
				// Passed array for var-args.
				cost += calculateDistance(a, b);
			} else {
				cost += calculateDistance(a, b.getComponentType());
				// Penalty for every parameter that wasn't used.
				cost += (parameterTypes.length - exec.getParameterCount()) * 3F;
				// Death penalty for using var-args.
				cost += 10F;
			}
		} else {
			cost += calculateDistance(a, b);
		}
	}
	return cost;
}
 
Example 16
Source File: ReflectionUtil.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
static boolean isAssignmentCompatible(Executable exec, Class<?>... parameterTypes) {

		// First checks to eliminate an obvious mismatch.
		if (exec.isVarArgs()) {
			if (exec.getParameterCount() == 1 && parameterTypes.length == 0)
				return true;
			if (parameterTypes.length < exec.getParameterCount() - 1)
				return false;
		} else if (parameterTypes.length != exec.getParameterCount()) {
			return false;
		}

		Class<?>[] execTypes = exec.getParameterTypes();
		for (int i = 0; i < exec.getParameterCount(); i++) {

			Class<?> a = parameterTypes[i];
			Class<?> b = execTypes[i];

			if (i == exec.getParameterCount() - 1 && exec.isVarArgs()) {

				// Passed array type for var-args
				if (isAssignmentCompatible(a, b)) {
					return true;
				}

				// Var-args, have to check every element against the array type.
				b = b.getComponentType();
				for (int j = i; j < parameterTypes.length; j++) {
					a = parameterTypes[j];
					if (!isAssignmentCompatible(a, b))
						return false;
				}
				return true;
			} else if (!isAssignmentCompatible(a, b)) {
				return false;
			}
		}
		return true;
	}
 
Example 17
Source File: ConstructorResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
		Executable executable, Object[] argsToResolve, boolean fallback) {

	TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
	TypeConverter converter = (customConverter != null ? customConverter : bw);
	BeanDefinitionValueResolver valueResolver =
			new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
	Class<?>[] paramTypes = executable.getParameterTypes();

	Object[] resolvedArgs = new Object[argsToResolve.length];
	for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
		Object argValue = argsToResolve[argIndex];
		MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
		GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass());
		if (argValue instanceof AutowiredArgumentMarker) {
			argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
		}
		else if (argValue instanceof BeanMetadataElement) {
			argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
		}
		else if (argValue instanceof String) {
			argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
		}
		Class<?> paramType = paramTypes[argIndex];
		try {
			resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
		}
		catch (TypeMismatchException ex) {
			throw new UnsatisfiedDependencyException(
					mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam),
					"Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) +
					"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
		}
	}
	return resolvedArgs;
}
 
Example 18
Source File: ExtractionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a method/constructor can be called with the given argument classes. This includes type
 * widening and vararg. {@code null} is a wildcard.
 *
 * <p>E.g., {@code (int.class, int.class)} matches {@code f(Object...), f(int, int), f(Integer, Object)}
 * and so forth.
 */
public static boolean isInvokable(Executable executable, Class<?>... classes) {
	final int m = executable.getModifiers();
	if (!Modifier.isPublic(m)) {
		return false;
	}
	final int paramCount = executable.getParameterCount();
	final int classCount = classes.length;
	// check for enough classes for each parameter
	if (classCount < paramCount || (executable.isVarArgs() && classCount < paramCount - 1)) {
		return false;
	}
	int currentClass = 0;
	for (int currentParam = 0; currentParam < paramCount; currentParam++) {
		final Class<?> param = executable.getParameterTypes()[currentParam];
		// entire parameter matches
		if (classes[currentClass] == null || ExtractionUtils.isAssignable(classes[currentClass], param, true)) {
			currentClass++;
		}
		// last parameter is a vararg that consumes remaining classes
		else if (currentParam == paramCount - 1 && executable.isVarArgs()) {
			final Class<?> paramComponent = executable.getParameterTypes()[currentParam].getComponentType();
			while (currentClass < classCount && ExtractionUtils.isAssignable(classes[currentClass], paramComponent, true)) {
				currentClass++;
			}
		}
	}
	// check if all classes have been consumed
	return currentClass == classCount;
}