KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > model > ElementImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.apt.model;
12
13 import java.lang.annotation.Annotation JavaDoc;
14 import java.lang.reflect.Proxy JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.lang.model.element.AnnotationMirror;
20 import javax.lang.model.element.Element;
21 import javax.lang.model.element.Modifier;
22 import javax.lang.model.element.Name;
23 import javax.lang.model.element.PackageElement;
24 import javax.lang.model.type.TypeMirror;
25 import javax.lang.model.util.Elements;
26
27 import org.eclipse.jdt.core.compiler.CharOperation;
28 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
29 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
30 import org.eclipse.jdt.internal.compiler.lookup.Binding;
31 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
32
33 /**
34  * Element represents any defined Java language element - a package,
35  * a method, a class or interface. Contrast with DeclaredType.
36  */

37 public abstract class ElementImpl
38     implements javax.lang.model.element.Element, IElementInfo
39 {
40     public final BaseProcessingEnvImpl _env;
41     public final Binding _binding;
42     
43     protected ElementImpl(BaseProcessingEnvImpl env, Binding binding) {
44         _env = env;
45         _binding = binding;
46     }
47
48     @Override JavaDoc
49     public TypeMirror asType() {
50         return _env.getFactory().newTypeMirror(_binding);
51     }
52
53     @SuppressWarnings JavaDoc("unchecked") // for cast of newProxyInstance() to A
54
@Override JavaDoc
55     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationClass) {
56         AnnotationBinding[] annoInstances = getAnnotationBindings();
57         if( annoInstances == null || annoInstances.length == 0 || annotationClass == null )
58             return null;
59
60         String JavaDoc annoTypeName = annotationClass.getName();
61         if( annoTypeName == null ) return null;
62         annoTypeName = annoTypeName.replace('$', '.');
63         for( AnnotationBinding annoInstance : annoInstances) {
64             if (annoInstance == null)
65                 continue;
66             ReferenceBinding binding = annoInstance.getAnnotationType();
67             if ( binding != null && binding.isAnnotationType() ) {
68                 char[] qName;
69                 if (binding.isMemberType()) {
70                     qName = CharOperation.concatWith(binding.enclosingType().compoundName, binding.sourceName, '.');
71                     CharOperation.replace(qName, '$', '.');
72                 } else {
73                     qName = CharOperation.concatWith(binding.compoundName, '.');
74                 }
75                 if( annoTypeName.equals(new String JavaDoc(qName)) ){
76                     AnnotationMirrorImpl annoMirror =
77                         (AnnotationMirrorImpl)_env.getFactory().newAnnotationMirror(annoInstance);
78                     return (A)Proxy.newProxyInstance(annotationClass.getClassLoader(),
79                             new Class JavaDoc[]{ annotationClass }, annoMirror );
80                 }
81             }
82         }
83         return null;
84     }
85     
86     /**
87      * @return the set of compiler annotation bindings on this element
88      */

89     protected abstract AnnotationBinding[] getAnnotationBindings();
90
91     @Override JavaDoc
92     public List JavaDoc<? extends AnnotationMirror> getAnnotationMirrors() {
93         return _env.getFactory().getAnnotationMirrors(getAnnotationBindings());
94     }
95
96     @Override JavaDoc
97     public Set JavaDoc<Modifier> getModifiers() {
98         // Most subclasses implement this; this default is appropriate for
99
// PackageElement and TypeParameterElement.
100
return Collections.emptySet();
101     }
102
103     @Override JavaDoc
104     public Name getSimpleName() {
105         return new NameImpl(_binding.shortReadableName());
106     }
107
108     @Override JavaDoc
109     public int hashCode() {
110         return _binding.hashCode();
111     }
112
113     // TODO: equals() implemented as == of JDT bindings. Valid within
114
// a single Compiler instance; breaks in IDE if processors cache values.
115
@Override JavaDoc
116     public boolean equals(Object JavaDoc obj) {
117         if (this == obj)
118             return true;
119         if (obj == null)
120             return false;
121         if (getClass() != obj.getClass())
122             return false;
123         final ElementImpl other = (ElementImpl) obj;
124         if (_binding == null) {
125             if (other._binding != null)
126                 return false;
127         } else if (_binding != other._binding)
128             return false;
129         return true;
130     }
131
132     @Override JavaDoc
133     public String JavaDoc toString() {
134         return _binding.toString();
135     }
136
137     @Override JavaDoc
138     public String JavaDoc getFileName() {
139         // Subclasses should override and return something of value
140
return null;
141     }
142
143     /**
144      * @return the package containing this element. The package of a PackageElement is itself.
145      * @see javax.lang.model.util.Elements#getPackageOf(javax.lang.model.element.Element)
146      */

147     abstract /* package */ PackageElement getPackage();
148
149     /**
150      * Subclassed by VariableElementImpl, TypeElementImpl, and ExecutableElementImpl.
151      * This base implementation suffices for other types.
152      * @see Elements#hides()
153      * @return true if this element hides {@code hidden}
154      */

155     public boolean hides(Element hidden)
156     {
157         return false;
158     }
159 }
160
Popular Tags