summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
blob: ff70fb89eba6b5182b6f77a9429dce75187003af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.implementation.java.introspect.impl;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.implementation.java.IntrospectionException;
import org.apache.tuscany.sca.implementation.java.JavaElementImpl;
import org.apache.tuscany.sca.implementation.java.JavaImplementation;
import org.apache.tuscany.sca.implementation.java.JavaParameterImpl;
import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor;
import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl;
import org.apache.tuscany.sca.interfacedef.util.XMLType;

/**
 * Base class for ImplementationProcessors that handle annotations that add
 * Properties.
 * 
 * @version $Rev$ $Date$
 */
public abstract class AbstractPropertyProcessor<A extends Annotation> extends BaseJavaClassVisitor {
    private final Class<A> annotationClass;

    protected AbstractPropertyProcessor(ExtensionPointRegistry registry, Class<A> annotationClass) {
        super(registry);
        this.annotationClass = annotationClass;
    }
    
    private static boolean removeProperty(JavaElementImpl prop, JavaImplementation type) {
        if(prop==null) {
            return false;
        }
        List<Property> props = type.getProperties();
        for(int i=0;i<props.size();i++) {
            if(props.get(i).getName().equals(prop.getName())) {
                props.remove(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException {
        A annotation = method.getAnnotation(annotationClass);
        if (annotation != null) {
        	
	        if (!JavaIntrospectionHelper.isSetter(method)) {
	            throw new IllegalPropertyException("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 Property");
	        }
	
	        String name = getName(annotation);
	        if (name == null || "".equals(name)) {
	            name = method.getName();
	            if (name.startsWith("set")) {
	                name = JavaIntrospectionHelper.toPropertyName(method.getName());
	            }
	        }
	
	        Map<String, JavaElementImpl> properties = type.getPropertyMembers();
	        JavaElementImpl prop = properties.get(name);
	        // Setter override field
	        if (prop != null && prop.getElementType() != ElementType.FIELD) {
	            throw new DuplicatePropertyException(name);
	        }
	
	        removeProperty(prop, type);
	        
	        JavaElementImpl element = new JavaElementImpl(method, 0);
	        Property property = createProperty(name, element);
	
	        // add databinding available as annotations, as extensions
	
	        initProperty(property, annotation);
	        type.getProperties().add(property);
	        properties.put(name, element);
        }
        
        // enforce the constraint that an ordinary method's argument can not be a Property
        Annotation paramsAnnotations[][] = method.getParameterAnnotations();
        for (int i = 0; i < paramsAnnotations.length; i++) {
        	Annotation argAnnotations[] = paramsAnnotations[i];
        	for (int j = 0; j < argAnnotations.length; j++) {
        		if(argAnnotations[j].annotationType() == org.oasisopen.sca.annotation.Property.class) {
        			throw new IllegalPropertyException("[JCA90001] Argument " + (i+1) + " of method " + method.getName() + " in class " + method.getDeclaringClass() + " can not be a Property");
        		}
        	}
		}
    }


    @Override
    public void visitField(Field field, JavaImplementation type) throws IntrospectionException {

        A annotation = field.getAnnotation(annotationClass);
        if (annotation == null) {
            return;
        }
        
        if(Modifier.isStatic(field.getModifiers())) {
        	throw new IllegalPropertyException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as a Property");
        }

        if(Modifier.isFinal(field.getModifiers())) {
            throw new IllegalPropertyException("[JCA90011] Final field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as a Property");
        }

        String name = getName(annotation);
        if (name == null) {
            name = "";
        }
        if ("".equals(name) || name.equals(field.getType().getName())) {
            name = field.getName();
        }

        Map<String, JavaElementImpl> properties = type.getPropertyMembers();
        JavaElementImpl prop = properties.get(name);
        // Setter override field
        if (prop != null && prop.getElementType() == ElementType.FIELD) {
            throw new DuplicatePropertyException(name);
        }

        if (prop == null) {
            JavaElementImpl element = new JavaElementImpl(field);
            Property property = createProperty(name, element);
            initProperty(property, annotation);
            type.getProperties().add(property);
            properties.put(name, element);
        }
    }

    @Override
    public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type)
        throws IntrospectionException {

        Map<String, JavaElementImpl> properties = type.getPropertyMembers();
        A annotation = parameter.getAnnotation(annotationClass);
        if (annotation != null) {
            String name = getName(annotation);
            if (name == null) {
                name = parameter.getType().getName();
            }
            if (!"".equals(name) && !"".equals(parameter.getName()) && !name.equals(parameter.getName())) {
                throw new InvalidConstructorException("Mismatched property name: " + parameter);
            }
            if ("".equals(name) && "".equals(parameter.getName())) {
                throw new InvalidPropertyException("[JCA90013] Missing property name: " + parameter);
            }
            if ("".equals(name)) {
                name = parameter.getName();
            }
            
            if (!getRequired(annotation)) {
                throw new InvalidPropertyException("[JCA90014] Constructor property must not have required=false: " + type.getName());
            }

            JavaElementImpl prop = properties.get(name);
            // Setter override field
            if (prop != null && prop.getElementType() != ElementType.FIELD) {
                throw new DuplicatePropertyException(name);
            }
            removeProperty(prop, type);
            
            parameter.setName(name);
            parameter.setClassifer(annotationClass);
            Property property = createProperty(name, parameter);
            initProperty(property, annotation);
            type.getProperties().add(property);
            properties.put(name, parameter);
        }
    }
    
    protected Property createProperty(String name, JavaElementImpl element) throws IntrospectionException {

        Type type = element.getGenericType();
        Class<?> javaType = element.getType();
        
        return createProperty(assemblyFactory, name, javaType, type);

    }

    public static Property createProperty(AssemblyFactory assemblyFactory, String name, Class<?> javaClass, Type genericType) {
        Property property = assemblyFactory.createProperty();
        property.setName(name);

        if (javaClass.isArray() || Collection.class.isAssignableFrom(javaClass)) {
            property.setMany(true);
            if (javaClass.isArray()) {
                Class<?> propType = javaClass.getComponentType();
                Type genericPropType = propType;
                if (genericType instanceof GenericArrayType) {
                    genericPropType = ((GenericArrayType)genericType).getGenericComponentType();
                }
                DataType dt = new DataTypeImpl(null, propType, genericPropType, XMLType.UNKNOWN);
                property.setDataType(dt);
            } else {
                if (genericType instanceof ParameterizedType) {
                    // Collection<BaseType> property;
                    Type genericPropType = ((ParameterizedType)genericType).getActualTypeArguments()[0];
                    Class<?> propType = JavaIntrospectionHelper.getErasure(genericPropType);
                    DataType dt = new DataTypeImpl(null, propType, genericPropType, XMLType.UNKNOWN);
                    property.setDataType(dt);
                } else {
                    // Collection property;
                    DataType dt = new DataTypeImpl(null, Object.class, Object.class, XMLType.UNKNOWN);
                    property.setDataType(dt);
                }
            }
        } else {
            DataType dt = new DataTypeImpl(null, javaClass, genericType, XMLType.UNKNOWN);
            property.setDataType(dt);
        }
        return property;
    }

    protected abstract String getName(A annotation);
    protected abstract boolean getRequired(A annotation);

    protected abstract void initProperty(Property property, A annotation) throws IntrospectionException;


}