Source Code Cross Referenced for JavacTypes.java in  » 6.0-JDK-Modules-com.sun » tools » com » sun » tools » javac » model » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun » tools » com.sun.tools.javac.model 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package com.sun.tools.javac.model;
027:
028:        import java.util.List;
029:        import java.util.Set;
030:        import java.util.EnumSet;
031:        import javax.lang.model.element.*;
032:        import javax.lang.model.type.*;
033:        import com.sun.tools.javac.code.*;
034:        import com.sun.tools.javac.code.Symbol.*;
035:        import com.sun.tools.javac.util.*;
036:
037:        /**
038:         * Utility methods for operating on types.
039:         *
040:         * <p><b>This is NOT part of any API supported by Sun Microsystems.
041:         * If you write code that depends on this, you do so at your own
042:         * risk.  This code and its internal interfaces are subject to change
043:         * or deletion without notice.</b></p>
044:         */
045:        @Version("@(#)JavacTypes.java	1.12 07/05/05")
046:        public class JavacTypes implements  javax.lang.model.util.Types {
047:
048:            private Symtab syms;
049:            private Types types;
050:
051:            private static final Context.Key<JavacTypes> KEY = new Context.Key<JavacTypes>();
052:
053:            public static JavacTypes instance(Context context) {
054:                JavacTypes instance = context.get(KEY);
055:                if (instance == null) {
056:                    instance = new JavacTypes(context);
057:                    context.put(KEY, instance);
058:                }
059:                return instance;
060:            }
061:
062:            /**
063:             * Public for use only by JavacProcessingEnvironment
064:             */
065:            // TODO JavacTypes constructor should be protected
066:            public JavacTypes(Context context) {
067:                setContext(context);
068:            }
069:
070:            /**
071:             * Use a new context.  May be called from outside to update
072:             * internal state for a new annotation-processing round.
073:             * This instance is *not* then registered with the new context.
074:             */
075:            public void setContext(Context context) {
076:                syms = Symtab.instance(context);
077:                types = Types.instance(context);
078:            }
079:
080:            public Element asElement(TypeMirror t) {
081:                Type type = cast(Type.class, t);
082:                if (type.tag != TypeTags.CLASS && type.tag != TypeTags.TYPEVAR)
083:                    return null;
084:                return type.asElement();
085:            }
086:
087:            public boolean isSameType(TypeMirror t1, TypeMirror t2) {
088:                return types.isSameType((Type) t1, (Type) t2);
089:            }
090:
091:            public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
092:                validateTypeNotIn(t1, EXEC_OR_PKG);
093:                validateTypeNotIn(t2, EXEC_OR_PKG);
094:                return types.isSubtype((Type) t1, (Type) t2);
095:            }
096:
097:            public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
098:                validateTypeNotIn(t1, EXEC_OR_PKG);
099:                validateTypeNotIn(t2, EXEC_OR_PKG);
100:                return types.isAssignable((Type) t1, (Type) t2);
101:            }
102:
103:            public boolean contains(TypeMirror t1, TypeMirror t2) {
104:                validateTypeNotIn(t1, EXEC_OR_PKG);
105:                validateTypeNotIn(t2, EXEC_OR_PKG);
106:                return ((Type) t1).contains((Type) t2);
107:            }
108:
109:            public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
110:                return types.isSubSignature((Type) m1, (Type) m2);
111:            }
112:
113:            public List<Type> directSupertypes(TypeMirror t) {
114:                validateTypeNotIn(t, EXEC_OR_PKG);
115:                Type type = (Type) t;
116:                Type sup = types.super type(type);
117:                return (sup == Type.noType || sup == type || sup == null) ? types
118:                        .interfaces(type)
119:                        : types.interfaces(type).prepend(sup);
120:            }
121:
122:            public TypeMirror erasure(TypeMirror t) {
123:                if (t.getKind() == TypeKind.PACKAGE)
124:                    throw new IllegalArgumentException(t.toString());
125:                return types.erasure((Type) t);
126:            }
127:
128:            public TypeElement boxedClass(PrimitiveType p) {
129:                return types.boxedClass((Type) p);
130:            }
131:
132:            public PrimitiveType unboxedType(TypeMirror t) {
133:                if (t.getKind() != TypeKind.DECLARED)
134:                    throw new IllegalArgumentException(t.toString());
135:                Type unboxed = types.unboxedType((Type) t);
136:                if (!unboxed.isPrimitive()) // only true primitives, not void
137:                    throw new IllegalArgumentException(t.toString());
138:                return unboxed;
139:            }
140:
141:            public TypeMirror capture(TypeMirror t) {
142:                validateTypeNotIn(t, EXEC_OR_PKG);
143:                return types.capture((Type) t);
144:            }
145:
146:            public PrimitiveType getPrimitiveType(TypeKind kind) {
147:                switch (kind) {
148:                case BOOLEAN:
149:                    return syms.booleanType;
150:                case BYTE:
151:                    return syms.byteType;
152:                case SHORT:
153:                    return syms.shortType;
154:                case INT:
155:                    return syms.intType;
156:                case LONG:
157:                    return syms.longType;
158:                case CHAR:
159:                    return syms.charType;
160:                case FLOAT:
161:                    return syms.floatType;
162:                case DOUBLE:
163:                    return syms.doubleType;
164:                default:
165:                    throw new IllegalArgumentException("Not a primitive type: "
166:                            + kind);
167:                }
168:            }
169:
170:            public NullType getNullType() {
171:                return (NullType) syms.botType;
172:            }
173:
174:            public NoType getNoType(TypeKind kind) {
175:                switch (kind) {
176:                case VOID:
177:                    return syms.voidType;
178:                case NONE:
179:                    return Type.noType;
180:                default:
181:                    throw new IllegalArgumentException(kind.toString());
182:                }
183:            }
184:
185:            public ArrayType getArrayType(TypeMirror componentType) {
186:                switch (componentType.getKind()) {
187:                case VOID:
188:                case EXECUTABLE:
189:                case WILDCARD: // heh!
190:                case PACKAGE:
191:                    throw new IllegalArgumentException(componentType.toString());
192:                }
193:                return new Type.ArrayType((Type) componentType, syms.arrayClass);
194:            }
195:
196:            public WildcardType getWildcardType(TypeMirror extendsBound,
197:                    TypeMirror super Bound) {
198:                BoundKind bkind;
199:                Type bound;
200:                if (extendsBound == null && super Bound == null) {
201:                    bkind = BoundKind.UNBOUND;
202:                    bound = syms.objectType;
203:                } else if (super Bound == null) {
204:                    bkind = BoundKind.EXTENDS;
205:                    bound = (Type) extendsBound;
206:                } else if (extendsBound == null) {
207:                    bkind = BoundKind.SUPER;
208:                    bound = (Type) super Bound;
209:                } else {
210:                    throw new IllegalArgumentException(
211:                            "Extends and super bounds cannot both be provided");
212:                }
213:                switch (bound.getKind()) {
214:                case ARRAY:
215:                case DECLARED:
216:                case ERROR:
217:                case TYPEVAR:
218:                    return new Type.WildcardType(bound, bkind, syms.boundClass);
219:                default:
220:                    throw new IllegalArgumentException(bound.toString());
221:                }
222:            }
223:
224:            public DeclaredType getDeclaredType(TypeElement typeElem,
225:                    TypeMirror... typeArgs) {
226:                ClassSymbol sym = (ClassSymbol) typeElem;
227:
228:                if (typeArgs.length == 0)
229:                    return (DeclaredType) sym.erasure(types);
230:                if (sym.type.getEnclosingType().isParameterized())
231:                    throw new IllegalArgumentException(sym.toString());
232:
233:                return getDeclaredType0(sym.type.getEnclosingType(), sym,
234:                        typeArgs);
235:            }
236:
237:            public DeclaredType getDeclaredType(DeclaredType enclosing,
238:                    TypeElement typeElem, TypeMirror... typeArgs) {
239:                if (enclosing == null)
240:                    return getDeclaredType(typeElem, typeArgs);
241:
242:                ClassSymbol sym = (ClassSymbol) typeElem;
243:                Type outer = (Type) enclosing;
244:
245:                if (outer.tsym != sym.owner.enclClass())
246:                    throw new IllegalArgumentException(enclosing.toString());
247:                if (!outer.isParameterized())
248:                    return getDeclaredType(typeElem, typeArgs);
249:
250:                return getDeclaredType0(outer, sym, typeArgs);
251:            }
252:
253:            // where
254:            private DeclaredType getDeclaredType0(Type outer, ClassSymbol sym,
255:                    TypeMirror... typeArgs) {
256:                if (typeArgs.length != sym.type.getTypeArguments().length())
257:                    throw new IllegalArgumentException(
258:                            "Incorrect number of type arguments");
259:
260:                ListBuffer<Type> targs = new ListBuffer<Type>();
261:                for (TypeMirror t : typeArgs) {
262:                    if (!(t instanceof  ReferenceType || t instanceof  WildcardType))
263:                        throw new IllegalArgumentException(t.toString());
264:                    targs.append((Type) t);
265:                }
266:                // TODO: Would like a way to check that type args match formals.
267:
268:                return (DeclaredType) new Type.ClassType(outer, targs.toList(),
269:                        sym);
270:            }
271:
272:            /**
273:             * Returns the type of an element when that element is viewed as
274:             * a member of, or otherwise directly contained by, a given type.
275:             * For example,
276:             * when viewed as a member of the parameterized type {@code Set<String>},
277:             * the {@code Set.add} method is an {@code ExecutableType}
278:             * whose parameter is of type {@code String}.
279:             *
280:             * @param containing  the containing type
281:             * @param element     the element
282:             * @return the type of the element as viewed from the containing type
283:             * @throws IllegalArgumentException if the element is not a valid one
284:             *		for the given type
285:             */
286:            public TypeMirror asMemberOf(DeclaredType containing,
287:                    Element element) {
288:                Type site = (Type) containing;
289:                Symbol sym = (Symbol) element;
290:                if (types.asSuper(site, sym.getEnclosingElement()) == null)
291:                    throw new IllegalArgumentException(sym + "@" + site);
292:                return types.memberType(site, sym);
293:            }
294:
295:            private static final Set<TypeKind> EXEC_OR_PKG = EnumSet.of(
296:                    TypeKind.EXECUTABLE, TypeKind.PACKAGE);
297:
298:            /**
299:             * Throws an IllegalArgumentException if a type's kind is one of a set.
300:             */
301:            private void validateTypeNotIn(TypeMirror t,
302:                    Set<TypeKind> invalidKinds) {
303:                if (invalidKinds.contains(t.getKind()))
304:                    throw new IllegalArgumentException(t.toString());
305:            }
306:
307:            /**
308:             * Returns an object cast to the specified type.
309:             * @throws NullPointerException if the object is {@code null}
310:             * @throws IllegalArgumentException if the object is of the wrong type
311:             */
312:            private static <T> T cast(Class<T> clazz, Object o) {
313:                if (!clazz.isInstance(o))
314:                    throw new IllegalArgumentException(o.toString());
315:                return clazz.cast(o);
316:            }
317:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.