KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > TypeImpl


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.tools.ajdoc;
23
24 import org.aspectj.compiler.base.ast.ArrayType;
25 import org.aspectj.compiler.base.ast.NameType;
26 import org.aspectj.compiler.base.ast.PrimitiveType;
27 import org.aspectj.compiler.base.ast.Type;
28
29 import com.sun.javadoc.ClassDoc;
30
31 public class TypeImpl { //implements org.aspectj.ajdoc.Type {
32

33     public static com.sun.javadoc.Type getInstance(Type type) {
34         return factory.getInstance(type);
35     }
36
37     public static com.sun.javadoc.Type getInstance(String JavaDoc spec,
38                                                    ClassDoc where) {
39         return factory.getInstance(spec, where);
40     }
41
42     private final static class Primitive implements org.aspectj.ajdoc.Type {
43
44
45
46         public final static Primitive getInstance(PrimitiveType type) {
47             return getInstance(type.getName());
48         }
49         
50         public final static Primitive getInstance(String JavaDoc name) {
51             if ("void".equals(name)) return voidType;
52             if ("boolean".equals(name)) return booleanType;
53             if ("byte".equals(name)) return byteType;
54             if ("char".equals(name)) return charType;
55             if ("short".equals(name)) return shortType;
56             if ("int".equals(name)) return intType;
57             if ("long".equals(name)) return longType;
58             if ("float".equals(name)) return floatType;
59             if ("double".equals(name)) return doubleType;
60             return null;
61         }
62
63         public final static boolean isPrimitive(String JavaDoc name) {
64             return name.equals("boolean")
65                 || name.equals("byte")
66                 || name.equals("char")
67                 || name.equals("short")
68                 || name.equals("int")
69                 || name.equals("long")
70                 || name.equals("float")
71                 || name.equals("double");
72         }
73         
74         private final String JavaDoc name;
75         private Primitive(String JavaDoc name) { this.name = name; }
76         
77         public String JavaDoc toString() { return name; }
78         public String JavaDoc typeName() { return name; }
79         public String JavaDoc qualifiedTypeName() { return name; }
80         public String JavaDoc dimension() { return ""; }
81         public ClassDoc asClassDoc() { return null; }
82         
83         private final static Primitive voidType = new Primitive("void");
84         private final static Primitive booleanType = new Primitive("boolean");
85         private final static Primitive byteType = new Primitive("byte");
86         private final static Primitive charType = new Primitive("char");
87         private final static Primitive shortType = new Primitive("short");
88         private final static Primitive intType = new Primitive("int");
89         private final static Primitive longType = new Primitive("long");
90         private final static Primitive floatType = new Primitive("float");
91         private final static Primitive doubleType = new Primitive("double");
92     }
93
94     private final static class Array implements org.aspectj.ajdoc.Type {
95         protected final com.sun.javadoc.Type type;
96         protected final int dimension;
97         private Array(com.sun.javadoc.Type type, int dimension) {
98             this.type = type;
99             //TODO: tune this later
100
this.dimension = dimension;
101         }
102
103         public String JavaDoc toString() { return type.toString(); }
104         public String JavaDoc typeName() { return type.typeName(); }
105         public String JavaDoc qualifiedTypeName() { return type.qualifiedTypeName(); }
106         public ClassDoc asClassDoc() { return type.asClassDoc(); }
107         public String JavaDoc dimension() {
108             String JavaDoc str = "";
109             for (int i = 0; i < dimension; i++) str += "[]";
110             return str;
111         }
112
113         public boolean equals(Object JavaDoc other) {
114             if (!(other instanceof Array)) {
115                 return super.equals(other);
116             }
117             Array array = (Array)other;
118             return array.type.equals(type)
119                 && array.dimension == dimension;
120         }
121     }
122         
123
124     private static final Factory factory = new Factory();
125     private final static class Factory {
126
127         private com.sun.javadoc.Type getInstance(Type type) {
128             if (type instanceof PrimitiveType) {
129                 return Primitive.getInstance((PrimitiveType)type);
130             } else if (type instanceof ArrayType) {
131                 ArrayType arrayType = (ArrayType)type;
132                 Type component = arrayType.getComponentType();
133                 while (component instanceof ArrayType) {
134                     component = ((ArrayType)component).getComponentType();
135                 }
136                 return new Array(getInstance(component),
137                                  arrayType.getArrayDimCount());
138             } else {
139                 return ClassDocImpl.getInstance(((NameType)type).getTypeDec());
140             }
141         }
142
143         private com.sun.javadoc.Type getInstance(String JavaDoc spec,
144                                                  ClassDoc where) {
145             int ibracket = spec.indexOf('[');
146             String JavaDoc name;
147             int dimension;
148             if (ibracket != -1) {
149                 name = spec.substring(0, ibracket);
150                 dimension = spec.substring(ibracket+1).length()/2;
151             } else {
152                 name = spec;
153                 dimension = 0;
154             }
155             com.sun.javadoc.Type type = Primitive.getInstance(name);
156             if (type == null) {
157                 type = where.findClass(name); //TODO
158
}
159             if (dimension > 0) {
160                 type = new Array(type, dimension);
161             }
162             return type;
163         }
164     }
165 }
166
Popular Tags