KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > impl > ComponentDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.impl;
24
25 import com.sun.enterprise.deployment.annotation.ComponentInfo;
26
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Modifier JavaDoc;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38
39 /**
40  * This class represents the view of a class from annotation.
41  *
42  * @author Shing Wai Chan
43  */

44 public class ComponentDefinition implements ComponentInfo {
45     final private Class JavaDoc clazz;
46     final private List JavaDoc<Constructor JavaDoc> constructors = new ArrayList JavaDoc<Constructor JavaDoc>();
47     final private List JavaDoc<Class JavaDoc> classes = new ArrayList JavaDoc<Class JavaDoc>();
48     final private List JavaDoc<Field JavaDoc> fields = new ArrayList JavaDoc<Field JavaDoc>();
49     final private Map JavaDoc<MethodKey, Method JavaDoc> methodMap = new HashMap JavaDoc<MethodKey, Method JavaDoc>();
50
51     public ComponentDefinition(Class JavaDoc clazz) {
52         this.clazz = clazz;
53         constructClassList();
54         initializeConstructors();
55         initializeFields();
56         initializeMethods();
57     }
58
59     public Field JavaDoc[] getFields() {
60         return fields.toArray(new Field JavaDoc[fields.size()]);
61     }
62
63     public Method JavaDoc[] getMethods() {
64         return methodMap.values().toArray(new Method JavaDoc[methodMap.size()]);
65     }
66
67     public Constructor JavaDoc[] getConstructors() {
68         return constructors.toArray(new Constructor JavaDoc[constructors.size()]);
69     }
70
71     private void constructClassList() {
72         classes.add(clazz);
73         Class JavaDoc parent = clazz;
74         while ((parent = parent.getSuperclass()) != null) {
75             if (parent.getPackage() == null ||
76                     !parent.getPackage().getName().startsWith("java.lang")) {
77                 classes.add(0, parent);
78             }
79         }
80     }
81
82     /**
83      * In P.148 of "The Java Langugage Specification 2/e",
84      * Constructors, static initializers, and instance initializers are not
85      * members and therefore not inherited.
86      */

87     private void initializeConstructors() {
88         for (Class JavaDoc cl : classes) {
89             for (Constructor JavaDoc constr : cl.getConstructors()) {
90                 constructors.add(constr);
91             }
92         }
93     }
94
95     private void initializeFields() {
96         for (Class JavaDoc cl : classes) {
97             for (Field JavaDoc f : cl.getDeclaredFields()) {
98                 fields.add(f);
99             }
100         }
101     }
102
103     private void initializeMethods() {
104         for (Class JavaDoc cl: classes) {
105             for (Method JavaDoc method : cl.getDeclaredMethods()) {
106                 methodMap.put(new MethodKey(method), method);
107             }
108         }
109     }
110
111     private class MethodKey {
112         private Method JavaDoc m = null;
113         private int hashCode;
114         private String JavaDoc className = null;
115         private Package JavaDoc classPackage = null;
116
117         private MethodKey(Method JavaDoc m) {
118             this.m = m;
119             hashCode = m.getName().hashCode();
120             // store className and classPackage as getters are native
121
className = m.getDeclaringClass().getName();
122             classPackage = m.getDeclaringClass().getPackage();
123         }
124
125         public int hashCode() {
126
127             return hashCode;
128         }
129
130         /**
131          * This equals method is defined in terms of inheritance overriding.
132          * We depends on java compiler to rule out irrelvant cases here.
133          * @return true for overriding and false otherwise
134          */

135         public boolean equals(Object JavaDoc o) {
136             if (!(o instanceof MethodKey)) {
137                 return false;
138             }
139
140             MethodKey mk2 = (MethodKey)o;
141             Method JavaDoc m2 = mk2.m;
142             if (m.getName().equals(m2.getName()) && Arrays.equals(
143                     m.getParameterTypes(), m2.getParameterTypes())) {
144                 int modifiers = m.getModifiers();
145                 int modifiers2 = m2.getModifiers();
146                 boolean isPackageProtected2 = !Modifier.isPublic(modifiers2) &&
147                         !Modifier.isProtected(modifiers2) &&
148                         !Modifier.isPrivate(modifiers2);
149                 boolean isSamePackage =
150                         (classPackage == null && mk2.classPackage == null) ||
151                         (classPackage != null && mk2.classPackage != null &&
152                             classPackage.getName().equals(
153                             mk2.classPackage.getName()));
154                 if (Modifier.isPrivate(modifiers)) {
155                     // need exact match
156
return Modifier.isPrivate(modifiers2) && isSamePackage
157                             && className.equals(mk2.className);
158                 } else { // public, protected, package protected
159
return Modifier.isPublic(modifiers2) ||
160                             Modifier.isProtected(modifiers2) ||
161                             isPackageProtected2 && isSamePackage;
162                 }
163             }
164
165             return false;
166         }
167     }
168 }
169
Popular Tags