KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > PerSingleton


1 /* -*- Mode: Java; tab-width: 4; 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.crosscuts.ast;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.*;
29 import org.aspectj.compiler.crosscuts.joinpoints.*;
30
31
32 import java.util.*;
33
34 import org.aspectj.util.*;
35
36 /**
37  * @grammar eachJVM()
38  */

39
40 public class PerSingleton extends PerClause {
41     FieldDec aspectField;
42     
43     public String JavaDoc toShortString() { return "issingleton"; }
44     
45     public JpPlanner makeInnerPlanner(PlanData planData) {
46         return new JpPlanner() {
47             public FuzzyBoolean fastMatch(JoinPoint jp) {
48                 return FuzzyBoolean.MAYBE;
49             }
50             public JpPlan makePlan(JoinPoint jp) {
51                 JpPlan plan = new JpPlan(jp);
52                 plan.test = maybeMakeTest(jp);
53                 plan.setInstanceExpr(makeAspectInstance());
54                 return plan;
55             }
56         };
57     }
58     
59     public JpPlanner makeInitializerPlanner(PlanData planData) {
60         // no initializers here
61
return JpPlanner.NO_PLANNER;
62     }
63
64     String JavaDoc makeAspectFieldName() {
65         return getAST().makeGeneratedName("aspectInstance");
66     }
67
68     FieldDec makeAspectField() {
69         final AST ast = getAST();
70         FieldDec ret = ast.makeField(
71             ast.makeModifiers(Modifiers.PUBLIC | Modifiers.FINAL | Modifiers.STATIC),
72             getAspectType(), makeAspectFieldName(), ast.makeNew(getAspectType()));
73         //ret.setSynthetic();
74
return ret;
75     }
76
77     //XXX this only works most of the time
78
Expr maybeMakeTest(JoinPoint point) {
79         final AST ast = getAST();
80         
81         if (getAspectType().isSubtypeOf(point.getBytecodeType())) {
82             return ast.makeCall(hasAspectMethod, ast.makeTypeExpr(getAspectType()));
83         } else {
84             return null;
85         }
86     }
87
88     protected MethodDec makeAspectOfMethod() {
89         /* public static A aspectOf() {
90                return aspect$;
91            }
92         */

93         final AST ast = getAST();
94         BlockStmt body = ast.makeBlock(ast.makeReturn(makeAspectInstance()));
95         return makeAspectOfMethod(ast.makeFormals(), body);
96     }
97
98
99     protected MethodDec makeHasAspectMethod() {
100         /* public static A hasAspect() {
101                return aspect$ != null;
102            }
103         */

104         final AST ast = getAST();
105         BlockStmt body = ast.makeBlock(ast.makeReturn(
106             ast.makeBinop("!=", makeAspectInstance(), ast.makeNull())));
107         return makeHasAspectMethod(ast.makeFormals(), body);
108     }
109
110
111     Expr makeAspectInstance() {
112         return getAST().makeGet(aspectField);
113     }
114
115     public Expr makeAspectOfExpr(Expr fromObject) { /* A.aspectOf() */
116         return getAST().makeCall(aspectOfMethod, getAST().makeTypeExpr(getAspectType()));
117     }
118
119
120     public void setupAspect() {
121         TypeDec typeDec = getAspectDec();
122         aspectField = makeAspectField();
123         typeDec.addToBody(aspectField);
124         super.setupAspect();
125     }
126     
127     //BEGIN: Generated from @child and @property
128

129     public PerSingleton(SourceLocation location) {
130         super(location);
131     
132     }
133     
134     public ASTObject copyWalk(CopyWalker walker) {
135         PerSingleton ret = new PerSingleton(getSourceLocation());
136         ret.preCopy(walker, this);
137     
138         return ret;
139     }
140     
141     
142     public String JavaDoc getDefaultDisplayName() {
143         return "PerSingleton()";
144     }
145     
146     //END: Generated from @child and @property
147
}
148
149
Popular Tags