Package org.oasisopen.sca.annotation

Examples of org.oasisopen.sca.annotation.Reference


    public static void inject(Object instance, ServletContext sc) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

        Class<?> clazz = instance.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            if (field.isAnnotationPresent(Reference.class)) {
                Reference ref = field.getAnnotation(Reference.class);
                String name = ref.name() != null && !ref.name().equals("") ? ref.name() : field.getName();
                Object value = getReference(name, field.getType(), sc);
                setField(instance, field, value);
            } else if (field.isAnnotationPresent(Property.class)) {
                Property prop = field.getAnnotation(Property.class);
                String name = prop.name() != null && !prop.name().equals("") ? prop.name() : field.getName();
                Object value = getProperty(name, sc);
                setField(instance, field, value);
            } else if (field.isAnnotationPresent(ComponentName.class)) {
                RuntimeComponent rc = (RuntimeComponent)sc.getAttribute(COMPONENT_ATTR);
                setField(instance, field, rc.getName());
            } else if (field.isAnnotationPresent(Context.class)) {
                setField(instance, field, getComponentContext(sc));
            }
        }

        for (Method method : clazz.getDeclaredMethods()) {
            if (!method.getName().startsWith("set") || method.getParameterTypes().length != 1) {
                continue;
            }
            String targetName = method.getName().substring(3);
            Class<?> type = method.getParameterTypes()[0];

            if (method.isAnnotationPresent(Reference.class)) {
                Reference ref = method.getAnnotation(Reference.class);
                String name = ref.name() != null && !ref.name().equals("") ? ref.name() : targetName;
                Object value = getReference(name, type, sc);
                setMethod(instance, method, value);
            } else if (method.isAnnotationPresent(Property.class)) {
                Property prop = method.getAnnotation(Property.class);
                String name = prop.name() != null && !prop.name().equals("") ? prop.name() : targetName;
View Full Code Here


        this.javaFactory = javaFactory;
    }

    @Override
    public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException {
        Reference annotation = method.getAnnotation(Reference.class);
        if (annotation == null) {
            return; // Not a reference annotation.
        }
        if (!JavaIntrospectionHelper.isSetter(method)) {
            throw new IllegalReferenceException("Annotated method is not a setter: " + method, method);
        }
        String name = annotation.name();
        if ("".equals(name)) {
            name = JavaIntrospectionHelper.toPropertyName(method.getName());
        }
        JavaElementImpl ref = type.getReferenceMembers().get(name);
        // Setter override field
View Full Code Here

    }


    @Override
    public void visitField(Field field, JavaImplementation type) throws IntrospectionException {
        Reference annotation = field.getAnnotation(Reference.class);
        if (annotation == null) {
            return;
        }
        String name = annotation.name();
        if ("".equals(name)) {
            name = field.getName();
        }
        JavaElementImpl ref = type.getReferenceMembers().get(name);
        if (ref != null && ref.getElementType() == ElementType.FIELD) {
View Full Code Here

    }

    @Override
    public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type)
        throws IntrospectionException {
        Reference refAnnotation = parameter.getAnnotation(Reference.class);
        if (refAnnotation == null) {
            return;
        }
        String paramName = parameter.getName();
        String name = getReferenceName(paramName, parameter.getIndex(), refAnnotation.name());
        JavaElementImpl ref = type.getReferenceMembers().get(name);

        // Setter override field
        if (ref != null && ref.getElementType() != ElementType.FIELD) {
            throw new DuplicateReferenceException(name);
View Full Code Here

        JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract();
        reference.setInterfaceContract(interfaceContract);

        // reference.setMember((Member)element.getAnchor());
        boolean required = true;
        Reference ref = element.getAnnotation(Reference.class);
        if (ref != null) {
            required = ref.required();
        }
        // reference.setRequired(required);
        reference.setName(name);
        Class<?> rawType = element.getType();
        if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
View Full Code Here

        final Class<?> clazz = bean.getClass();       

        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) {

                Reference annotation = (Reference) method.getAnnotation(getReferenceAnnotationType());
               
                if (annotation != null) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException("Reference annotation is not supported on static methods");
                    }
                   
                    if (Modifier.isPrivate(method.getModifiers())) {
                        throw new IllegalStateException("Reference annotation is not supported on private methods");
                    }

                    if (method.getParameterTypes().length == 0) {
                        throw new IllegalStateException("Reference annotation requires at least one argument: " + method);
                    }
                   
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
                    if (pd != null) {
                        String refName = annotation.name();
                        if ("".equals(refName)) {
                            injectReference(bean, pd, pd.getName());
                        } else {
                            injectReference(bean, pd, refName);
                        }
                    }
                }
            }
        });
       
        ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
            public void doWith(Field field) {

                Reference annotation = (Reference) field.getAnnotation(getReferenceAnnotationType());
               
                if (annotation != null) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalStateException("Reference annotation is not supported on static fields");
                    }
                   
                    if (Modifier.isPrivate(field.getModifiers())) {
                        throw new IllegalStateException("Reference annotation is not supported on private fields");
                    }

                    ReflectionUtils.makeAccessible(field);
                   
                    Object referenceObj = null;
                    String refName = annotation.name();
                    if ("".equals(refName)) {
                        referenceObj = component.getService(field.getType(), field.getName());
                    } else {
                        referenceObj = component.getService(field.getType(), refName);
                    }                       
View Full Code Here

        super(registry);
    }   

    @Override
    public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException {
        Reference annotation = method.getAnnotation(Reference.class);
        if (annotation != null) {
 
          if (!JavaIntrospectionHelper.isSetter(method)) {
              throw new IllegalReferenceException("Annotated method is not a setter: " + method, method);
          }
        
          if(Modifier.isStatic(method.getModifiers())) {
            throw new IllegalPropertyException("Static method " + method.getName() +" in class " + method.getDeclaringClass().getName() + " can not be annotated as a Reference");
          }
         
          String name = annotation.name();
          if ("".equals(name)) {
              name = JavaIntrospectionHelper.toPropertyName(method.getName());
          }
          JavaElementImpl ref = type.getReferenceMembers().get(name);
          // Setter override field
View Full Code Here

    }


    @Override
    public void visitField(Field field, JavaImplementation type) throws IntrospectionException {
        Reference annotation = field.getAnnotation(Reference.class);
        if (annotation == null) {
            return;
        }
       
        if(Modifier.isStatic(field.getModifiers())) {
          throw new IllegalReferenceException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as Reference");
        }
        String name = annotation.name();
        if ("".equals(name)) {
            name = field.getName();
        }
        JavaElementImpl ref = type.getReferenceMembers().get(name);
        if (ref != null && ref.getElementType() == ElementType.FIELD) {
View Full Code Here

    }

    @Override
    public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type)
        throws IntrospectionException {
        Reference refAnnotation = parameter.getAnnotation(Reference.class);
        if (refAnnotation == null) {
            return;
        }
       
        if (!refAnnotation.required()) {
            throw new InvalidReferenceException("[JCA90016] Constructor has @Reference with required=false: " + type.getName());
        }
       
        if (refAnnotation.name() == null || refAnnotation.name().length() < 1) {
            throw new InvalidReferenceException("[JCA90018] @Reference in a Constructor must have a name attribute" + type.getName());
        }
       
        String paramName = parameter.getName();
        String name = getReferenceName(paramName, parameter.getIndex(), refAnnotation.name());
        JavaElementImpl ref = type.getReferenceMembers().get(name);

        // Setter override field
        if (ref != null && ref.getElementType() != ElementType.FIELD) {
            throw new DuplicateReferenceException(name);
View Full Code Here

            reference.setAllowsPassByReference(implementation.isAllowsPassByReference());
        }

        // reference.setMember((Member)element.getAnchor());
        boolean required = true;
        Reference ref = element.getAnnotation(Reference.class);
        if (ref != null) {
            required = ref.required();
        }
        // reference.setRequired(required);
        reference.setName(name);
        Class<?> rawType = element.getType();
        if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
View Full Code Here

TOP

Related Classes of org.oasisopen.sca.annotation.Reference

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.