/** * Return the constructor on the supplied class that matches this signature. * * @param clazz the class whose constructor should be returned * @return the matching constructor * @throws ClassNotFoundException if the class for one of the parameters could not be loaded * @throws NoSuchMethodException if no matching constructor could be found */ public <T> Constructor<T> getConstructor(Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException { if (!isConstructor) throw new AssertionError("Illegal call to getConstructor on a Method Signature"); Class<?>[] types = getParameterTypes(clazz.getClassLoader()); return clazz.getConstructor(types); }
public Signature(Constructor constructor) { name = "init"; setParameterTypes(constructor.getParameterTypes()); isConstructor = true; } /**
private void introspectInterface(Class<?> interfaceClass) { this.interfaceClass = interfaceClass; methodSignatures = new ArrayList<>(); Class<?> superClass = interfaceClass.getSuperclass(); if (superClass != null) { superType = superClass.getName(); } interfaces = new ArrayList<>(); for (Method method : interfaceClass.getDeclaredMethods()) { Signature signature = new Signature(method); if (!methodSignatures.contains(signature)) { methodSignatures.add(signature); } } addInterfaces(interfaceClass, interfaces); }
public Set<Method> getInjectionMethods(Class<?> type, Collection<Service<ComponentType>> services) { Set<Signature> exclude = getOperations(services); Set<Method> methods = new HashSet<>(); while (type != null) { for (Method method : type.getDeclaredMethods()) { // check method accessibility int modifiers = method.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers)) { continue; } if (!(Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers))) { continue; } if (!isSetter(method)) { continue; } // exclude methods we have seen already (i.e. in a contract or overriden in a subclass) // we check using the signature as the method itself will have been declared on a different class Signature signature = new Signature(method); if (exclude.contains(signature)) { continue; } exclude.add(signature); methods.add(method); } type = type.getSuperclass(); } return methods; }
private Set<Signature> getOperations(Collection<Service<ComponentType>> services) { Set<Signature> operations = new HashSet<>(); for (Service<ComponentType> definition : services) { List<? extends Operation> ops = definition.getServiceContract().getOperations(); for (Operation operation : ops) { String name = operation.getName(); List<? extends DataType> inputTypes = operation.getInputTypes(); List<String> paramTypes = new ArrayList<>(inputTypes.size()); for (DataType inputType : inputTypes) { paramTypes.add(inputType.getType().getName()); } operations.add(new Signature(name, paramTypes)); } } return operations; }
/** * Constructor that initializes this signature based on the supplied method. The name is taken from the method and the parameter types from the * method's parameter classes. * * @param method the method to initialize from */ public Signature(Method method) { name = method.getName(); setParameterTypes(method.getParameterTypes()); }