1 46 package org.codehaus.groovy.classgen; 47 48 import org.codehaus.groovy.ast.ClassNode; 49 import org.codehaus.groovy.ast.CompileUnit; 50 import org.codehaus.groovy.ast.MethodNode; 51 52 53 59 public class GeneratorContext { 60 61 private int innerClassIdx = 1; 62 private CompileUnit compileUnit; 63 64 public GeneratorContext(CompileUnit compileUnit) { 65 this.compileUnit = compileUnit; 66 } 67 68 public int getNextInnerClassIdx() { 69 return innerClassIdx++; 70 } 71 72 public CompileUnit getCompileUnit() { 73 return compileUnit; 74 } 75 76 public String getNextClosureInnerName(ClassNode owner, ClassNode enclosingClass, MethodNode enclosingMethod) { 77 String ownerShortName = owner.getNameWithoutPackage(); 78 String classShortName = enclosingClass.getNameWithoutPackage(); 79 if (classShortName.equals(ownerShortName)) { 80 classShortName = ""; 81 } 82 else { 83 classShortName += "_"; 84 } 85 int dp = classShortName.lastIndexOf("$"); 87 if (dp >= 0) { 88 classShortName = classShortName.substring(++dp); 89 } 90 if (classShortName.startsWith("_")) { 92 classShortName = classShortName.substring(1); 93 } 94 String methodName = ""; 95 if (enclosingMethod != null) { 96 methodName = enclosingMethod.getName() + "_"; 97 98 if (enclosingClass.isDerivedFrom("groovy.lang.Closure")) { 99 methodName = ""; 100 } 101 methodName = methodName.replace('<', '_'); 102 methodName = methodName.replace('>', '_'); 103 } 104 return "_" + classShortName + methodName + "closure" + getNextInnerClassIdx(); 105 } 106 } 107 | Popular Tags |