From 1fb6a28a73ca17dbb8c4b3059db590e2f9620943 Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 3 Aug 2011 09:21:41 +0000 Subject: Correct tag name git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1153404 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/impl/JavaClassIntrospectorImpl.java | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java (limited to 'sca-java-1.x/tags/1.6-TUSCANY-3909/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java') diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java new file mode 100644 index 0000000000..0bcec3c78f --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java @@ -0,0 +1,141 @@ +/* + * 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.impl; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.impl.JavaIntrospectionHelper; + +/** + * An extensible Java class introspector implementation. + * + * @version $Rev$ $Date$ + */ +public class JavaClassIntrospectorImpl { + + private List visitors; + + public JavaClassIntrospectorImpl(List visitors) { + this.visitors = visitors; + } + + /** + * JSR-250 PFD recommends the following guidelines for how annotations + * interact with inheritance in order to keep the resulting complexity in + * control: + *
    + *
  1. Class-level annotations only affect the class they annotate and + * their members, that is, its methods and fields. They never affect a + * member declared by a superclass, even if it is not hidden or overridden + * by the class in question. + *
  2. In addition to affecting the annotated class, class-level + * annotations may act as a shorthand for member-level annotations. If a + * member carries a specific member-level annotation, any annotations of the + * same type implied by a class-level annotation are ignored. In other + * words, explicit member-level annotations have priority over member-level + * annotations implied by a class-level annotation. + *
  3. The interfaces implemented by a class never contribute annotations + * to the class itself or any of its members. + *
  4. Members inherited from a superclass and which are not hidden or + * overridden maintain the annotations they had in the class that declared + * them, including member-level annotations implied by class-level ones. + *
  5. Member-level annotations on a hidden or overridden member are always + * ignored. + *
+ */ + public void introspectClass(JavaImplementation type, Class clazz) + throws IntrospectionException { + for (JavaClassVisitor extension : visitors) { + extension.visitClass(clazz, type); + } + + for (Constructor constructor : clazz.getConstructors()) { + for (JavaClassVisitor extension : visitors) { + extension.visitConstructor(constructor, type); + // Assuming the visitClass or visitConstructor will populate the + // type.getConstructors + JavaConstructorImpl definition = type.getConstructors().get(constructor); + if (definition != null) { + for (JavaParameterImpl p : definition.getParameters()) { + extension.visitConstructorParameter(p, type); + } + } + } + } + + Set fields = JavaIntrospectionHelper.getAllPublicAndProtectedFields(clazz, true); + for (Field field : fields) { + for (JavaClassVisitor extension : visitors) { + extension.visitField(field, type); + } + } + + // Check if any private fields have illegal annotations that should be raised as errors + Set privateFields = JavaIntrospectionHelper.getPrivateFields(clazz); + for (Field field : privateFields) { + for (JavaClassVisitor processor : visitors) { + processor.visitField(field, type); + } + } + + Set methods = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(clazz, true); + for (Method method : methods) { + for (JavaClassVisitor processor : visitors) { + processor.visitMethod(method, type); + } + } + + // Check if any private methods have illegal annotations that should be raised as errors + Set privateMethods = JavaIntrospectionHelper.getPrivateMethods(clazz); + for (Method method : privateMethods) { + for (JavaClassVisitor processor : visitors) { + processor.visitMethod(method, type); + } + } + + Class superClass = clazz.getSuperclass(); + if (superClass != null) { + visitSuperClass(superClass, type); + } + + for (JavaClassVisitor extension : visitors) { + extension.visitEnd(clazz, type); + } + } + + private void visitSuperClass(Class clazz, JavaImplementation type) throws IntrospectionException { + if (!Object.class.equals(clazz)) { + for (JavaClassVisitor extension : visitors) { + extension.visitSuperClass(clazz, type); + } + clazz = clazz.getSuperclass(); + if (clazz != null) { + visitSuperClass(clazz, type); + } + } + } + +} -- cgit v1.2.3