001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler.typesystem.impl;
020:
021: import org.apache.beehive.netui.compiler.typesystem.declaration.*;
022: import org.apache.beehive.netui.compiler.typesystem.impl.declaration.*;
023: import org.apache.beehive.netui.compiler.typesystem.impl.type.AnnotationTypeImpl;
024: import org.apache.beehive.netui.compiler.typesystem.impl.type.ArrayTypeImpl;
025: import org.apache.beehive.netui.compiler.typesystem.impl.type.ClassTypeImpl;
026: import org.apache.beehive.netui.compiler.typesystem.impl.type.InterfaceTypeImpl;
027: import org.apache.beehive.netui.compiler.typesystem.impl.type.PrimitiveTypeImpl;
028: import org.apache.beehive.netui.compiler.typesystem.impl.type.TypeVariableImpl;
029: import org.apache.beehive.netui.compiler.typesystem.impl.type.VoidTypeImpl;
030: import org.apache.beehive.netui.compiler.typesystem.impl.type.ErrorTypeImpl;
031: import org.apache.beehive.netui.compiler.typesystem.type.AnnotationType;
032: import org.apache.beehive.netui.compiler.typesystem.type.ClassType;
033: import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
034: import org.apache.beehive.netui.compiler.typesystem.type.InterfaceType;
035: import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
036: import org.apache.beehive.netui.compiler.typesystem.type.ReferenceType;
037: import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
038: import org.apache.beehive.netui.compiler.typesystem.type.TypeVariable;
039: import org.apache.beehive.netui.compiler.typesystem.type.VoidType;
040: import org.apache.beehive.netui.compiler.typesystem.TypesystemElement;
041:
042: import java.util.ArrayList;
043: import java.util.List;
044:
045: public class WrapperFactory {
046: private static final WrapperFactory INSTANCE = new WrapperFactory();
047:
048: private WrapperFactory() {
049: }
050:
051: public static WrapperFactory get() {
052: return INSTANCE;
053: }
054:
055: public TypeInstance getTypeInstance(
056: com.sun.mirror.type.TypeMirror delegate) {
057: if (delegate == null)
058: return null;
059:
060: if (delegate instanceof com.sun.mirror.type.ReferenceType) {
061: return getReferenceType((com.sun.mirror.type.ReferenceType) delegate);
062: } else if (delegate instanceof com.sun.mirror.type.VoidType) {
063: return getVoidType((com.sun.mirror.type.VoidType) delegate);
064: } else {
065: assert delegate instanceof com.sun.mirror.type.PrimitiveType : delegate
066: .getClass().getName();
067: return getPrimitiveType((com.sun.mirror.type.PrimitiveType) delegate);
068: }
069: }
070:
071: public VoidType getVoidType(com.sun.mirror.type.VoidType delegate) {
072: if (delegate == null)
073: return null;
074: return new VoidTypeImpl(delegate);
075: }
076:
077: public ReferenceType getReferenceType(
078: com.sun.mirror.type.ReferenceType delegate) {
079: if (delegate == null)
080: return null;
081:
082: if (delegate instanceof com.sun.mirror.type.DeclaredType) {
083: return getDeclaredType((com.sun.mirror.type.DeclaredType) delegate);
084: } else if (delegate instanceof com.sun.mirror.type.ArrayType) {
085: return new ArrayTypeImpl(
086: (com.sun.mirror.type.ArrayType) delegate);
087: } else {
088: assert delegate instanceof com.sun.mirror.type.TypeVariable : delegate
089: .getClass().getName();
090: return getTypeVariable((com.sun.mirror.type.TypeVariable) delegate);
091: }
092: }
093:
094: public PrimitiveType getPrimitiveType(
095: com.sun.mirror.type.PrimitiveType delegate) {
096: if (delegate == null)
097: return null;
098:
099: return new PrimitiveTypeImpl(delegate);
100: }
101:
102: public TypeVariable getTypeVariable(
103: com.sun.mirror.type.TypeVariable delegate) {
104: if (delegate == null)
105: return null;
106:
107: return new TypeVariableImpl(delegate);
108: }
109:
110: public DeclaredType getDeclaredType(
111: com.sun.mirror.type.DeclaredType delegate) {
112: if (delegate == null)
113: return null;
114:
115: if (delegate instanceof com.sun.mirror.type.ClassType) {
116: return getClassType((com.sun.mirror.type.ClassType) delegate);
117: } else if (delegate instanceof com.sun.mirror.type.InterfaceType) {
118: return getInterfaceType((com.sun.mirror.type.InterfaceType) delegate);
119: }
120:
121: //
122: // This must be an error type, which is indicated by a DeclaredType with no type declaration.
123: //
124: assert delegate.getDeclaration() == null : "expected error type, got "
125: + delegate.toString()
126: + " with declaration "
127: + delegate.getDeclaration();
128: return getErrorType(delegate);
129: }
130:
131: public DeclaredType getErrorType(
132: com.sun.mirror.type.DeclaredType delegate) {
133: if (delegate == null)
134: return null;
135: return new ErrorTypeImpl(delegate);
136: }
137:
138: public ClassType getClassType(com.sun.mirror.type.ClassType delegate) {
139: if (delegate == null)
140: return null;
141:
142: return new ClassTypeImpl(delegate);
143: }
144:
145: public InterfaceType getInterfaceType(
146: com.sun.mirror.type.InterfaceType delegate) {
147: if (delegate == null)
148: return null;
149:
150: if (delegate instanceof com.sun.mirror.type.AnnotationType) {
151: return getAnnotationType((com.sun.mirror.type.AnnotationType) delegate);
152: }
153:
154: return new InterfaceTypeImpl(delegate);
155: }
156:
157: public AnnotationType getAnnotationType(
158: com.sun.mirror.type.AnnotationType delegate) {
159: if (delegate == null)
160: return null;
161: return new AnnotationTypeImpl(delegate);
162: }
163:
164: public AnnotationInstance getAnnotationInstance(
165: com.sun.mirror.declaration.AnnotationMirror delegate,
166: TypesystemElement containingElement) {
167: if (delegate == null)
168: return null;
169: return new AnnotationInstanceImpl(delegate, containingElement);
170: }
171:
172: public AnnotationValue getAnnotationValue(
173: com.sun.mirror.declaration.AnnotationValue delegate,
174: AnnotationInstance containingAnnotation) {
175: if (delegate == null)
176: return null;
177: return new AnnotationValueImpl(delegate, containingAnnotation);
178: }
179:
180: public Declaration getDeclaration(
181: com.sun.mirror.declaration.Declaration delegate) {
182: if (delegate == null)
183: return null;
184:
185: if (delegate instanceof com.sun.mirror.declaration.MemberDeclaration) {
186: return getMemberDeclaration((com.sun.mirror.declaration.MemberDeclaration) delegate);
187: } else if (delegate instanceof com.sun.mirror.declaration.ParameterDeclaration) {
188: return getParameterDeclaration((com.sun.mirror.declaration.ParameterDeclaration) delegate);
189: } else {
190: assert delegate instanceof com.sun.mirror.declaration.TypeParameterDeclaration : delegate
191: .getClass().getName();
192: return getTypeParameterDeclaration((com.sun.mirror.declaration.TypeParameterDeclaration) delegate);
193: }
194: }
195:
196: public MemberDeclaration getMemberDeclaration(
197: com.sun.mirror.declaration.MemberDeclaration delegate) {
198: if (delegate == null)
199: return null;
200:
201: if (delegate instanceof com.sun.mirror.declaration.TypeDeclaration) {
202: return getTypeDeclaration((com.sun.mirror.declaration.TypeDeclaration) delegate);
203: } else if (delegate instanceof com.sun.mirror.declaration.ExecutableDeclaration) {
204: return getExecutableDeclaration((com.sun.mirror.declaration.ExecutableDeclaration) delegate);
205: } else {
206: assert delegate instanceof com.sun.mirror.declaration.FieldDeclaration : delegate
207: .getClass().getName();
208: return getFieldDeclaration((com.sun.mirror.declaration.FieldDeclaration) delegate);
209: }
210: }
211:
212: public TypeDeclaration getTypeDeclaration(
213: com.sun.mirror.declaration.TypeDeclaration delegate) {
214: if (delegate == null)
215: return null;
216:
217: if (delegate instanceof com.sun.mirror.declaration.ClassDeclaration) {
218: return getClassDeclaration((com.sun.mirror.declaration.ClassDeclaration) delegate);
219: } else {
220: assert delegate instanceof com.sun.mirror.declaration.InterfaceDeclaration : delegate
221: .getClass().getName();
222: return getInterfaceDeclaration((com.sun.mirror.declaration.InterfaceDeclaration) delegate);
223: }
224: }
225:
226: public ClassDeclaration getClassDeclaration(
227: com.sun.mirror.declaration.ClassDeclaration delegate) {
228: if (delegate == null)
229: return null;
230:
231: return new ClassDeclarationImpl(delegate);
232: }
233:
234: public InterfaceDeclaration getInterfaceDeclaration(
235: com.sun.mirror.declaration.InterfaceDeclaration delegate) {
236: if (delegate == null)
237: return null;
238:
239: if (delegate instanceof com.sun.mirror.declaration.AnnotationTypeDeclaration) {
240: return getAnnotationTypeDeclaration((com.sun.mirror.declaration.AnnotationTypeDeclaration) delegate);
241: }
242:
243: return new InterfaceDeclarationImpl(delegate);
244: }
245:
246: public ExecutableDeclaration getExecutableDeclaration(
247: com.sun.mirror.declaration.ExecutableDeclaration delegate) {
248: if (delegate == null)
249: return null;
250:
251: if (delegate instanceof com.sun.mirror.declaration.MethodDeclaration) {
252: return getMethodDeclaration((com.sun.mirror.declaration.MethodDeclaration) delegate);
253: }
254:
255: assert delegate instanceof com.sun.mirror.declaration.ConstructorDeclaration : delegate
256: .getClass().getName();
257: return getConstructorDeclaration((com.sun.mirror.declaration.ConstructorDeclaration) delegate);
258: }
259:
260: public ParameterDeclaration getParameterDeclaration(
261: com.sun.mirror.declaration.ParameterDeclaration delegate) {
262: if (delegate == null)
263: return null;
264:
265: return new ParameterDeclarationImpl(delegate);
266: }
267:
268: public PackageDeclaration getPackageDeclaration(
269: com.sun.mirror.declaration.PackageDeclaration delegate) {
270: if (delegate == null)
271: return null;
272:
273: return new PackageDeclarationImpl(delegate);
274: }
275:
276: public ConstructorDeclaration getConstructorDeclaration(
277: com.sun.mirror.declaration.ConstructorDeclaration delegate) {
278: if (delegate == null)
279: return null;
280:
281: return new ConstructorDeclarationImpl(delegate);
282: }
283:
284: public MethodDeclaration getMethodDeclaration(
285: com.sun.mirror.declaration.MethodDeclaration delegate) {
286: if (delegate == null)
287: return null;
288:
289: if (delegate instanceof com.sun.mirror.declaration.AnnotationTypeElementDeclaration) {
290: return getAnnotationTypeElementDeclaration((com.sun.mirror.declaration.AnnotationTypeElementDeclaration) delegate);
291: }
292:
293: return new MethodDeclarationImpl(delegate);
294: }
295:
296: public AnnotationTypeDeclaration getAnnotationTypeDeclaration(
297: com.sun.mirror.declaration.AnnotationTypeDeclaration delegate) {
298: if (delegate == null)
299: return null;
300:
301: return new AnnotationTypeDeclarationImpl(delegate);
302: }
303:
304: public AnnotationTypeElementDeclaration getAnnotationTypeElementDeclaration(
305: com.sun.mirror.declaration.AnnotationTypeElementDeclaration delegate) {
306: if (delegate == null)
307: return null;
308:
309: return new AnnotationTypeElementDeclarationImpl(delegate);
310: }
311:
312: public FieldDeclaration getFieldDeclaration(
313: com.sun.mirror.declaration.FieldDeclaration delegate) {
314: if (delegate == null)
315: return null;
316:
317: return new FieldDeclarationImpl(delegate);
318: }
319:
320: public TypeParameterDeclaration getTypeParameterDeclaration(
321: com.sun.mirror.declaration.TypeParameterDeclaration delegate) {
322: if (delegate == null)
323: return null;
324:
325: return new TypeParameterDeclarationImpl(delegate);
326: }
327:
328: public Object getWrapper(Object o,
329: TypesystemElement containingElement) {
330: if (o == null)
331: return null;
332:
333: if (o instanceof com.sun.mirror.type.TypeMirror) {
334: return getTypeInstance((com.sun.mirror.type.TypeMirror) o);
335: } else if (o instanceof com.sun.mirror.declaration.Declaration) {
336: return getDeclaration((com.sun.mirror.declaration.Declaration) o);
337: } else if (o instanceof com.sun.mirror.declaration.AnnotationMirror) {
338: assert containingElement != null;
339: return getAnnotationInstance(
340: (com.sun.mirror.declaration.AnnotationMirror) o,
341: containingElement);
342: } else if (o instanceof com.sun.mirror.declaration.AnnotationValue) {
343: assert containingElement != null;
344: assert containingElement instanceof AnnotationInstance : containingElement
345: .getClass().getName();
346: AnnotationInstance containingAnnotation = (AnnotationInstance) containingElement;
347: return getAnnotationValue(
348: (com.sun.mirror.declaration.AnnotationValue) o,
349: containingAnnotation);
350: } else if (o instanceof com.sun.mirror.declaration.PackageDeclaration) {
351: return getPackageDeclaration((com.sun.mirror.declaration.PackageDeclaration) o);
352: } else if (o instanceof List) {
353: List list = (List) o;
354: ArrayList ret = new ArrayList(list.size());
355:
356: for (Object i : list) {
357: ret.add(getWrapper(i, containingElement));
358: }
359:
360: return ret;
361: }
362:
363: return o;
364: }
365: }
|