langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
changeset 22163 3651128c74eb
parent 22162 3b3e23e67329
child 22165 ec53c8946fc2
equal deleted inserted replaced
22162:3b3e23e67329 22163:3651128c74eb
   104 
   104 
   105     /** Flag for alternate metafactories indicating the lambda object requires multiple bridges */
   105     /** Flag for alternate metafactories indicating the lambda object requires multiple bridges */
   106     public static final int FLAG_BRIDGES = 1 << 2;
   106     public static final int FLAG_BRIDGES = 1 << 2;
   107 
   107 
   108     // <editor-fold defaultstate="collapsed" desc="Instantiating">
   108     // <editor-fold defaultstate="collapsed" desc="Instantiating">
   109     protected static final Context.Key<LambdaToMethod> unlambdaKey =
   109     protected static final Context.Key<LambdaToMethod> unlambdaKey = new Context.Key<>();
   110             new Context.Key<LambdaToMethod>();
       
   111 
   110 
   112     public static LambdaToMethod instance(Context context) {
   111     public static LambdaToMethod instance(Context context) {
   113         LambdaToMethod instance = context.get(unlambdaKey);
   112         LambdaToMethod instance = context.get(unlambdaKey);
   114         if (instance == null) {
   113         if (instance == null) {
   115             instance = new LambdaToMethod(context);
   114             instance = new LambdaToMethod(context);
   159         private final JCClassDecl clazz;
   158         private final JCClassDecl clazz;
   160 
   159 
   161         private KlassInfo(JCClassDecl clazz) {
   160         private KlassInfo(JCClassDecl clazz) {
   162             this.clazz = clazz;
   161             this.clazz = clazz;
   163             appendedMethodList = new ListBuffer<>();
   162             appendedMethodList = new ListBuffer<>();
   164             deserializeCases = new HashMap<String, ListBuffer<JCStatement>>();
   163             deserializeCases = new HashMap<>();
   165             MethodType type = new MethodType(List.of(syms.serializedLambdaType), syms.objectType,
   164             MethodType type = new MethodType(List.of(syms.serializedLambdaType), syms.objectType,
   166                     List.<Type>nil(), syms.methodClass);
   165                     List.<Type>nil(), syms.methodClass);
   167             deserMethodSym = makePrivateSyntheticMethod(STATIC, names.deserializeLambda, type, clazz.sym);
   166             deserMethodSym = makePrivateSyntheticMethod(STATIC, names.deserializeLambda, type, clazz.sym);
   168             deserParamSym = new VarSymbol(FINAL, names.fromString("lambda"),
   167             deserParamSym = new VarSymbol(FINAL, names.fromString("lambda"),
   169                     syms.serializedLambdaType, deserMethodSym);
   168                     syms.serializedLambdaType, deserMethodSym);
   202 
   201 
   203     public JCTree translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
   202     public JCTree translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
   204         this.make = make;
   203         this.make = make;
   205         this.attrEnv = env;
   204         this.attrEnv = env;
   206         this.context = null;
   205         this.context = null;
   207         this.contextMap = new HashMap<JCTree, TranslationContext<?>>();
   206         this.contextMap = new HashMap<>();
   208         return translate(cdef);
   207         return translate(cdef);
   209     }
   208     }
   210     // </editor-fold>
   209     // </editor-fold>
   211 
   210 
   212     // <editor-fold defaultstate="collapsed" desc="visitor methods">
   211     // <editor-fold defaultstate="collapsed" desc="visitor methods">
   259         MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
   258         MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
   260         MethodType lambdaType = (MethodType) sym.type;
   259         MethodType lambdaType = (MethodType) sym.type;
   261 
   260 
   262         {
   261         {
   263             Symbol owner = localContext.owner;
   262             Symbol owner = localContext.owner;
   264             ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
   263             ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<>();
   265             ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
   264             ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<>();
   266 
   265 
   267             for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
   266             for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
   268                 if (tc.position.onLambda == tree) {
   267                 if (tc.position.onLambda == tree) {
   269                     lambdaTypeAnnos.append(tc);
   268                     lambdaTypeAnnos.append(tc);
   270                 } else {
   269                 } else {
  1145 
  1144 
  1146         /**
  1145         /**
  1147          * maps for fake clinit symbols to be used as owners of lambda occurring in
  1146          * maps for fake clinit symbols to be used as owners of lambda occurring in
  1148          * a static var init context
  1147          * a static var init context
  1149          */
  1148          */
  1150         private Map<ClassSymbol, Symbol> clinits =
  1149         private Map<ClassSymbol, Symbol> clinits = new HashMap<>();
  1151                 new HashMap<ClassSymbol, Symbol>();
       
  1152 
  1150 
  1153         private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) {
  1151         private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) {
  1154             frameStack = List.nil();
  1152             frameStack = List.nil();
  1155             localClassDefs = new HashMap<Symbol, JCClassDecl>();
  1153             localClassDefs = new HashMap<>();
  1156             return translate(tree);
  1154             return translate(tree);
  1157         }
  1155         }
  1158 
  1156 
  1159         @Override
  1157         @Override
  1160         public void visitBlock(JCBlock tree) {
  1158         public void visitBlock(JCBlock tree) {
  1178             Map<ClassSymbol, Symbol> prevClinits = clinits;
  1176             Map<ClassSymbol, Symbol> prevClinits = clinits;
  1179             DiagnosticSource prevSource = log.currentSource();
  1177             DiagnosticSource prevSource = log.currentSource();
  1180             try {
  1178             try {
  1181                 log.useSource(tree.sym.sourcefile);
  1179                 log.useSource(tree.sym.sourcefile);
  1182                 syntheticMethodNameCounts = new SyntheticMethodNameCounter();
  1180                 syntheticMethodNameCounts = new SyntheticMethodNameCounter();
  1183                 prevClinits = new HashMap<ClassSymbol, Symbol>();
  1181                 prevClinits = new HashMap<>();
  1184                 if (tree.sym.owner.kind == MTH) {
  1182                 if (tree.sym.owner.kind == MTH) {
  1185                     localClassDefs.put(tree.sym, tree);
  1183                     localClassDefs.put(tree.sym, tree);
  1186                 }
  1184                 }
  1187                 if (directlyEnclosingLambda() != null) {
  1185                 if (directlyEnclosingLambda() != null) {
  1188                     tree.sym.owner = owner();
  1186                     tree.sym.owner = owner();
  1350                 Type classType = consSym.owner.type;
  1348                 Type classType = consSym.owner.type;
  1351 
  1349 
  1352                 // Build lambda parameters
  1350                 // Build lambda parameters
  1353                 // partially cloned from TreeMaker.Params until 8014021 is fixed
  1351                 // partially cloned from TreeMaker.Params until 8014021 is fixed
  1354                 Symbol owner = owner();
  1352                 Symbol owner = owner();
  1355                 ListBuffer<JCVariableDecl> paramBuff = new ListBuffer<JCVariableDecl>();
  1353                 ListBuffer<JCVariableDecl> paramBuff = new ListBuffer<>();
  1356                 int i = 0;
  1354                 int i = 0;
  1357                 for (List<Type> l = ptypes; l.nonEmpty(); l = l.tail) {
  1355                 for (List<Type> l = ptypes; l.nonEmpty(); l = l.tail) {
  1358                     JCVariableDecl param = make.Param(make.paramName(i++), l.head, owner);
  1356                     JCVariableDecl param = make.Param(make.paramName(i++), l.head, owner);
  1359                     param.sym.pos = tree.pos;
  1357                     param.sym.pos = tree.pos;
  1360                     paramBuff.append(param);
  1358                     paramBuff.append(param);
  1771                 if (dumpLambdaToMethodStats) {
  1769                 if (dumpLambdaToMethodStats) {
  1772                     log.note(tree, "lambda.stat", needsAltMetafactory(), translatedSym);
  1770                     log.note(tree, "lambda.stat", needsAltMetafactory(), translatedSym);
  1773                 }
  1771                 }
  1774                 translatedSymbols = new EnumMap<>(LambdaSymbolKind.class);
  1772                 translatedSymbols = new EnumMap<>(LambdaSymbolKind.class);
  1775 
  1773 
  1776                 translatedSymbols.put(PARAM, new LinkedHashMap<Symbol, Symbol>());
  1774                 translatedSymbols.put(PARAM, new LinkedHashMap<>());
  1777                 translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>());
  1775                 translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<>());
  1778                 translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>());
  1776                 translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<>());
  1779                 translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>());
  1777                 translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<>());
  1780                 translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>());
  1778                 translatedSymbols.put(TYPE_VAR, new LinkedHashMap<>());
  1781             }
  1779             }
  1782 
  1780 
  1783              /**
  1781              /**
  1784              * For a serializable lambda, generate a disambiguating string
  1782              * For a serializable lambda, generate a disambiguating string
  1785              * which maximizes stability across deserialization.
  1783              * which maximizes stability across deserialization.
  2166     enum LambdaSymbolKind {
  2164     enum LambdaSymbolKind {
  2167         PARAM,          // original to translated lambda parameters
  2165         PARAM,          // original to translated lambda parameters
  2168         LOCAL_VAR,      // original to translated lambda locals
  2166         LOCAL_VAR,      // original to translated lambda locals
  2169         CAPTURED_VAR,   // variables in enclosing scope to translated synthetic parameters
  2167         CAPTURED_VAR,   // variables in enclosing scope to translated synthetic parameters
  2170         CAPTURED_THIS,  // class symbols to translated synthetic parameters (for captured member access)
  2168         CAPTURED_THIS,  // class symbols to translated synthetic parameters (for captured member access)
  2171         TYPE_VAR;       // original to translated lambda type variables
  2169         TYPE_VAR       // original to translated lambda type variables
  2172     }
  2170     }
  2173 
  2171 
  2174     /**
  2172     /**
  2175      * ****************************************************************
  2173      * ****************************************************************
  2176      * Signature Generation
  2174      * Signature Generation