1 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.*; 30 31 import java.util.*; 32 33 39 public class AnonymousMethodExpr extends Expr { 40 protected Type discoverType() { return methodDec.getResultType(); } 41 42 public void grabContext() { 43 ExprMaker em = new ExprMaker(getCompiler()); 44 em.process(methodDec); 45 Map inParams = em.getInParams(); 46 47 Exprs args = getArgs(); 48 Formals formals = methodDec.getFormals(); 49 50 for (Iterator i = inParams.entrySet().iterator(); i.hasNext(); ) { 52 Map.Entry entry = (Map.Entry)i.next(); 53 VarDec varDec = (VarDec)entry.getKey(); 54 varDec.getModifiers().setFinal(true); args.add(getAST().makeVar(varDec)); 56 57 FormalDec formalDec = (FormalDec)entry.getValue(); 58 formals.add(formalDec); 59 } 60 } 61 62 public ASTObject postMove(MovingWalker walker) { 63 66 grabContext(); 68 return this; 69 } 70 71 78 public void fixAST(ASTFixerPass fixer) { 79 args = (Exprs)fixer.process(args); 82 } 83 84 public ASTObject postFixAST(final ASTFixerPass fixer) { 86 if (inStaticContext()) { 87 methodDec.getModifiers().setStatic(true); 88 } else { 89 methodDec.getModifiers().setStatic(false); 90 } 91 92 TypeDec inTypeDec = getBytecodeTypeDec(); 93 if (methodDec.isStatic() && inTypeDec.isInner()) { 94 inTypeDec = inTypeDec.getOutermostTypeDec(); 95 } 96 97 Decs body = inTypeDec.getBody(); 98 methodDec.getModifiers().setPrivate(true); 99 body.add(getMethodDec()); 101 getMethodDec().setAllEnclosingTypes(inTypeDec.getType()); 102 103 Expr thisExpr; 104 if (methodDec.isStatic()) { 105 thisExpr = getAST().makeTypeExpr(inTypeDec.getType()); 106 } else { 107 thisExpr = getAST().makeThis(inTypeDec.getType()); 108 } 109 110 return getAST().makeCall(getMethodDec(), thisExpr, args); 111 } 112 113 public void implementMixin(MixinImplementationPass fixer) { 115 return; 116 } 117 118 119 public void unparse(CodeWriter writer) { 120 writer.write("DecInExpr["); 121 writer.write(args); 122 writer.write(" -> "); 123 writer.write(methodDec); 124 writer.write("]"); 125 } 126 127 protected Exprs args; 129 public Exprs getArgs() { return args; } 130 public void setArgs(Exprs _args) { 131 if (_args != null) _args.setParent(this); 132 args = _args; 133 } 134 135 protected MethodDec methodDec; 136 public MethodDec getMethodDec() { return methodDec; } 137 public void setMethodDec(MethodDec _methodDec) { 138 if (_methodDec != null) _methodDec.setParent(this); 139 methodDec = _methodDec; 140 } 141 142 public AnonymousMethodExpr(SourceLocation location, Exprs _args, MethodDec _methodDec) { 143 super(location); 144 setArgs(_args); 145 setMethodDec(_methodDec); 146 } 147 protected AnonymousMethodExpr(SourceLocation source) { 148 super(source); 149 } 150 151 public ASTObject copyWalk(CopyWalker walker) { 152 AnonymousMethodExpr ret = new AnonymousMethodExpr(getSourceLocation()); 153 ret.preCopy(walker, this); 154 if (args != null) ret.setArgs( (Exprs)walker.process(args) ); 155 if (methodDec != null) ret.setMethodDec( (MethodDec)walker.process(methodDec) ); 156 return ret; 157 } 158 159 public ASTObject getChildAt(int childIndex) { 160 switch(childIndex) { 161 case 0: return args; 162 case 1: return methodDec; 163 default: return super.getChildAt(childIndex); 164 } 165 } 166 public String getChildNameAt(int childIndex) { 167 switch(childIndex) { 168 case 0: return "args"; 169 case 1: return "methodDec"; 170 default: return super.getChildNameAt(childIndex); 171 } 172 } 173 public void setChildAt(int childIndex, ASTObject child) { 174 switch(childIndex) { 175 case 0: setArgs((Exprs)child); return; 176 case 1: setMethodDec((MethodDec)child); return; 177 default: super.setChildAt(childIndex, child); return; 178 } 179 } 180 public int getChildCount() { 181 return 2; 182 } 183 184 public String getDefaultDisplayName() { 185 return "AnonymousMethodExpr()"; 186 } 187 188 } 190 | Popular Tags |