001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso;
005:
006: import org.eclipse.jdt.core.IField;
007: import org.eclipse.jdt.core.IMethod;
008: import org.eclipse.jdt.core.IType;
009: import org.eclipse.jdt.core.JavaModelException;
010: import org.eclipse.jdt.core.dom.Annotation;
011: import org.eclipse.jdt.core.dom.IAnnotationBinding;
012:
013: import com.tc.aspectwerkz.reflect.ClassInfo;
014: import com.tc.aspectwerkz.reflect.FieldInfo;
015: import com.tc.aspectwerkz.reflect.MethodInfo;
016: import com.tc.backport175.bytecode.AnnotationElement;
017: import com.tc.object.bytecode.aspectwerkz.SimpleClassInfo;
018:
019: import java.lang.ref.SoftReference;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: public class JavaModelClassInfo extends SimpleClassInfo {
026: private IType fType;
027: private String fSuperClassSig;
028: private ClassInfo fSuperClass;
029: private String[] fInterfaceSigs;
030: private ClassInfo[] fInterfaces;
031: private List<AnnotationElement.Annotation> fAnnotations = new ArrayList<AnnotationElement.Annotation>();
032: private final Map<IMethod, SoftReference> fMethodInfoCache = new HashMap<IMethod, SoftReference>();
033: private final Map<IField, SoftReference> fFieldInfoCache = new HashMap<IField, SoftReference>();
034:
035: private static final String[] NO_INTERFACE_SIGS = new String[0];
036: private static final ClassInfo[] NO_INTERFACES = new ClassInfo[0];
037:
038: public JavaModelClassInfo(String classname) {
039: super (classname);
040: fInterfaceSigs = NO_INTERFACE_SIGS;
041: fInterfaces = NO_INTERFACES;
042: }
043:
044: public JavaModelClassInfo(IType type) {
045: super (type.getFullyQualifiedName('$'));
046: fType = type;
047: try {
048: fSuperClassSig = type.getSuperclassTypeSignature();
049: if (fSuperClassSig != null) {
050: fSuperClass = new JavaModelClassInfo(JdtUtils
051: .getResolvedTypeName(fSuperClassSig, type));
052: }
053: fInterfaceSigs = NO_INTERFACE_SIGS;
054: fInterfaces = NO_INTERFACES;
055: if (type.isClass()) {
056: fInterfaceSigs = type.getSuperInterfaceTypeSignatures();
057: fInterfaces = new JavaModelClassInfo[fInterfaceSigs.length];
058: for (int i = 0; i < fInterfaceSigs.length; i++) {
059: fInterfaces[i] = new JavaModelClassInfo(JdtUtils
060: .getResolvedTypeName(fInterfaceSigs[i],
061: type));
062: }
063: }
064: } catch (JavaModelException jme) {
065: fInterfaceSigs = NO_INTERFACE_SIGS;
066: fInterfaces = NO_INTERFACES;
067: }
068: }
069:
070: public IType getType() {
071: return fType;
072: }
073:
074: public ClassInfo getSuperclass() {
075: return fSuperClass;
076: }
077:
078: public ClassInfo[] getInterfaces() {
079: return fInterfaces.clone();
080: }
081:
082: public MethodInfo getMethod(ClassInfoFactory classInfoFactory,
083: IMethod method) throws JavaModelException {
084: MethodInfo info = null;
085: synchronized (fMethodInfoCache) {
086: SoftReference ref = fMethodInfoCache.get(method);
087: if (ref != null) {
088: info = (MethodInfo) ref.get();
089: }
090: if (info == null) {
091: info = new JavaModelMethodInfo(classInfoFactory, method);
092: fMethodInfoCache.put(method, new SoftReference(info));
093: }
094: }
095: return info;
096: }
097:
098: public FieldInfo getField(ClassInfoFactory classInfoFactory,
099: IField field) {
100: FieldInfo info = null;
101: synchronized (fFieldInfoCache) {
102: SoftReference ref = fFieldInfoCache.get(field);
103: if (ref != null) {
104: info = (FieldInfo) ref.get();
105: }
106: if (info == null) {
107: info = new JavaModelFieldInfo(classInfoFactory, field);
108: fFieldInfoCache.put(field, new SoftReference(info));
109: }
110: }
111: return info;
112: }
113:
114: public void clearAnnotations() {
115: fAnnotations.clear();
116: }
117:
118: public void addAnnotation(Annotation annotation) {
119: IAnnotationBinding binding = annotation
120: .resolveAnnotationBinding();
121: String name = binding.getAnnotationType().getQualifiedName();
122: fAnnotations.add(new AnnotationElement.Annotation(name));
123: }
124:
125: public AnnotationElement.Annotation[] getAnnotations() {
126: return fAnnotations
127: .toArray(new AnnotationElement.Annotation[0]);
128: }
129:
130: public boolean isStale() {
131: try {
132: if (fType == null)
133: return true;
134: String super ClassSig = fType.getSuperclassTypeSignature();
135: if (super ClassSig == null && fSuperClassSig != null
136: || super ClassSig != null && fSuperClassSig == null
137: || super ClassSig != null
138: && !super ClassSig.equals(fSuperClassSig)) {
139: return true;
140: }
141:
142: String[] interfaceSigs = fType
143: .getSuperInterfaceTypeSignatures();
144: if (interfaceSigs.length != fInterfaceSigs.length)
145: return true;
146: for (int i = 0; i < interfaceSigs.length; i++) {
147: if (!interfaceSigs[i].equals(fInterfaceSigs[i]))
148: return true;
149: }
150: } catch (JavaModelException jme) {/**/
151: }
152: return false;
153: }
154: }
|