src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/TypeMonikerFactory.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.internal.ws.processor.modeler.annotation;
27
28 import javax.annotation.processing.ProcessingEnvironment;
29 import javax.lang.model.element.Name;
30 import javax.lang.model.element.TypeElement;
31 import javax.lang.model.type.ArrayType;
32 import javax.lang.model.type.DeclaredType;
33 import javax.lang.model.type.PrimitiveType;
34 import javax.lang.model.type.TypeKind;
35 import javax.lang.model.type.TypeMirror;
36 import java.util.ArrayList;
37 import java.util.Collection;
38
39 /**
40 * @author dkohlert
41 */
42 public class TypeMonikerFactory {
43
44 public static TypeMoniker getTypeMoniker(TypeMirror typeMirror) {
45 if (typeMirror == null)
46 throw new NullPointerException();
47
48 if (typeMirror.getKind().isPrimitive())
49 return new PrimitiveTypeMoniker((PrimitiveType) typeMirror);
50 else if (typeMirror.getKind().equals(TypeKind.ARRAY))
51 return new ArrayTypeMoniker((ArrayType) typeMirror);
52 else if (typeMirror.getKind().equals(TypeKind.DECLARED))
53 return new DeclaredTypeMoniker((DeclaredType) typeMirror);
54 return getTypeMoniker(typeMirror.toString());
55 }
56
57 public static TypeMoniker getTypeMoniker(String typeName) {
58 return new StringMoniker(typeName);
59 }
60
61 static class ArrayTypeMoniker implements TypeMoniker {
62 private TypeMoniker arrayType;
63
64 public ArrayTypeMoniker(ArrayType type) {
65 arrayType = TypeMonikerFactory.getTypeMoniker(type.getComponentType());
66 }
67
68 public TypeMirror create(ProcessingEnvironment apEnv) {
69 return apEnv.getTypeUtils().getArrayType(arrayType.create(apEnv));
70 }
71 }
72 static class DeclaredTypeMoniker implements TypeMoniker {
73 private Name typeDeclName;
74 private Collection<TypeMoniker> typeArgs = new ArrayList<TypeMoniker>();
75
76 public DeclaredTypeMoniker(DeclaredType type) {
77 typeDeclName = ((TypeElement) type.asElement()).getQualifiedName();
78 for (TypeMirror arg : type.getTypeArguments())
79 typeArgs.add(TypeMonikerFactory.getTypeMoniker(arg));
80 }
81
82 public TypeMirror create(ProcessingEnvironment apEnv) {
83 TypeElement typeDecl = apEnv.getElementUtils().getTypeElement(typeDeclName);
84 TypeMirror[] tmpArgs = new TypeMirror[typeArgs.size()];
85 int idx = 0;
86 for (TypeMoniker moniker : typeArgs)
87 tmpArgs[idx++] = moniker.create(apEnv);
88
89 return apEnv.getTypeUtils().getDeclaredType(typeDecl, tmpArgs);
90 }
91 }
92 static class PrimitiveTypeMoniker implements TypeMoniker {
93 private TypeKind kind;
94
95 public PrimitiveTypeMoniker(PrimitiveType type) {
96 kind = type.getKind();
97 }
98
99 public TypeMirror create(ProcessingEnvironment apEnv) {
100 return apEnv.getTypeUtils().getPrimitiveType(kind);
101 }
102 }
103 static class StringMoniker implements TypeMoniker {
104 private String typeName;
105
106 public StringMoniker(String typeName) {
107 this.typeName = typeName;
108 }
109
110 public TypeMirror create(ProcessingEnvironment apEnv) {
111 return apEnv.getTypeUtils().getDeclaredType(apEnv.getElementUtils().getTypeElement(typeName));
112 }
113 }
114 }

mercurial