KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > PrivilegeFixer


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;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.JavaCompiler;
29 import org.aspectj.compiler.base.AbstractCompilerPass;
30 import org.aspectj.compiler.base.WorldPass;
31 import org.aspectj.compiler.base.CompilationUnitPass;
32
33 import java.util.*;
34
35 import org.aspectj.compiler.crosscuts.ast.AspectDec;
36
37 /** apply is always true here */
38
39 public class PrivilegeFixer extends AbstractCompilerPass {
40     private boolean apply;
41     
42     /** apply is used for separate compilation. You need to make the access
43      * methods for everybody before you do any bytecode, but you don't need to
44      * change the call sites until you need to.
45      */

46     
47     public PrivilegeFixer(JavaCompiler compiler, boolean apply) {
48         super(compiler);
49         this.apply = apply;
50     }
51
52     public String JavaDoc getDisplayName() {
53         return (apply ? "implement":"plan") + " backdoors";
54     }
55
56     public void transform(TypeDec td) {
57         //XXX make this much less efficient in return for being simpler
58
//XXXcheckTypeDecs(cu,apply);
59
//System.out.println("BEFORE: " + td.unparse());
60
AccessFixer fixer = new AccessFixer(getCompiler(), td, apply);
61         fixer.fixAccesses(td);
62         //System.out.println("AFTER: " + td.unparse());
63
}
64
65     /** Scans compilation unit for privilaged aspects, and
66      * creates backdoor methods if apply==false, or
67      * replaces method calls/field access with backdoor
68      * methods, if apply == true
69      */

70     private void checkTypeDecs(CompilationUnit cu, boolean apply) {
71         List typeDecs = cu.getDefinedTypes();
72         final int N = typeDecs.size();
73
74         for (int j = 0; j < N; j++) {
75             Dec dec = (Dec)typeDecs.get(j);
76             if (dec instanceof TypeDec) {
77                 TypeDec typeDec = (TypeDec)dec;
78                 if (typeDec instanceof AspectDec &&
79                     typeDec.getModifiers().isPrivileged())
80                 {
81                     //typeDec.bindNames();
82
AccessFixer fixer = new AccessFixer(getCompiler(), typeDec, apply);
83                     fixer.fixAccesses(typeDec);
84                 }
85                 
86                 fixIntroducedDecs( typeDec, apply );
87             }
88         }
89     }
90     
91     private void fixIntroducedDecs(TypeDec typeDec, boolean apply) {
92         //System.out.println("fixing: " + typeDec);
93

94         Decs decs = typeDec.getBody();
95         
96         for(int i=0; i < decs.size(); i++) {
97             Dec dec = (Dec)decs.get(i);
98             //System.out.println(" " + dec + " intro " + dec.getIntroducedFromType());
99
if (dec.isIntroduced()) {
100                 new AccessFixer(getCompiler(), typeDec, apply).fixAccesses(dec);
101             }
102         }
103     }
104 }
105
Popular Tags