KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ClassExprPass


1 /* -*- Mode: Java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler 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  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import java.util.*;
28 import org.aspectj.compiler.base.ast.*;
29
30 public class ClassExprPass extends WalkerPass {
31     public ClassExprPass(JavaCompiler jc) { super(jc); }
32     public String JavaDoc getDisplayName() { return "desugaring T.class exprs"; }
33
34     TypeDec dec = null;
35     HashMap/*<FieldDec>*/ fields = null;
36     MethodDec method = null;
37     TypeDec holder = null; // used by interfaces
38

39     public ASTObject process(ASTObject object) {
40     if (object instanceof TypeDec) {
41         TypeDec savedDec = this.dec;
42         HashMap savedFields = this.fields;
43         MethodDec savedMethod = this.method;
44         TypeDec savedHolder = this.holder;
45         dec = (TypeDec) object;
46         fields = new HashMap();
47         if (object instanceof ClassDec) {
48         holder = dec;
49         }
50         object.walk(this);
51         this.dec = savedDec;
52         this.fields = savedFields;
53         this.method = savedMethod;
54         this.holder = savedHolder;
55         return object;
56     }
57     if (object instanceof ClassExpr) {
58         object = handleClassExpr((ClassExpr) object);
59     }
60         object.walk(this);
61         return object;
62     }
63
64     ASTObject handleClassExpr(ClassExpr object) {
65         final AST ast = getAST();
66     Type ty = object.getTypeD().getType();
67         if (ty instanceof PrimitiveType) return ty.getClassExpr();
68     FieldDec field = genField(ty);
69     MethodDec maker = genMethod();
70     return ast.makeTriTest(ast.makeBinop("==",
71                                              ast.makeGet(field),
72                          ast.makeNull()),
73                    ast.makeSet(field,
74                        ast.makeStaticCall(method,
75                                   ast.makeString(ty.getExternalName()))),
76                    ast.makeGet(field));
77     }
78
79     FieldDec genField(Type ty) {
80         final AST ast = getAST();
81         final TypeManager tm = getTypeManager();
82     FieldDec field = (FieldDec) fields.get(ty);
83     if (field == null) {
84         if (holder == null) genHolder();
85         field = ast.makeField(ast.makeModifiers(Modifiers.STATIC),
86                   tm.getClassType(),
87                                   genFieldName(ty));
88         holder.getBody().add(field);
89         fields.put(ty, field);
90     }
91     return field;
92     }
93
94     String JavaDoc genFieldName(Type ty) {
95         String JavaDoc str = ty.getExternalName();
96         if (ty instanceof ArrayType) {
97             str = "array" + str;
98         } else {
99             str = "class$" + str;
100         }
101         str = str.replace('[','$').replace('.','$');
102         if (str.charAt(str.length() - 1) == ';') {
103             str = str.substring(0, str.length() - 1);
104         }
105         return str;
106     }
107
108     MethodDec genMethod() {
109         final AST ast = getAST();
110         final TypeManager tm = getTypeManager();
111     if (method == null) {
112         if (holder == null) genHolder();
113             FormalDec formal = ast.makeFormal(tm.getStringType(), "name$");
114             FormalDec exnFormal = ast.makeFormal(tm.getType("java.lang", "ClassNotFoundException"),
115                                                  "exn$");
116             method = ast.makeMethod(
117                          ast.makeModifiers(Modifiers.STATIC),
118                          tm.getClassType(),
119                          "class$",
120                          ast.makeFormals(formal),
121                          ast.makeCodeBody(
122                              ast.makeTryCatch(
123                                  ast.makeBlock(
124                                      ast.makeReturn(
125                                          ast.makeStaticCall(tm.getClassType(),
126                                                             "forName",
127                                                             ast.makeExprs(ast.makeVar(formal))))),
128                                  exnFormal,
129                                  ast.makeBlock(
130                                      ast.makeThrow(
131                                          ast.makeNew(
132                                              tm.getType("java.lang", "NoClassDefFoundError"),
133                                              ast.makeCall(ast.makeVar(exnFormal),
134                                                           "getMessage",
135                                                           ast.makeExprs())))))));
136             holder.getBody().add(method);
137     }
138     return method;
139     }
140
141     void genHolder() {
142         final AST ast = getAST();
143         final TypeManager tm = getTypeManager();
144         
145         holder = ast.makeClass(ast.makeModifiers(Modifiers.STATIC | Modifiers.PRIVATE),
146                                "00",
147                                tm.getObjectType().makeTypeD(),
148                                ast.makeTypeDs());
149         holder.setIsInner(false);
150         dec.addMemberTypeDec(holder);
151     }
152 }
153
Popular Tags