1 23 package com.sun.enterprise.deployment.annotation.impl; 24 25 import com.sun.enterprise.deployment.annotation.ComponentInfo; 26 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Modifier ; 31 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 39 44 public class ComponentDefinition implements ComponentInfo { 45 final private Class clazz; 46 final private List <Constructor > constructors = new ArrayList <Constructor >(); 47 final private List <Class > classes = new ArrayList <Class >(); 48 final private List <Field > fields = new ArrayList <Field >(); 49 final private Map <MethodKey, Method > methodMap = new HashMap <MethodKey, Method >(); 50 51 public ComponentDefinition(Class clazz) { 52 this.clazz = clazz; 53 constructClassList(); 54 initializeConstructors(); 55 initializeFields(); 56 initializeMethods(); 57 } 58 59 public Field [] getFields() { 60 return fields.toArray(new Field [fields.size()]); 61 } 62 63 public Method [] getMethods() { 64 return methodMap.values().toArray(new Method [methodMap.size()]); 65 } 66 67 public Constructor [] getConstructors() { 68 return constructors.toArray(new Constructor [constructors.size()]); 69 } 70 71 private void constructClassList() { 72 classes.add(clazz); 73 Class 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 87 private void initializeConstructors() { 88 for (Class cl : classes) { 89 for (Constructor constr : cl.getConstructors()) { 90 constructors.add(constr); 91 } 92 } 93 } 94 95 private void initializeFields() { 96 for (Class cl : classes) { 97 for (Field f : cl.getDeclaredFields()) { 98 fields.add(f); 99 } 100 } 101 } 102 103 private void initializeMethods() { 104 for (Class cl: classes) { 105 for (Method method : cl.getDeclaredMethods()) { 106 methodMap.put(new MethodKey(method), method); 107 } 108 } 109 } 110 111 private class MethodKey { 112 private Method m = null; 113 private int hashCode; 114 private String className = null; 115 private Package classPackage = null; 116 117 private MethodKey(Method m) { 118 this.m = m; 119 hashCode = m.getName().hashCode(); 120 className = m.getDeclaringClass().getName(); 122 classPackage = m.getDeclaringClass().getPackage(); 123 } 124 125 public int hashCode() { 126 127 return hashCode; 128 } 129 130 135 public boolean equals(Object o) { 136 if (!(o instanceof MethodKey)) { 137 return false; 138 } 139 140 MethodKey mk2 = (MethodKey)o; 141 Method 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 return Modifier.isPrivate(modifiers2) && isSamePackage 157 && className.equals(mk2.className); 158 } else { return Modifier.isPublic(modifiers2) || 160 Modifier.isProtected(modifiers2) || 161 isPackageProtected2 && isSamePackage; 162 } 163 } 164 165 return false; 166 } 167 } 168 } 169 | Popular Tags |