private Signature getCallback(Class<?> implClass, String name) { while (Object.class != implClass) { try { Method callback = implClass.getDeclaredMethod(name); return new Signature(callback); } catch (NoSuchMethodException e) { implClass = implClass.getSuperclass(); } } return null; }
private Method getMethod(Class<?> implClass, Signature signature) throws NoSuchMethodException, ClassNotFoundException { return signature == null ? null : signature.getMethod(implClass); }
private <T> Constructor<T> getConstructor(Class<T> implClass, Signature signature) throws ClassNotFoundException, NoSuchMethodException { Constructor<T> ctr = signature.getConstructor(implClass); ctr.setAccessible(true); return ctr; }
try { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getSignature().getMethod(implementationClass); return method.getGenericParameterTypes()[methodSite.getParam()]; } catch (ClassNotFoundException e) { try { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getSignature().getConstructor(implementationClass); return method.getGenericParameterTypes()[methodSite.getParam()]; } catch (ClassNotFoundException e) {
public void processInjectionSites(InjectingComponentType componentType, ImplementationManagerDefinition managerDefinition) { Map<InjectionSite, Injectable> mappings = componentType.getInjectionSites(); // add injections for all the active constructor args Map<InjectionSite, Injectable> construction = managerDefinition.getConstruction(); Signature constructor = componentType.getConstructor(); Set<Injectable> byConstruction = new HashSet<Injectable>(constructor.getParameterTypes().size()); for (int i = 0; i < constructor.getParameterTypes().size(); i++) { InjectionSite site = new ConstructorInjectionSite(constructor, i); Injectable attribute = mappings.get(site); construction.put(site, attribute); byConstruction.add(attribute); } // add field/method injections Map<InjectionSite, Injectable> postConstruction = managerDefinition.getPostConstruction(); Map<InjectionSite, Injectable> reinjection = managerDefinition.getReinjectables(); for (Map.Entry<InjectionSite, Injectable> entry : mappings.entrySet()) { InjectionSite site = entry.getKey(); if (site instanceof ConstructorInjectionSite) { continue; } Injectable attribute = entry.getValue(); if (!byConstruction.contains(attribute)) { postConstruction.put(site, attribute); } reinjection.put(site, attribute); } }
Method method = methodSite.getSignature().getMethod(implementationClass); return method.getParameterTypes()[methodSite.getParam()]; } catch (ClassNotFoundException e) { try { ConstructorInjectionSite methodSite = (ConstructorInjectionSite) site; Constructor<?> method = methodSite.getSignature().getConstructor(implementationClass); return method.getParameterTypes()[methodSite.getParam()]; } catch (ClassNotFoundException e) {
private Signature getCallback(Class<?> implClass, Class<? extends Annotation> annotation) { while (Object.class != implClass) { for (Method method : implClass.getDeclaredMethods()) { if (method.isAnnotationPresent(annotation)) { return new Signature(method); } } implClass = implClass.getSuperclass(); } return null; }
private Method getMethod(MethodInjectionSite methodSite, String implementationClass, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException { Class<?> clazz = classLoader.loadClass(implementationClass); return methodSite.getSignature().getMethod(clazz); }
private void evaluateConstructor(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); Constructor<?> constructor; try { if (componentType.getConstructor() == null) { // there was an error with the constructor previously, just return return; } constructor = componentType.getConstructor().getConstructor(implClass); } catch (ClassNotFoundException e) { throw new AssertionError(e); } catch (NoSuchMethodException e) { throw new AssertionError(e); } TypeMapping typeMapping = context.getTypeMapping(implClass); Type[] parameterTypes = constructor.getGenericParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { InjectionSite site = new ConstructorInjectionSite(constructor, i); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } Type parameterType = parameterTypes[i]; String name = helper.getSiteName(constructor, i, null); Annotation[] annotations = constructor.getParameterAnnotations()[i]; processSite(componentType, typeMapping, name, constructor, parameterType, site, annotations, context); } }
public Set<Method> getInjectionMethods(Class<?> type, Collection<AbstractService> services) { Set<Signature> exclude = getOperations(services); Set<Method> methods = new HashSet<Method>(); 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; }
try { MethodInjectionSite methodSite = (MethodInjectionSite) site; Method method = methodSite.getSignature().getMethod(implementationClass); Injector<?> injector = reflectionFactory.createInjector(method, factory); injectors.put(attribute, injector);
public void visitMethod(Init annotation, Method method, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { if (!validate(method, context, componentType)) { return; } componentType.setInitMethod(new Signature(method)); }
Map<OperationKey, MethodHolder> operations = new HashMap<OperationKey, MethodHolder>(); for (ManagementOperationInfo operationInfo : info.getOperations()) { Method method = operationInfo.getSignature().getMethod(clazz); String description = operationInfo.getDescription(); Set<Role> roles = operationInfo.getRoles();
public void visitMethod(Destroy annotation, Method method, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { if (!validate(method, context, componentType)) { return; } componentType.setDestroyMethod(new Signature(method)); }
private Set<Signature> getOperations(Collection<AbstractService> services) { Set<Signature> operations = new HashSet<Signature>(); for (AbstractService 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<String>(inputTypes.size()); for (DataType<?> inputType : inputTypes) { paramTypes.add(inputType.getPhysical().getName()); } operations.add(new Signature(name, paramTypes)); } } return operations; }
private void introspect(Object instance, ManagementInfo info) { for (Method method : instance.getClass().getMethods()) { ManagementOperation annotation = method.getAnnotation(ManagementOperation.class); if (annotation == null) { continue; } String description = annotation.description(); if (description.trim().length() == 0) { description = null; } Signature signature = new Signature(method); String[] roleNames = annotation.rolesAllowed(); Set<Role> roles = new HashSet<Role>(); for (String name : roleNames) { roles.add(new Role(name)); } String path = annotation.path(); ManagementOperationInfo operationInfo = new ManagementOperationInfo(signature, path, OperationType.UNDEFINED, description, roles); info.addOperation(operationInfo); } }
private Signature findConstructor(Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { Constructor<?>[] constructors = implClass.getDeclaredConstructors(); Constructor<?> selected = null; if (constructors.length == 1) { selected = constructors[0]; } else { for (Constructor<?> constructor : constructors) { if (constructor.isAnnotationPresent(org.oasisopen.sca.annotation.Constructor.class)) { if (selected != null) { context.addError(new AmbiguousConstructor(implClass, componentType)); return null; } selected = constructor; } } if (selected == null) { context.addError(new NoConstructorFound(implClass, componentType)); return null; } } return new Signature(selected); }
public void visitMethod(ManagementOperation annotation, Method method, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { ManagementInfo info = componentType.getManagementInfo(); if (info == null) { // there was no management annotation on the type - record an error Class<?> clazz = method.getDeclaringClass(); context.addError(new InvalidAnnotation("Implementation is missing @" + Management.class.getSimpleName(), clazz)); return; } String description = annotation.description(); if (description.trim().length() == 0) { description = null; } Signature signature = new Signature(method); Set<Role> roles = new HashSet<Role>(); for (String roleName : annotation.rolesAllowed()) { roles.add(new Role(roleName)); } String path = annotation.path(); org.fabric3.api.annotation.management.OperationType operationType = annotation.type(); OperationType type = OperationType.valueOf(operationType.toString()); ManagementOperationInfo operationInfo = new ManagementOperationInfo(signature, path, type, description, roles); info.addOperation(operationInfo); }
public void visitMethod(Consumer annotation, Method method, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { if (method.getParameterTypes().length > 1) { InvalidConsumerMethod failure = new InvalidConsumerMethod("Consumer method " + method + " has more than one parameter", method, componentType); context.addError(failure); return; } TypeMapping typeMapping = context.getTypeMapping(implClass); List<DataType<?>> types = introspectParameterTypes(method, typeMapping); // TODO handle policies String name = helper.getSiteName(method, annotation.value()); Signature signature = new Signature(method); ConsumerDefinition definition = new ConsumerDefinition(name, types); int sequence = annotation.sequence(); if (sequence < 0) { context.addError(new InvalidConsumerMethod("Sequence number cannot be negative: " + method, method, componentType)); } else { definition.setSequence(sequence); } componentType.add(definition, signature); }