src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 package com.sun.tools.javac.comp;
aoqi@0 26
aoqi@0 27 import com.sun.tools.javac.tree.*;
aoqi@0 28 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 29 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
aoqi@0 30 import com.sun.tools.javac.tree.TreeMaker;
aoqi@0 31 import com.sun.tools.javac.tree.TreeTranslator;
aoqi@0 32 import com.sun.tools.javac.code.Attribute;
aoqi@0 33 import com.sun.tools.javac.code.Kinds;
aoqi@0 34 import com.sun.tools.javac.code.Scope;
aoqi@0 35 import com.sun.tools.javac.code.Symbol;
aoqi@0 36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
aoqi@0 37 import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol;
aoqi@0 38 import com.sun.tools.javac.code.Symbol.MethodSymbol;
aoqi@0 39 import com.sun.tools.javac.code.Symbol.TypeSymbol;
aoqi@0 40 import com.sun.tools.javac.code.Symbol.VarSymbol;
aoqi@0 41 import com.sun.tools.javac.code.Symtab;
aoqi@0 42 import com.sun.tools.javac.code.Type;
aoqi@0 43 import com.sun.tools.javac.code.Type.MethodType;
aoqi@0 44 import com.sun.tools.javac.code.Types;
aoqi@0 45 import com.sun.tools.javac.comp.LambdaToMethod.LambdaAnalyzerPreprocessor.*;
aoqi@0 46 import com.sun.tools.javac.comp.Lower.BasicFreeVarCollector;
aoqi@0 47 import com.sun.tools.javac.jvm.*;
aoqi@0 48 import com.sun.tools.javac.util.*;
aoqi@0 49 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0 50 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
aoqi@0 51
aoqi@0 52 import java.util.EnumMap;
aoqi@0 53 import java.util.HashMap;
aoqi@0 54 import java.util.HashSet;
aoqi@0 55 import java.util.LinkedHashMap;
aoqi@0 56 import java.util.Map;
aoqi@0 57 import java.util.Set;
aoqi@0 58
aoqi@0 59 import static com.sun.tools.javac.comp.LambdaToMethod.LambdaSymbolKind.*;
aoqi@0 60 import static com.sun.tools.javac.code.Flags.*;
aoqi@0 61 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 62 import static com.sun.tools.javac.code.TypeTag.*;
aoqi@0 63 import static com.sun.tools.javac.tree.JCTree.Tag.*;
aoqi@0 64
aoqi@0 65 /**
aoqi@0 66 * This pass desugars lambda expressions into static methods
aoqi@0 67 *
aoqi@0 68 * <p><b>This is NOT part of any supported API.
aoqi@0 69 * If you write code that depends on this, you do so at your own risk.
aoqi@0 70 * This code and its internal interfaces are subject to change or
aoqi@0 71 * deletion without notice.</b>
aoqi@0 72 */
aoqi@0 73 public class LambdaToMethod extends TreeTranslator {
aoqi@0 74
aoqi@0 75 private Attr attr;
aoqi@0 76 private JCDiagnostic.Factory diags;
aoqi@0 77 private Log log;
aoqi@0 78 private Lower lower;
aoqi@0 79 private Names names;
aoqi@0 80 private Symtab syms;
aoqi@0 81 private Resolve rs;
aoqi@0 82 private TreeMaker make;
aoqi@0 83 private Types types;
aoqi@0 84 private TransTypes transTypes;
aoqi@0 85 private Env<AttrContext> attrEnv;
aoqi@0 86
aoqi@0 87 /** the analyzer scanner */
aoqi@0 88 private LambdaAnalyzerPreprocessor analyzer;
aoqi@0 89
aoqi@0 90 /** map from lambda trees to translation contexts */
aoqi@0 91 private Map<JCTree, TranslationContext<?>> contextMap;
aoqi@0 92
aoqi@0 93 /** current translation context (visitor argument) */
aoqi@0 94 private TranslationContext<?> context;
aoqi@0 95
aoqi@0 96 /** info about the current class being processed */
aoqi@0 97 private KlassInfo kInfo;
aoqi@0 98
aoqi@0 99 /** dump statistics about lambda code generation */
aoqi@0 100 private boolean dumpLambdaToMethodStats;
aoqi@0 101
aoqi@0 102 /** force serializable representation, for stress testing **/
aoqi@0 103 private final boolean forceSerializable;
aoqi@0 104
aoqi@0 105 /** Flag for alternate metafactories indicating the lambda object is intended to be serializable */
aoqi@0 106 public static final int FLAG_SERIALIZABLE = 1 << 0;
aoqi@0 107
aoqi@0 108 /** Flag for alternate metafactories indicating the lambda object has multiple targets */
aoqi@0 109 public static final int FLAG_MARKERS = 1 << 1;
aoqi@0 110
aoqi@0 111 /** Flag for alternate metafactories indicating the lambda object requires multiple bridges */
aoqi@0 112 public static final int FLAG_BRIDGES = 1 << 2;
aoqi@0 113
aoqi@0 114 // <editor-fold defaultstate="collapsed" desc="Instantiating">
aoqi@0 115 protected static final Context.Key<LambdaToMethod> unlambdaKey =
aoqi@0 116 new Context.Key<LambdaToMethod>();
aoqi@0 117
aoqi@0 118 public static LambdaToMethod instance(Context context) {
aoqi@0 119 LambdaToMethod instance = context.get(unlambdaKey);
aoqi@0 120 if (instance == null) {
aoqi@0 121 instance = new LambdaToMethod(context);
aoqi@0 122 }
aoqi@0 123 return instance;
aoqi@0 124 }
aoqi@0 125 private LambdaToMethod(Context context) {
aoqi@0 126 context.put(unlambdaKey, this);
aoqi@0 127 diags = JCDiagnostic.Factory.instance(context);
aoqi@0 128 log = Log.instance(context);
aoqi@0 129 lower = Lower.instance(context);
aoqi@0 130 names = Names.instance(context);
aoqi@0 131 syms = Symtab.instance(context);
aoqi@0 132 rs = Resolve.instance(context);
aoqi@0 133 make = TreeMaker.instance(context);
aoqi@0 134 types = Types.instance(context);
aoqi@0 135 transTypes = TransTypes.instance(context);
aoqi@0 136 analyzer = new LambdaAnalyzerPreprocessor();
aoqi@0 137 Options options = Options.instance(context);
aoqi@0 138 dumpLambdaToMethodStats = options.isSet("dumpLambdaToMethodStats");
aoqi@0 139 attr = Attr.instance(context);
aoqi@0 140 forceSerializable = options.isSet("forceSerializable");
aoqi@0 141 }
aoqi@0 142 // </editor-fold>
aoqi@0 143
aoqi@0 144 private class KlassInfo {
aoqi@0 145
aoqi@0 146 /**
aoqi@0 147 * list of methods to append
aoqi@0 148 */
aoqi@0 149 private ListBuffer<JCTree> appendedMethodList;
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 * list of deserialization cases
aoqi@0 153 */
aoqi@0 154 private final Map<String, ListBuffer<JCStatement>> deserializeCases;
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 * deserialize method symbol
aoqi@0 158 */
aoqi@0 159 private final MethodSymbol deserMethodSym;
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 * deserialize method parameter symbol
aoqi@0 163 */
aoqi@0 164 private final VarSymbol deserParamSym;
aoqi@0 165
aoqi@0 166 private final JCClassDecl clazz;
aoqi@0 167
aoqi@0 168 private KlassInfo(JCClassDecl clazz) {
aoqi@0 169 this.clazz = clazz;
aoqi@0 170 appendedMethodList = new ListBuffer<>();
aoqi@0 171 deserializeCases = new HashMap<String, ListBuffer<JCStatement>>();
aoqi@0 172 MethodType type = new MethodType(List.of(syms.serializedLambdaType), syms.objectType,
aoqi@0 173 List.<Type>nil(), syms.methodClass);
aoqi@0 174 deserMethodSym = makePrivateSyntheticMethod(STATIC, names.deserializeLambda, type, clazz.sym);
aoqi@0 175 deserParamSym = new VarSymbol(FINAL, names.fromString("lambda"),
aoqi@0 176 syms.serializedLambdaType, deserMethodSym);
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 private void addMethod(JCTree decl) {
aoqi@0 180 appendedMethodList = appendedMethodList.prepend(decl);
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 // <editor-fold defaultstate="collapsed" desc="translate methods">
aoqi@0 185 @Override
aoqi@0 186 public <T extends JCTree> T translate(T tree) {
aoqi@0 187 TranslationContext<?> newContext = contextMap.get(tree);
aoqi@0 188 return translate(tree, newContext != null ? newContext : context);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 <T extends JCTree> T translate(T tree, TranslationContext<?> newContext) {
aoqi@0 192 TranslationContext<?> prevContext = context;
aoqi@0 193 try {
aoqi@0 194 context = newContext;
aoqi@0 195 return super.translate(tree);
aoqi@0 196 }
aoqi@0 197 finally {
aoqi@0 198 context = prevContext;
aoqi@0 199 }
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 <T extends JCTree> List<T> translate(List<T> trees, TranslationContext<?> newContext) {
aoqi@0 203 ListBuffer<T> buf = new ListBuffer<>();
aoqi@0 204 for (T tree : trees) {
aoqi@0 205 buf.append(translate(tree, newContext));
aoqi@0 206 }
aoqi@0 207 return buf.toList();
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 public JCTree translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
aoqi@0 211 this.make = make;
aoqi@0 212 this.attrEnv = env;
aoqi@0 213 this.context = null;
aoqi@0 214 this.contextMap = new HashMap<JCTree, TranslationContext<?>>();
aoqi@0 215 return translate(cdef);
aoqi@0 216 }
aoqi@0 217 // </editor-fold>
aoqi@0 218
aoqi@0 219 // <editor-fold defaultstate="collapsed" desc="visitor methods">
aoqi@0 220 /**
aoqi@0 221 * Visit a class.
aoqi@0 222 * Maintain the translatedMethodList across nested classes.
aoqi@0 223 * Append the translatedMethodList to the class after it is translated.
aoqi@0 224 * @param tree
aoqi@0 225 */
aoqi@0 226 @Override
aoqi@0 227 public void visitClassDef(JCClassDecl tree) {
aoqi@0 228 if (tree.sym.owner.kind == PCK) {
aoqi@0 229 //analyze class
aoqi@0 230 tree = analyzer.analyzeAndPreprocessClass(tree);
aoqi@0 231 }
aoqi@0 232 KlassInfo prevKlassInfo = kInfo;
aoqi@0 233 try {
aoqi@0 234 kInfo = new KlassInfo(tree);
aoqi@0 235 super.visitClassDef(tree);
aoqi@0 236 if (!kInfo.deserializeCases.isEmpty()) {
aoqi@0 237 int prevPos = make.pos;
aoqi@0 238 try {
aoqi@0 239 make.at(tree);
aoqi@0 240 kInfo.addMethod(makeDeserializeMethod(tree.sym));
aoqi@0 241 } finally {
aoqi@0 242 make.at(prevPos);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245 //add all translated instance methods here
aoqi@0 246 List<JCTree> newMethods = kInfo.appendedMethodList.toList();
aoqi@0 247 tree.defs = tree.defs.appendList(newMethods);
aoqi@0 248 for (JCTree lambda : newMethods) {
aoqi@0 249 tree.sym.members().enter(((JCMethodDecl)lambda).sym);
aoqi@0 250 }
aoqi@0 251 result = tree;
aoqi@0 252 } finally {
aoqi@0 253 kInfo = prevKlassInfo;
aoqi@0 254 }
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 /**
aoqi@0 258 * Translate a lambda into a method to be inserted into the class.
aoqi@0 259 * Then replace the lambda site with an invokedynamic call of to lambda
aoqi@0 260 * meta-factory, which will use the lambda method.
aoqi@0 261 * @param tree
aoqi@0 262 */
aoqi@0 263 @Override
aoqi@0 264 public void visitLambda(JCLambda tree) {
aoqi@0 265 LambdaTranslationContext localContext = (LambdaTranslationContext)context;
aoqi@0 266 MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
aoqi@0 267 MethodType lambdaType = (MethodType) sym.type;
aoqi@0 268
aoqi@0 269 {
aoqi@0 270 Symbol owner = localContext.owner;
aoqi@0 271 ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
aoqi@0 272 ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
aoqi@0 273
aoqi@0 274 for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
aoqi@0 275 if (tc.position.onLambda == tree) {
aoqi@0 276 lambdaTypeAnnos.append(tc);
aoqi@0 277 } else {
aoqi@0 278 ownerTypeAnnos.append(tc);
aoqi@0 279 }
aoqi@0 280 }
aoqi@0 281 if (lambdaTypeAnnos.nonEmpty()) {
aoqi@0 282 owner.setTypeAttributes(ownerTypeAnnos.toList());
aoqi@0 283 sym.setTypeAttributes(lambdaTypeAnnos.toList());
aoqi@0 284 }
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 //create the method declaration hoisting the lambda body
aoqi@0 288 JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
aoqi@0 289 sym.name,
aoqi@0 290 make.QualIdent(lambdaType.getReturnType().tsym),
aoqi@0 291 List.<JCTypeParameter>nil(),
aoqi@0 292 localContext.syntheticParams,
aoqi@0 293 lambdaType.getThrownTypes() == null ?
aoqi@0 294 List.<JCExpression>nil() :
aoqi@0 295 make.Types(lambdaType.getThrownTypes()),
aoqi@0 296 null,
aoqi@0 297 null);
aoqi@0 298 lambdaDecl.sym = sym;
aoqi@0 299 lambdaDecl.type = lambdaType;
aoqi@0 300
aoqi@0 301 //translate lambda body
aoqi@0 302 //As the lambda body is translated, all references to lambda locals,
aoqi@0 303 //captured variables, enclosing members are adjusted accordingly
aoqi@0 304 //to refer to the static method parameters (rather than i.e. acessing to
aoqi@0 305 //captured members directly).
aoqi@0 306 lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));
aoqi@0 307
aoqi@0 308 //Add the method to the list of methods to be added to this class.
aoqi@0 309 kInfo.addMethod(lambdaDecl);
aoqi@0 310
aoqi@0 311 //now that we have generated a method for the lambda expression,
aoqi@0 312 //we can translate the lambda into a method reference pointing to the newly
aoqi@0 313 //created method.
aoqi@0 314 //
aoqi@0 315 //Note that we need to adjust the method handle so that it will match the
aoqi@0 316 //signature of the SAM descriptor - this means that the method reference
aoqi@0 317 //should be added the following synthetic arguments:
aoqi@0 318 //
aoqi@0 319 // * the "this" argument if it is an instance method
aoqi@0 320 // * enclosing locals captured by the lambda expression
aoqi@0 321
aoqi@0 322 ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();
aoqi@0 323
aoqi@0 324 if (!sym.isStatic()) {
aoqi@0 325 syntheticInits.append(makeThis(
aoqi@0 326 sym.owner.enclClass().asType(),
aoqi@0 327 localContext.owner.enclClass()));
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 //add captured locals
aoqi@0 331 for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
aoqi@0 332 if (fv != localContext.self) {
aoqi@0 333 JCTree captured_local = make.Ident(fv).setType(fv.type);
aoqi@0 334 syntheticInits.append((JCExpression) captured_local);
aoqi@0 335 }
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 //then, determine the arguments to the indy call
aoqi@0 339 List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);
aoqi@0 340
aoqi@0 341 //build a sam instance using an indy call to the meta-factory
aoqi@0 342 int refKind = referenceKind(sym);
aoqi@0 343
aoqi@0 344 //convert to an invokedynamic call
aoqi@0 345 result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 private JCIdent makeThis(Type type, Symbol owner) {
aoqi@0 349 VarSymbol _this = new VarSymbol(PARAMETER | FINAL | SYNTHETIC,
aoqi@0 350 names._this,
aoqi@0 351 type,
aoqi@0 352 owner);
aoqi@0 353 return make.Ident(_this);
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 /**
aoqi@0 357 * Translate a method reference into an invokedynamic call to the
aoqi@0 358 * meta-factory.
aoqi@0 359 * @param tree
aoqi@0 360 */
aoqi@0 361 @Override
aoqi@0 362 public void visitReference(JCMemberReference tree) {
aoqi@0 363 ReferenceTranslationContext localContext = (ReferenceTranslationContext)context;
aoqi@0 364
aoqi@0 365 //first determine the method symbol to be used to generate the sam instance
aoqi@0 366 //this is either the method reference symbol, or the bridged reference symbol
aoqi@0 367 Symbol refSym = localContext.needsBridge()
aoqi@0 368 ? localContext.bridgeSym
aoqi@0 369 : localContext.isSignaturePolymorphic()
aoqi@0 370 ? localContext.sigPolySym
aoqi@0 371 : tree.sym;
aoqi@0 372
aoqi@0 373 //build the bridge method, if needed
aoqi@0 374 if (localContext.needsBridge()) {
aoqi@0 375 bridgeMemberReference(tree, localContext);
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 //the qualifying expression is treated as a special captured arg
aoqi@0 379 JCExpression init;
aoqi@0 380 switch(tree.kind) {
aoqi@0 381
aoqi@0 382 case IMPLICIT_INNER: /** Inner :: new */
aoqi@0 383 case SUPER: /** super :: instMethod */
aoqi@0 384 init = makeThis(
aoqi@0 385 localContext.owner.enclClass().asType(),
aoqi@0 386 localContext.owner.enclClass());
aoqi@0 387 break;
aoqi@0 388
aoqi@0 389 case BOUND: /** Expr :: instMethod */
aoqi@0 390 init = tree.getQualifierExpression();
aoqi@0 391 init = attr.makeNullCheck(init);
aoqi@0 392 break;
aoqi@0 393
aoqi@0 394 case UNBOUND: /** Type :: instMethod */
aoqi@0 395 case STATIC: /** Type :: staticMethod */
aoqi@0 396 case TOPLEVEL: /** Top level :: new */
aoqi@0 397 case ARRAY_CTOR: /** ArrayType :: new */
aoqi@0 398 init = null;
aoqi@0 399 break;
aoqi@0 400
aoqi@0 401 default:
aoqi@0 402 throw new InternalError("Should not have an invalid kind");
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 List<JCExpression> indy_args = init==null? List.<JCExpression>nil() : translate(List.of(init), localContext.prev);
aoqi@0 406
aoqi@0 407
aoqi@0 408 //build a sam instance using an indy call to the meta-factory
aoqi@0 409 result = makeMetafactoryIndyCall(localContext, localContext.referenceKind(), refSym, indy_args);
aoqi@0 410 }
aoqi@0 411
aoqi@0 412 /**
aoqi@0 413 * Translate identifiers within a lambda to the mapped identifier
aoqi@0 414 * @param tree
aoqi@0 415 */
aoqi@0 416 @Override
aoqi@0 417 public void visitIdent(JCIdent tree) {
aoqi@0 418 if (context == null || !analyzer.lambdaIdentSymbolFilter(tree.sym)) {
aoqi@0 419 super.visitIdent(tree);
aoqi@0 420 } else {
aoqi@0 421 int prevPos = make.pos;
aoqi@0 422 try {
aoqi@0 423 make.at(tree);
aoqi@0 424
aoqi@0 425 LambdaTranslationContext lambdaContext = (LambdaTranslationContext) context;
aoqi@0 426 JCTree ltree = lambdaContext.translate(tree);
aoqi@0 427 if (ltree != null) {
aoqi@0 428 result = ltree;
aoqi@0 429 } else {
aoqi@0 430 //access to untranslated symbols (i.e. compile-time constants,
aoqi@0 431 //members defined inside the lambda body, etc.) )
aoqi@0 432 super.visitIdent(tree);
aoqi@0 433 }
aoqi@0 434 } finally {
aoqi@0 435 make.at(prevPos);
aoqi@0 436 }
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 @Override
aoqi@0 441 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 442 LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context;
aoqi@0 443 if (context != null && lambdaContext.getSymbolMap(LOCAL_VAR).containsKey(tree.sym)) {
aoqi@0 444 tree.init = translate(tree.init);
aoqi@0 445 tree.sym = (VarSymbol) lambdaContext.getSymbolMap(LOCAL_VAR).get(tree.sym);
aoqi@0 446 result = tree;
aoqi@0 447 } else if (context != null && lambdaContext.getSymbolMap(TYPE_VAR).containsKey(tree.sym)) {
aoqi@0 448 JCExpression init = translate(tree.init);
aoqi@0 449 VarSymbol xsym = (VarSymbol)lambdaContext.getSymbolMap(TYPE_VAR).get(tree.sym);
aoqi@0 450 int prevPos = make.pos;
aoqi@0 451 try {
aoqi@0 452 result = make.at(tree).VarDef(xsym, init);
aoqi@0 453 } finally {
aoqi@0 454 make.at(prevPos);
aoqi@0 455 }
aoqi@0 456 // Replace the entered symbol for this variable
aoqi@0 457 Scope sc = tree.sym.owner.members();
aoqi@0 458 if (sc != null) {
aoqi@0 459 sc.remove(tree.sym);
aoqi@0 460 sc.enter(xsym);
aoqi@0 461 }
aoqi@0 462 } else {
aoqi@0 463 super.visitVarDef(tree);
aoqi@0 464 }
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 // </editor-fold>
aoqi@0 468
aoqi@0 469 // <editor-fold defaultstate="collapsed" desc="Translation helper methods">
aoqi@0 470
aoqi@0 471 private JCBlock makeLambdaBody(JCLambda tree, JCMethodDecl lambdaMethodDecl) {
aoqi@0 472 return tree.getBodyKind() == JCLambda.BodyKind.EXPRESSION ?
aoqi@0 473 makeLambdaExpressionBody((JCExpression)tree.body, lambdaMethodDecl) :
aoqi@0 474 makeLambdaStatementBody((JCBlock)tree.body, lambdaMethodDecl, tree.canCompleteNormally);
aoqi@0 475 }
aoqi@0 476
aoqi@0 477 private JCBlock makeLambdaExpressionBody(JCExpression expr, JCMethodDecl lambdaMethodDecl) {
aoqi@0 478 Type restype = lambdaMethodDecl.type.getReturnType();
aoqi@0 479 boolean isLambda_void = expr.type.hasTag(VOID);
aoqi@0 480 boolean isTarget_void = restype.hasTag(VOID);
aoqi@0 481 boolean isTarget_Void = types.isSameType(restype, types.boxedClass(syms.voidType).type);
aoqi@0 482 int prevPos = make.pos;
aoqi@0 483 try {
aoqi@0 484 if (isTarget_void) {
aoqi@0 485 //target is void:
aoqi@0 486 // BODY;
aoqi@0 487 JCStatement stat = make.at(expr).Exec(expr);
aoqi@0 488 return make.Block(0, List.<JCStatement>of(stat));
aoqi@0 489 } else if (isLambda_void && isTarget_Void) {
aoqi@0 490 //void to Void conversion:
aoqi@0 491 // BODY; return null;
aoqi@0 492 ListBuffer<JCStatement> stats = new ListBuffer<>();
aoqi@0 493 stats.append(make.at(expr).Exec(expr));
aoqi@0 494 stats.append(make.Return(make.Literal(BOT, null).setType(syms.botType)));
aoqi@0 495 return make.Block(0, stats.toList());
aoqi@0 496 } else {
aoqi@0 497 //non-void to non-void conversion:
aoqi@0 498 // return (TYPE)BODY;
aoqi@0 499 JCExpression retExpr = transTypes.coerce(attrEnv, expr, restype);
aoqi@0 500 return make.at(retExpr).Block(0, List.<JCStatement>of(make.Return(retExpr)));
aoqi@0 501 }
aoqi@0 502 } finally {
aoqi@0 503 make.at(prevPos);
aoqi@0 504 }
aoqi@0 505 }
aoqi@0 506
aoqi@0 507 private JCBlock makeLambdaStatementBody(JCBlock block, final JCMethodDecl lambdaMethodDecl, boolean completeNormally) {
aoqi@0 508 final Type restype = lambdaMethodDecl.type.getReturnType();
aoqi@0 509 final boolean isTarget_void = restype.hasTag(VOID);
aoqi@0 510 boolean isTarget_Void = types.isSameType(restype, types.boxedClass(syms.voidType).type);
aoqi@0 511
aoqi@0 512 class LambdaBodyTranslator extends TreeTranslator {
aoqi@0 513
aoqi@0 514 @Override
aoqi@0 515 public void visitClassDef(JCClassDecl tree) {
aoqi@0 516 //do NOT recurse on any inner classes
aoqi@0 517 result = tree;
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 @Override
aoqi@0 521 public void visitLambda(JCLambda tree) {
aoqi@0 522 //do NOT recurse on any nested lambdas
aoqi@0 523 result = tree;
aoqi@0 524 }
aoqi@0 525
aoqi@0 526 @Override
aoqi@0 527 public void visitReturn(JCReturn tree) {
aoqi@0 528 boolean isLambda_void = tree.expr == null;
aoqi@0 529 if (isTarget_void && !isLambda_void) {
aoqi@0 530 //Void to void conversion:
aoqi@0 531 // { TYPE $loc = RET-EXPR; return; }
aoqi@0 532 VarSymbol loc = makeSyntheticVar(0, names.fromString("$loc"), tree.expr.type, lambdaMethodDecl.sym);
aoqi@0 533 JCVariableDecl varDef = make.VarDef(loc, tree.expr);
aoqi@0 534 result = make.Block(0, List.<JCStatement>of(varDef, make.Return(null)));
aoqi@0 535 } else if (!isTarget_void || !isLambda_void) {
aoqi@0 536 //non-void to non-void conversion:
aoqi@0 537 // return (TYPE)RET-EXPR;
aoqi@0 538 tree.expr = transTypes.coerce(attrEnv, tree.expr, restype);
aoqi@0 539 result = tree;
aoqi@0 540 } else {
aoqi@0 541 result = tree;
aoqi@0 542 }
aoqi@0 543
aoqi@0 544 }
aoqi@0 545 }
aoqi@0 546
aoqi@0 547 JCBlock trans_block = new LambdaBodyTranslator().translate(block);
aoqi@0 548 if (completeNormally && isTarget_Void) {
aoqi@0 549 //there's no return statement and the lambda (possibly inferred)
aoqi@0 550 //return type is java.lang.Void; emit a synthetic return statement
aoqi@0 551 trans_block.stats = trans_block.stats.append(make.Return(make.Literal(BOT, null).setType(syms.botType)));
aoqi@0 552 }
aoqi@0 553 return trans_block;
aoqi@0 554 }
aoqi@0 555
aoqi@0 556 private JCMethodDecl makeDeserializeMethod(Symbol kSym) {
aoqi@0 557 ListBuffer<JCCase> cases = new ListBuffer<>();
aoqi@0 558 ListBuffer<JCBreak> breaks = new ListBuffer<>();
aoqi@0 559 for (Map.Entry<String, ListBuffer<JCStatement>> entry : kInfo.deserializeCases.entrySet()) {
aoqi@0 560 JCBreak br = make.Break(null);
aoqi@0 561 breaks.add(br);
aoqi@0 562 List<JCStatement> stmts = entry.getValue().append(br).toList();
aoqi@0 563 cases.add(make.Case(make.Literal(entry.getKey()), stmts));
aoqi@0 564 }
aoqi@0 565 JCSwitch sw = make.Switch(deserGetter("getImplMethodName", syms.stringType), cases.toList());
aoqi@0 566 for (JCBreak br : breaks) {
aoqi@0 567 br.target = sw;
aoqi@0 568 }
aoqi@0 569 JCBlock body = make.Block(0L, List.<JCStatement>of(
aoqi@0 570 sw,
aoqi@0 571 make.Throw(makeNewClass(
aoqi@0 572 syms.illegalArgumentExceptionType,
aoqi@0 573 List.<JCExpression>of(make.Literal("Invalid lambda deserialization"))))));
aoqi@0 574 JCMethodDecl deser = make.MethodDef(make.Modifiers(kInfo.deserMethodSym.flags()),
aoqi@0 575 names.deserializeLambda,
aoqi@0 576 make.QualIdent(kInfo.deserMethodSym.getReturnType().tsym),
aoqi@0 577 List.<JCTypeParameter>nil(),
aoqi@0 578 List.of(make.VarDef(kInfo.deserParamSym, null)),
aoqi@0 579 List.<JCExpression>nil(),
aoqi@0 580 body,
aoqi@0 581 null);
aoqi@0 582 deser.sym = kInfo.deserMethodSym;
aoqi@0 583 deser.type = kInfo.deserMethodSym.type;
aoqi@0 584 //System.err.printf("DESER: '%s'\n", deser);
aoqi@0 585 return deser;
aoqi@0 586 }
aoqi@0 587
aoqi@0 588 /** Make an attributed class instance creation expression.
aoqi@0 589 * @param ctype The class type.
aoqi@0 590 * @param args The constructor arguments.
aoqi@0 591 * @param cons The constructor symbol
aoqi@0 592 */
aoqi@0 593 JCNewClass makeNewClass(Type ctype, List<JCExpression> args, Symbol cons) {
aoqi@0 594 JCNewClass tree = make.NewClass(null,
aoqi@0 595 null, make.QualIdent(ctype.tsym), args, null);
aoqi@0 596 tree.constructor = cons;
aoqi@0 597 tree.type = ctype;
aoqi@0 598 return tree;
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 /** Make an attributed class instance creation expression.
aoqi@0 602 * @param ctype The class type.
aoqi@0 603 * @param args The constructor arguments.
aoqi@0 604 */
aoqi@0 605 JCNewClass makeNewClass(Type ctype, List<JCExpression> args) {
aoqi@0 606 return makeNewClass(ctype, args,
aoqi@0 607 rs.resolveConstructor(null, attrEnv, ctype, TreeInfo.types(args), List.<Type>nil()));
aoqi@0 608 }
aoqi@0 609
aoqi@0 610 private void addDeserializationCase(int implMethodKind, Symbol refSym, Type targetType, MethodSymbol samSym,
aoqi@0 611 DiagnosticPosition pos, List<Object> staticArgs, MethodType indyType) {
aoqi@0 612 String functionalInterfaceClass = classSig(targetType);
aoqi@0 613 String functionalInterfaceMethodName = samSym.getSimpleName().toString();
aoqi@0 614 String functionalInterfaceMethodSignature = typeSig(types.erasure(samSym.type));
aoqi@0 615 String implClass = classSig(types.erasure(refSym.owner.type));
aoqi@0 616 String implMethodName = refSym.getQualifiedName().toString();
aoqi@0 617 String implMethodSignature = typeSig(types.erasure(refSym.type));
aoqi@0 618
aoqi@0 619 JCExpression kindTest = eqTest(syms.intType, deserGetter("getImplMethodKind", syms.intType), make.Literal(implMethodKind));
aoqi@0 620 ListBuffer<JCExpression> serArgs = new ListBuffer<>();
aoqi@0 621 int i = 0;
aoqi@0 622 for (Type t : indyType.getParameterTypes()) {
aoqi@0 623 List<JCExpression> indexAsArg = new ListBuffer<JCExpression>().append(make.Literal(i)).toList();
aoqi@0 624 List<Type> argTypes = new ListBuffer<Type>().append(syms.intType).toList();
aoqi@0 625 serArgs.add(make.TypeCast(types.erasure(t), deserGetter("getCapturedArg", syms.objectType, argTypes, indexAsArg)));
aoqi@0 626 ++i;
aoqi@0 627 }
aoqi@0 628 JCStatement stmt = make.If(
aoqi@0 629 deserTest(deserTest(deserTest(deserTest(deserTest(
aoqi@0 630 kindTest,
aoqi@0 631 "getFunctionalInterfaceClass", functionalInterfaceClass),
aoqi@0 632 "getFunctionalInterfaceMethodName", functionalInterfaceMethodName),
aoqi@0 633 "getFunctionalInterfaceMethodSignature", functionalInterfaceMethodSignature),
aoqi@0 634 "getImplClass", implClass),
aoqi@0 635 "getImplMethodSignature", implMethodSignature),
aoqi@0 636 make.Return(makeIndyCall(
aoqi@0 637 pos,
aoqi@0 638 syms.lambdaMetafactory,
aoqi@0 639 names.altMetafactory,
aoqi@0 640 staticArgs, indyType, serArgs.toList(), samSym.name)),
aoqi@0 641 null);
aoqi@0 642 ListBuffer<JCStatement> stmts = kInfo.deserializeCases.get(implMethodName);
aoqi@0 643 if (stmts == null) {
aoqi@0 644 stmts = new ListBuffer<>();
aoqi@0 645 kInfo.deserializeCases.put(implMethodName, stmts);
aoqi@0 646 }
aoqi@0 647 /****
aoqi@0 648 System.err.printf("+++++++++++++++++\n");
aoqi@0 649 System.err.printf("*functionalInterfaceClass: '%s'\n", functionalInterfaceClass);
aoqi@0 650 System.err.printf("*functionalInterfaceMethodName: '%s'\n", functionalInterfaceMethodName);
aoqi@0 651 System.err.printf("*functionalInterfaceMethodSignature: '%s'\n", functionalInterfaceMethodSignature);
aoqi@0 652 System.err.printf("*implMethodKind: %d\n", implMethodKind);
aoqi@0 653 System.err.printf("*implClass: '%s'\n", implClass);
aoqi@0 654 System.err.printf("*implMethodName: '%s'\n", implMethodName);
aoqi@0 655 System.err.printf("*implMethodSignature: '%s'\n", implMethodSignature);
aoqi@0 656 ****/
aoqi@0 657 stmts.append(stmt);
aoqi@0 658 }
aoqi@0 659
aoqi@0 660 private JCExpression eqTest(Type argType, JCExpression arg1, JCExpression arg2) {
aoqi@0 661 JCBinary testExpr = make.Binary(JCTree.Tag.EQ, arg1, arg2);
aoqi@0 662 testExpr.operator = rs.resolveBinaryOperator(null, JCTree.Tag.EQ, attrEnv, argType, argType);
aoqi@0 663 testExpr.setType(syms.booleanType);
aoqi@0 664 return testExpr;
aoqi@0 665 }
aoqi@0 666
aoqi@0 667 private JCExpression deserTest(JCExpression prev, String func, String lit) {
aoqi@0 668 MethodType eqmt = new MethodType(List.of(syms.objectType), syms.booleanType, List.<Type>nil(), syms.methodClass);
aoqi@0 669 Symbol eqsym = rs.resolveQualifiedMethod(null, attrEnv, syms.objectType, names.equals, List.of(syms.objectType), List.<Type>nil());
aoqi@0 670 JCMethodInvocation eqtest = make.Apply(
aoqi@0 671 List.<JCExpression>nil(),
aoqi@0 672 make.Select(deserGetter(func, syms.stringType), eqsym).setType(eqmt),
aoqi@0 673 List.<JCExpression>of(make.Literal(lit)));
aoqi@0 674 eqtest.setType(syms.booleanType);
aoqi@0 675 JCBinary compound = make.Binary(JCTree.Tag.AND, prev, eqtest);
aoqi@0 676 compound.operator = rs.resolveBinaryOperator(null, JCTree.Tag.AND, attrEnv, syms.booleanType, syms.booleanType);
aoqi@0 677 compound.setType(syms.booleanType);
aoqi@0 678 return compound;
aoqi@0 679 }
aoqi@0 680
aoqi@0 681 private JCExpression deserGetter(String func, Type type) {
aoqi@0 682 return deserGetter(func, type, List.<Type>nil(), List.<JCExpression>nil());
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 private JCExpression deserGetter(String func, Type type, List<Type> argTypes, List<JCExpression> args) {
aoqi@0 686 MethodType getmt = new MethodType(argTypes, type, List.<Type>nil(), syms.methodClass);
aoqi@0 687 Symbol getsym = rs.resolveQualifiedMethod(null, attrEnv, syms.serializedLambdaType, names.fromString(func), argTypes, List.<Type>nil());
aoqi@0 688 return make.Apply(
aoqi@0 689 List.<JCExpression>nil(),
aoqi@0 690 make.Select(make.Ident(kInfo.deserParamSym).setType(syms.serializedLambdaType), getsym).setType(getmt),
aoqi@0 691 args).setType(type);
aoqi@0 692 }
aoqi@0 693
aoqi@0 694 /**
aoqi@0 695 * Create new synthetic method with given flags, name, type, owner
aoqi@0 696 */
aoqi@0 697 private MethodSymbol makePrivateSyntheticMethod(long flags, Name name, Type type, Symbol owner) {
aoqi@0 698 return new MethodSymbol(flags | SYNTHETIC | PRIVATE, name, type, owner);
aoqi@0 699 }
aoqi@0 700
aoqi@0 701 /**
aoqi@0 702 * Create new synthetic variable with given flags, name, type, owner
aoqi@0 703 */
aoqi@0 704 private VarSymbol makeSyntheticVar(long flags, String name, Type type, Symbol owner) {
aoqi@0 705 return makeSyntheticVar(flags, names.fromString(name), type, owner);
aoqi@0 706 }
aoqi@0 707
aoqi@0 708 /**
aoqi@0 709 * Create new synthetic variable with given flags, name, type, owner
aoqi@0 710 */
aoqi@0 711 private VarSymbol makeSyntheticVar(long flags, Name name, Type type, Symbol owner) {
aoqi@0 712 return new VarSymbol(flags | SYNTHETIC, name, type, owner);
aoqi@0 713 }
aoqi@0 714
aoqi@0 715 /**
aoqi@0 716 * Set varargsElement field on a given tree (must be either a new class tree
aoqi@0 717 * or a method call tree)
aoqi@0 718 */
aoqi@0 719 private void setVarargsIfNeeded(JCTree tree, Type varargsElement) {
aoqi@0 720 if (varargsElement != null) {
aoqi@0 721 switch (tree.getTag()) {
aoqi@0 722 case APPLY: ((JCMethodInvocation)tree).varargsElement = varargsElement; break;
aoqi@0 723 case NEWCLASS: ((JCNewClass)tree).varargsElement = varargsElement; break;
aoqi@0 724 default: throw new AssertionError();
aoqi@0 725 }
aoqi@0 726 }
aoqi@0 727 }
aoqi@0 728
aoqi@0 729 /**
aoqi@0 730 * Convert method/constructor arguments by inserting appropriate cast
aoqi@0 731 * as required by type-erasure - this is needed when bridging a lambda/method
aoqi@0 732 * reference, as the bridged signature might require downcast to be compatible
aoqi@0 733 * with the generated signature.
aoqi@0 734 */
aoqi@0 735 private List<JCExpression> convertArgs(Symbol meth, List<JCExpression> args, Type varargsElement) {
aoqi@0 736 Assert.check(meth.kind == Kinds.MTH);
aoqi@0 737 List<Type> formals = types.erasure(meth.type).getParameterTypes();
aoqi@0 738 if (varargsElement != null) {
aoqi@0 739 Assert.check((meth.flags() & VARARGS) != 0);
aoqi@0 740 }
aoqi@0 741 return transTypes.translateArgs(args, formals, varargsElement, attrEnv);
aoqi@0 742 }
aoqi@0 743
aoqi@0 744 // </editor-fold>
aoqi@0 745
aoqi@0 746 /**
aoqi@0 747 * Generate an adapter method "bridge" for a method reference which cannot
aoqi@0 748 * be used directly.
aoqi@0 749 */
aoqi@0 750 private class MemberReferenceBridger {
aoqi@0 751
aoqi@0 752 private final JCMemberReference tree;
aoqi@0 753 private final ReferenceTranslationContext localContext;
aoqi@0 754 private final ListBuffer<JCExpression> args = new ListBuffer<>();
aoqi@0 755 private final ListBuffer<JCVariableDecl> params = new ListBuffer<>();
aoqi@0 756
aoqi@0 757 MemberReferenceBridger(JCMemberReference tree, ReferenceTranslationContext localContext) {
aoqi@0 758 this.tree = tree;
aoqi@0 759 this.localContext = localContext;
aoqi@0 760 }
aoqi@0 761
aoqi@0 762 /**
aoqi@0 763 * Generate the bridge
aoqi@0 764 */
aoqi@0 765 JCMethodDecl bridge() {
aoqi@0 766 int prevPos = make.pos;
aoqi@0 767 try {
aoqi@0 768 make.at(tree);
aoqi@0 769 Type samDesc = localContext.bridgedRefSig();
aoqi@0 770 List<Type> samPTypes = samDesc.getParameterTypes();
aoqi@0 771
aoqi@0 772 //an extra argument is prepended to the signature of the bridge in case
aoqi@0 773 //the member reference is an instance method reference (in which case
aoqi@0 774 //the receiver expression is passed to the bridge itself).
aoqi@0 775 Type recType = null;
aoqi@0 776 switch (tree.kind) {
aoqi@0 777 case IMPLICIT_INNER:
aoqi@0 778 recType = tree.sym.owner.type.getEnclosingType();
aoqi@0 779 break;
aoqi@0 780 case BOUND:
aoqi@0 781 recType = tree.getQualifierExpression().type;
aoqi@0 782 break;
aoqi@0 783 case UNBOUND:
aoqi@0 784 recType = samPTypes.head;
aoqi@0 785 samPTypes = samPTypes.tail;
aoqi@0 786 break;
aoqi@0 787 }
aoqi@0 788
aoqi@0 789 //generate the parameter list for the bridged member reference - the
aoqi@0 790 //bridge signature will match the signature of the target sam descriptor
aoqi@0 791
aoqi@0 792 VarSymbol rcvr = (recType == null)
aoqi@0 793 ? null
aoqi@0 794 : addParameter("rec$", recType, false);
aoqi@0 795
aoqi@0 796 List<Type> refPTypes = tree.sym.type.getParameterTypes();
aoqi@0 797 int refSize = refPTypes.size();
aoqi@0 798 int samSize = samPTypes.size();
aoqi@0 799 // Last parameter to copy from referenced method
aoqi@0 800 int last = localContext.needsVarArgsConversion() ? refSize - 1 : refSize;
aoqi@0 801
aoqi@0 802 List<Type> l = refPTypes;
aoqi@0 803 // Use parameter types of the referenced method, excluding final var args
aoqi@0 804 for (int i = 0; l.nonEmpty() && i < last; ++i) {
aoqi@0 805 addParameter("x$" + i, l.head, true);
aoqi@0 806 l = l.tail;
aoqi@0 807 }
aoqi@0 808 // Flatten out the var args
aoqi@0 809 for (int i = last; i < samSize; ++i) {
aoqi@0 810 addParameter("xva$" + i, tree.varargsElement, true);
aoqi@0 811 }
aoqi@0 812
aoqi@0 813 //generate the bridge method declaration
aoqi@0 814 JCMethodDecl bridgeDecl = make.MethodDef(make.Modifiers(localContext.bridgeSym.flags()),
aoqi@0 815 localContext.bridgeSym.name,
aoqi@0 816 make.QualIdent(samDesc.getReturnType().tsym),
aoqi@0 817 List.<JCTypeParameter>nil(),
aoqi@0 818 params.toList(),
aoqi@0 819 tree.sym.type.getThrownTypes() == null
aoqi@0 820 ? List.<JCExpression>nil()
aoqi@0 821 : make.Types(tree.sym.type.getThrownTypes()),
aoqi@0 822 null,
aoqi@0 823 null);
aoqi@0 824 bridgeDecl.sym = (MethodSymbol) localContext.bridgeSym;
aoqi@0 825 bridgeDecl.type = localContext.bridgeSym.type =
aoqi@0 826 types.createMethodTypeWithParameters(samDesc, TreeInfo.types(params.toList()));
aoqi@0 827
aoqi@0 828 //bridge method body generation - this can be either a method call or a
aoqi@0 829 //new instance creation expression, depending on the member reference kind
aoqi@0 830 JCExpression bridgeExpr = (tree.getMode() == ReferenceMode.INVOKE)
aoqi@0 831 ? bridgeExpressionInvoke(makeReceiver(rcvr))
aoqi@0 832 : bridgeExpressionNew();
aoqi@0 833
aoqi@0 834 //the body is either a return expression containing a method call,
aoqi@0 835 //or the method call itself, depending on whether the return type of
aoqi@0 836 //the bridge is non-void/void.
aoqi@0 837 bridgeDecl.body = makeLambdaExpressionBody(bridgeExpr, bridgeDecl);
aoqi@0 838
aoqi@0 839 return bridgeDecl;
aoqi@0 840 } finally {
aoqi@0 841 make.at(prevPos);
aoqi@0 842 }
aoqi@0 843 }
aoqi@0 844 //where
aoqi@0 845 private JCExpression makeReceiver(VarSymbol rcvr) {
aoqi@0 846 if (rcvr == null) return null;
aoqi@0 847 JCExpression rcvrExpr = make.Ident(rcvr);
aoqi@0 848 Type rcvrType = tree.sym.enclClass().type;
aoqi@0 849 if (!rcvr.type.tsym.isSubClass(rcvrType.tsym, types)) {
aoqi@0 850 rcvrExpr = make.TypeCast(make.Type(rcvrType), rcvrExpr).setType(rcvrType);
aoqi@0 851 }
aoqi@0 852 return rcvrExpr;
aoqi@0 853 }
aoqi@0 854
aoqi@0 855 /**
aoqi@0 856 * determine the receiver of the bridged method call - the receiver can
aoqi@0 857 * be either the synthetic receiver parameter or a type qualifier; the
aoqi@0 858 * original qualifier expression is never used here, as it might refer
aoqi@0 859 * to symbols not available in the static context of the bridge
aoqi@0 860 */
aoqi@0 861 private JCExpression bridgeExpressionInvoke(JCExpression rcvr) {
aoqi@0 862 JCExpression qualifier =
aoqi@0 863 tree.sym.isStatic() ?
aoqi@0 864 make.Type(tree.sym.owner.type) :
aoqi@0 865 (rcvr != null) ?
aoqi@0 866 rcvr :
aoqi@0 867 tree.getQualifierExpression();
aoqi@0 868
aoqi@0 869 //create the qualifier expression
aoqi@0 870 JCFieldAccess select = make.Select(qualifier, tree.sym.name);
aoqi@0 871 select.sym = tree.sym;
aoqi@0 872 select.type = tree.sym.erasure(types);
aoqi@0 873
aoqi@0 874 //create the method call expression
aoqi@0 875 JCExpression apply = make.Apply(List.<JCExpression>nil(), select,
aoqi@0 876 convertArgs(tree.sym, args.toList(), tree.varargsElement)).
aoqi@0 877 setType(tree.sym.erasure(types).getReturnType());
aoqi@0 878
aoqi@0 879 apply = transTypes.coerce(apply, localContext.generatedRefSig().getReturnType());
aoqi@0 880 setVarargsIfNeeded(apply, tree.varargsElement);
aoqi@0 881 return apply;
aoqi@0 882 }
aoqi@0 883
aoqi@0 884 /**
aoqi@0 885 * the enclosing expression is either 'null' (no enclosing type) or set
aoqi@0 886 * to the first bridge synthetic parameter
aoqi@0 887 */
aoqi@0 888 private JCExpression bridgeExpressionNew() {
aoqi@0 889 if (tree.kind == ReferenceKind.ARRAY_CTOR) {
aoqi@0 890 //create the array creation expression
aoqi@0 891 JCNewArray newArr = make.NewArray(
aoqi@0 892 make.Type(types.elemtype(tree.getQualifierExpression().type)),
aoqi@0 893 List.of(make.Ident(params.first())),
aoqi@0 894 null);
aoqi@0 895 newArr.type = tree.getQualifierExpression().type;
aoqi@0 896 return newArr;
aoqi@0 897 } else {
aoqi@0 898 JCExpression encl = null;
aoqi@0 899 switch (tree.kind) {
aoqi@0 900 case UNBOUND:
aoqi@0 901 case IMPLICIT_INNER:
aoqi@0 902 encl = make.Ident(params.first());
aoqi@0 903 }
aoqi@0 904
aoqi@0 905 //create the instance creation expression
aoqi@0 906 JCNewClass newClass = make.NewClass(encl,
aoqi@0 907 List.<JCExpression>nil(),
aoqi@0 908 make.Type(tree.getQualifierExpression().type),
aoqi@0 909 convertArgs(tree.sym, args.toList(), tree.varargsElement),
aoqi@0 910 null);
aoqi@0 911 newClass.constructor = tree.sym;
aoqi@0 912 newClass.constructorType = tree.sym.erasure(types);
aoqi@0 913 newClass.type = tree.getQualifierExpression().type;
aoqi@0 914 setVarargsIfNeeded(newClass, tree.varargsElement);
aoqi@0 915 return newClass;
aoqi@0 916 }
aoqi@0 917 }
aoqi@0 918
aoqi@0 919 private VarSymbol addParameter(String name, Type p, boolean genArg) {
aoqi@0 920 VarSymbol vsym = new VarSymbol(0, names.fromString(name), p, localContext.bridgeSym);
aoqi@0 921 params.append(make.VarDef(vsym, null));
aoqi@0 922 if (genArg) {
aoqi@0 923 args.append(make.Ident(vsym));
aoqi@0 924 }
aoqi@0 925 return vsym;
aoqi@0 926 }
aoqi@0 927 }
aoqi@0 928
aoqi@0 929 /**
aoqi@0 930 * Bridges a member reference - this is needed when:
aoqi@0 931 * * Var args in the referenced method need to be flattened away
aoqi@0 932 * * super is used
aoqi@0 933 */
aoqi@0 934 private void bridgeMemberReference(JCMemberReference tree, ReferenceTranslationContext localContext) {
aoqi@0 935 kInfo.addMethod(new MemberReferenceBridger(tree, localContext).bridge());
aoqi@0 936 }
aoqi@0 937
aoqi@0 938 private MethodType typeToMethodType(Type mt) {
aoqi@0 939 Type type = types.erasure(mt);
aoqi@0 940 return new MethodType(type.getParameterTypes(),
aoqi@0 941 type.getReturnType(),
aoqi@0 942 type.getThrownTypes(),
aoqi@0 943 syms.methodClass);
aoqi@0 944 }
aoqi@0 945
aoqi@0 946 /**
aoqi@0 947 * Generate an indy method call to the meta factory
aoqi@0 948 */
aoqi@0 949 private JCExpression makeMetafactoryIndyCall(TranslationContext<?> context,
aoqi@0 950 int refKind, Symbol refSym, List<JCExpression> indy_args) {
aoqi@0 951 JCFunctionalExpression tree = context.tree;
aoqi@0 952 //determine the static bsm args
aoqi@0 953 MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.type.tsym);
aoqi@0 954 List<Object> staticArgs = List.<Object>of(
aoqi@0 955 typeToMethodType(samSym.type),
aoqi@0 956 new Pool.MethodHandle(refKind, refSym, types),
aoqi@0 957 typeToMethodType(tree.getDescriptorType(types)));
aoqi@0 958
aoqi@0 959 //computed indy arg types
aoqi@0 960 ListBuffer<Type> indy_args_types = new ListBuffer<>();
aoqi@0 961 for (JCExpression arg : indy_args) {
aoqi@0 962 indy_args_types.append(arg.type);
aoqi@0 963 }
aoqi@0 964
aoqi@0 965 //finally, compute the type of the indy call
aoqi@0 966 MethodType indyType = new MethodType(indy_args_types.toList(),
aoqi@0 967 tree.type,
aoqi@0 968 List.<Type>nil(),
aoqi@0 969 syms.methodClass);
aoqi@0 970
aoqi@0 971 Name metafactoryName = context.needsAltMetafactory() ?
aoqi@0 972 names.altMetafactory : names.metafactory;
aoqi@0 973
aoqi@0 974 if (context.needsAltMetafactory()) {
aoqi@0 975 ListBuffer<Object> markers = new ListBuffer<>();
aoqi@0 976 for (Type t : tree.targets.tail) {
aoqi@0 977 if (t.tsym != syms.serializableType.tsym) {
aoqi@0 978 markers.append(t.tsym);
aoqi@0 979 }
aoqi@0 980 }
aoqi@0 981 int flags = context.isSerializable() ? FLAG_SERIALIZABLE : 0;
aoqi@0 982 boolean hasMarkers = markers.nonEmpty();
aoqi@0 983 boolean hasBridges = context.bridges.nonEmpty();
aoqi@0 984 if (hasMarkers) {
aoqi@0 985 flags |= FLAG_MARKERS;
aoqi@0 986 }
aoqi@0 987 if (hasBridges) {
aoqi@0 988 flags |= FLAG_BRIDGES;
aoqi@0 989 }
aoqi@0 990 staticArgs = staticArgs.append(flags);
aoqi@0 991 if (hasMarkers) {
aoqi@0 992 staticArgs = staticArgs.append(markers.length());
aoqi@0 993 staticArgs = staticArgs.appendList(markers.toList());
aoqi@0 994 }
aoqi@0 995 if (hasBridges) {
aoqi@0 996 staticArgs = staticArgs.append(context.bridges.length() - 1);
aoqi@0 997 for (Symbol s : context.bridges) {
aoqi@0 998 Type s_erasure = s.erasure(types);
aoqi@0 999 if (!types.isSameType(s_erasure, samSym.erasure(types))) {
aoqi@0 1000 staticArgs = staticArgs.append(s.erasure(types));
aoqi@0 1001 }
aoqi@0 1002 }
aoqi@0 1003 }
aoqi@0 1004 if (context.isSerializable()) {
aoqi@0 1005 int prevPos = make.pos;
aoqi@0 1006 try {
aoqi@0 1007 make.at(kInfo.clazz);
aoqi@0 1008 addDeserializationCase(refKind, refSym, tree.type, samSym,
aoqi@0 1009 tree, staticArgs, indyType);
aoqi@0 1010 } finally {
aoqi@0 1011 make.at(prevPos);
aoqi@0 1012 }
aoqi@0 1013 }
aoqi@0 1014 }
aoqi@0 1015
aoqi@0 1016 return makeIndyCall(tree, syms.lambdaMetafactory, metafactoryName, staticArgs, indyType, indy_args, samSym.name);
aoqi@0 1017 }
aoqi@0 1018
aoqi@0 1019 /**
aoqi@0 1020 * Generate an indy method call with given name, type and static bootstrap
aoqi@0 1021 * arguments types
aoqi@0 1022 */
aoqi@0 1023 private JCExpression makeIndyCall(DiagnosticPosition pos, Type site, Name bsmName,
aoqi@0 1024 List<Object> staticArgs, MethodType indyType, List<JCExpression> indyArgs,
aoqi@0 1025 Name methName) {
aoqi@0 1026 int prevPos = make.pos;
aoqi@0 1027 try {
aoqi@0 1028 make.at(pos);
aoqi@0 1029 List<Type> bsm_staticArgs = List.of(syms.methodHandleLookupType,
aoqi@0 1030 syms.stringType,
aoqi@0 1031 syms.methodTypeType).appendList(bsmStaticArgToTypes(staticArgs));
aoqi@0 1032
aoqi@0 1033 Symbol bsm = rs.resolveInternalMethod(pos, attrEnv, site,
aoqi@0 1034 bsmName, bsm_staticArgs, List.<Type>nil());
aoqi@0 1035
aoqi@0 1036 DynamicMethodSymbol dynSym =
aoqi@0 1037 new DynamicMethodSymbol(methName,
aoqi@0 1038 syms.noSymbol,
aoqi@0 1039 bsm.isStatic() ?
aoqi@0 1040 ClassFile.REF_invokeStatic :
aoqi@0 1041 ClassFile.REF_invokeVirtual,
aoqi@0 1042 (MethodSymbol)bsm,
aoqi@0 1043 indyType,
aoqi@0 1044 staticArgs.toArray());
aoqi@0 1045
aoqi@0 1046 JCFieldAccess qualifier = make.Select(make.QualIdent(site.tsym), bsmName);
aoqi@0 1047 qualifier.sym = dynSym;
aoqi@0 1048 qualifier.type = indyType.getReturnType();
aoqi@0 1049
aoqi@0 1050 JCMethodInvocation proxyCall = make.Apply(List.<JCExpression>nil(), qualifier, indyArgs);
aoqi@0 1051 proxyCall.type = indyType.getReturnType();
aoqi@0 1052 return proxyCall;
aoqi@0 1053 } finally {
aoqi@0 1054 make.at(prevPos);
aoqi@0 1055 }
aoqi@0 1056 }
aoqi@0 1057 //where
aoqi@0 1058 private List<Type> bsmStaticArgToTypes(List<Object> args) {
aoqi@0 1059 ListBuffer<Type> argtypes = new ListBuffer<>();
aoqi@0 1060 for (Object arg : args) {
aoqi@0 1061 argtypes.append(bsmStaticArgToType(arg));
aoqi@0 1062 }
aoqi@0 1063 return argtypes.toList();
aoqi@0 1064 }
aoqi@0 1065
aoqi@0 1066 private Type bsmStaticArgToType(Object arg) {
aoqi@0 1067 Assert.checkNonNull(arg);
aoqi@0 1068 if (arg instanceof ClassSymbol) {
aoqi@0 1069 return syms.classType;
aoqi@0 1070 } else if (arg instanceof Integer) {
aoqi@0 1071 return syms.intType;
aoqi@0 1072 } else if (arg instanceof Long) {
aoqi@0 1073 return syms.longType;
aoqi@0 1074 } else if (arg instanceof Float) {
aoqi@0 1075 return syms.floatType;
aoqi@0 1076 } else if (arg instanceof Double) {
aoqi@0 1077 return syms.doubleType;
aoqi@0 1078 } else if (arg instanceof String) {
aoqi@0 1079 return syms.stringType;
aoqi@0 1080 } else if (arg instanceof Pool.MethodHandle) {
aoqi@0 1081 return syms.methodHandleType;
aoqi@0 1082 } else if (arg instanceof MethodType) {
aoqi@0 1083 return syms.methodTypeType;
aoqi@0 1084 } else {
aoqi@0 1085 Assert.error("bad static arg " + arg.getClass());
aoqi@0 1086 return null;
aoqi@0 1087 }
aoqi@0 1088 }
aoqi@0 1089
aoqi@0 1090 /**
aoqi@0 1091 * Get the opcode associated with this method reference
aoqi@0 1092 */
aoqi@0 1093 private int referenceKind(Symbol refSym) {
aoqi@0 1094 if (refSym.isConstructor()) {
aoqi@0 1095 return ClassFile.REF_newInvokeSpecial;
aoqi@0 1096 } else {
aoqi@0 1097 if (refSym.isStatic()) {
aoqi@0 1098 return ClassFile.REF_invokeStatic;
aoqi@0 1099 } else if ((refSym.flags() & PRIVATE) != 0) {
aoqi@0 1100 return ClassFile.REF_invokeSpecial;
aoqi@0 1101 } else if (refSym.enclClass().isInterface()) {
aoqi@0 1102 return ClassFile.REF_invokeInterface;
aoqi@0 1103 } else {
aoqi@0 1104 return ClassFile.REF_invokeVirtual;
aoqi@0 1105 }
aoqi@0 1106 }
aoqi@0 1107 }
aoqi@0 1108
aoqi@0 1109 // <editor-fold defaultstate="collapsed" desc="Lambda/reference analyzer">
aoqi@0 1110 /**
aoqi@0 1111 * This visitor collects information about translation of a lambda expression.
aoqi@0 1112 * More specifically, it keeps track of the enclosing contexts and captured locals
aoqi@0 1113 * accessed by the lambda being translated (as well as other useful info).
aoqi@0 1114 * It also translates away problems for LambdaToMethod.
aoqi@0 1115 */
aoqi@0 1116 class LambdaAnalyzerPreprocessor extends TreeTranslator {
aoqi@0 1117
aoqi@0 1118 /** the frame stack - used to reconstruct translation info about enclosing scopes */
aoqi@0 1119 private List<Frame> frameStack;
aoqi@0 1120
aoqi@0 1121 /**
aoqi@0 1122 * keep the count of lambda expression (used to generate unambiguous
aoqi@0 1123 * names)
aoqi@0 1124 */
aoqi@0 1125 private int lambdaCount = 0;
aoqi@0 1126
aoqi@0 1127 /**
aoqi@0 1128 * keep the count of lambda expression defined in given context (used to
aoqi@0 1129 * generate unambiguous names for serializable lambdas)
aoqi@0 1130 */
aoqi@0 1131 private class SyntheticMethodNameCounter {
aoqi@0 1132 private Map<String, Integer> map = new HashMap<>();
aoqi@0 1133 int getIndex(StringBuilder buf) {
aoqi@0 1134 String temp = buf.toString();
aoqi@0 1135 Integer count = map.get(temp);
aoqi@0 1136 if (count == null) {
aoqi@0 1137 count = 0;
aoqi@0 1138 }
aoqi@0 1139 ++count;
aoqi@0 1140 map.put(temp, count);
aoqi@0 1141 return count;
aoqi@0 1142 }
aoqi@0 1143 }
aoqi@0 1144 private SyntheticMethodNameCounter syntheticMethodNameCounts =
aoqi@0 1145 new SyntheticMethodNameCounter();
aoqi@0 1146
aoqi@0 1147 private Map<Symbol, JCClassDecl> localClassDefs;
aoqi@0 1148
aoqi@0 1149 /**
aoqi@0 1150 * maps for fake clinit symbols to be used as owners of lambda occurring in
aoqi@0 1151 * a static var init context
aoqi@0 1152 */
aoqi@0 1153 private Map<ClassSymbol, Symbol> clinits =
aoqi@0 1154 new HashMap<ClassSymbol, Symbol>();
aoqi@0 1155
aoqi@0 1156 private JCClassDecl analyzeAndPreprocessClass(JCClassDecl tree) {
aoqi@0 1157 frameStack = List.nil();
aoqi@0 1158 localClassDefs = new HashMap<Symbol, JCClassDecl>();
aoqi@0 1159 return translate(tree);
aoqi@0 1160 }
aoqi@0 1161
aoqi@0 1162 @Override
aoqi@0 1163 public void visitBlock(JCBlock tree) {
aoqi@0 1164 List<Frame> prevStack = frameStack;
aoqi@0 1165 try {
aoqi@0 1166 if (frameStack.nonEmpty() && frameStack.head.tree.hasTag(CLASSDEF)) {
aoqi@0 1167 frameStack = frameStack.prepend(new Frame(tree));
aoqi@0 1168 }
aoqi@0 1169 super.visitBlock(tree);
aoqi@0 1170 }
aoqi@0 1171 finally {
aoqi@0 1172 frameStack = prevStack;
aoqi@0 1173 }
aoqi@0 1174 }
aoqi@0 1175
aoqi@0 1176 @Override
aoqi@0 1177 public void visitClassDef(JCClassDecl tree) {
aoqi@0 1178 List<Frame> prevStack = frameStack;
aoqi@0 1179 SyntheticMethodNameCounter prevSyntheticMethodNameCounts =
aoqi@0 1180 syntheticMethodNameCounts;
aoqi@0 1181 Map<ClassSymbol, Symbol> prevClinits = clinits;
aoqi@0 1182 DiagnosticSource prevSource = log.currentSource();
aoqi@0 1183 try {
aoqi@0 1184 log.useSource(tree.sym.sourcefile);
aoqi@0 1185 syntheticMethodNameCounts = new SyntheticMethodNameCounter();
aoqi@0 1186 prevClinits = new HashMap<ClassSymbol, Symbol>();
aoqi@0 1187 if (tree.sym.owner.kind == MTH) {
aoqi@0 1188 localClassDefs.put(tree.sym, tree);
aoqi@0 1189 }
aoqi@0 1190 if (directlyEnclosingLambda() != null) {
aoqi@0 1191 tree.sym.owner = owner();
aoqi@0 1192 if (tree.sym.hasOuterInstance()) {
aoqi@0 1193 //if a class is defined within a lambda, the lambda must capture
aoqi@0 1194 //its enclosing instance (if any)
aoqi@0 1195 TranslationContext<?> localContext = context();
aoqi@0 1196 while (localContext != null) {
aoqi@0 1197 if (localContext.tree.getTag() == LAMBDA) {
aoqi@0 1198 ((LambdaTranslationContext)localContext)
aoqi@0 1199 .addSymbol(tree.sym.type.getEnclosingType().tsym, CAPTURED_THIS);
aoqi@0 1200 }
aoqi@0 1201 localContext = localContext.prev;
aoqi@0 1202 }
aoqi@0 1203 }
aoqi@0 1204 }
aoqi@0 1205 frameStack = frameStack.prepend(new Frame(tree));
aoqi@0 1206 super.visitClassDef(tree);
aoqi@0 1207 }
aoqi@0 1208 finally {
aoqi@0 1209 log.useSource(prevSource.getFile());
aoqi@0 1210 frameStack = prevStack;
aoqi@0 1211 syntheticMethodNameCounts = prevSyntheticMethodNameCounts;
aoqi@0 1212 clinits = prevClinits;
aoqi@0 1213 }
aoqi@0 1214 }
aoqi@0 1215
aoqi@0 1216 @Override
aoqi@0 1217 public void visitIdent(JCIdent tree) {
aoqi@0 1218 if (context() != null && lambdaIdentSymbolFilter(tree.sym)) {
aoqi@0 1219 if (tree.sym.kind == VAR &&
aoqi@0 1220 tree.sym.owner.kind == MTH &&
aoqi@0 1221 tree.type.constValue() == null) {
aoqi@0 1222 TranslationContext<?> localContext = context();
aoqi@0 1223 while (localContext != null) {
aoqi@0 1224 if (localContext.tree.getTag() == LAMBDA) {
aoqi@0 1225 JCTree block = capturedDecl(localContext.depth, tree.sym);
aoqi@0 1226 if (block == null) break;
aoqi@0 1227 ((LambdaTranslationContext)localContext)
aoqi@0 1228 .addSymbol(tree.sym, CAPTURED_VAR);
aoqi@0 1229 }
aoqi@0 1230 localContext = localContext.prev;
aoqi@0 1231 }
aoqi@0 1232 } else if (tree.sym.owner.kind == TYP) {
aoqi@0 1233 TranslationContext<?> localContext = context();
aoqi@0 1234 while (localContext != null) {
aoqi@0 1235 if (localContext.tree.hasTag(LAMBDA)) {
aoqi@0 1236 JCTree block = capturedDecl(localContext.depth, tree.sym);
aoqi@0 1237 if (block == null) break;
aoqi@0 1238 switch (block.getTag()) {
aoqi@0 1239 case CLASSDEF:
aoqi@0 1240 JCClassDecl cdecl = (JCClassDecl)block;
aoqi@0 1241 ((LambdaTranslationContext)localContext)
aoqi@0 1242 .addSymbol(cdecl.sym, CAPTURED_THIS);
aoqi@0 1243 break;
aoqi@0 1244 default:
aoqi@0 1245 Assert.error("bad block kind");
aoqi@0 1246 }
aoqi@0 1247 }
aoqi@0 1248 localContext = localContext.prev;
aoqi@0 1249 }
aoqi@0 1250 }
aoqi@0 1251 }
aoqi@0 1252 super.visitIdent(tree);
aoqi@0 1253 }
aoqi@0 1254
aoqi@0 1255 @Override
aoqi@0 1256 public void visitLambda(JCLambda tree) {
aoqi@0 1257 List<Frame> prevStack = frameStack;
aoqi@0 1258 try {
aoqi@0 1259 LambdaTranslationContext context = (LambdaTranslationContext)makeLambdaContext(tree);
aoqi@0 1260 frameStack = frameStack.prepend(new Frame(tree));
aoqi@0 1261 for (JCVariableDecl param : tree.params) {
aoqi@0 1262 context.addSymbol(param.sym, PARAM);
aoqi@0 1263 frameStack.head.addLocal(param.sym);
aoqi@0 1264 }
aoqi@0 1265 contextMap.put(tree, context);
aoqi@0 1266 super.visitLambda(tree);
aoqi@0 1267 context.complete();
aoqi@0 1268 }
aoqi@0 1269 finally {
aoqi@0 1270 frameStack = prevStack;
aoqi@0 1271 }
aoqi@0 1272 }
aoqi@0 1273
aoqi@0 1274 @Override
aoqi@0 1275 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 1276 List<Frame> prevStack = frameStack;
aoqi@0 1277 try {
aoqi@0 1278 frameStack = frameStack.prepend(new Frame(tree));
aoqi@0 1279 super.visitMethodDef(tree);
aoqi@0 1280 }
aoqi@0 1281 finally {
aoqi@0 1282 frameStack = prevStack;
aoqi@0 1283 }
aoqi@0 1284 }
aoqi@0 1285
aoqi@0 1286 @Override
aoqi@0 1287 public void visitNewClass(JCNewClass tree) {
aoqi@0 1288 TypeSymbol def = tree.type.tsym;
aoqi@0 1289 boolean inReferencedClass = currentlyInClass(def);
aoqi@0 1290 boolean isLocal = def.isLocal();
aoqi@0 1291 if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) {
aoqi@0 1292 TranslationContext<?> localContext = context();
aoqi@0 1293 while (localContext != null) {
aoqi@0 1294 if (localContext.tree.getTag() == LAMBDA) {
aoqi@0 1295 ((LambdaTranslationContext)localContext)
aoqi@0 1296 .addSymbol(tree.type.getEnclosingType().tsym, CAPTURED_THIS);
aoqi@0 1297 }
aoqi@0 1298 localContext = localContext.prev;
aoqi@0 1299 }
aoqi@0 1300 }
aoqi@0 1301 if (context() != null && !inReferencedClass && isLocal) {
aoqi@0 1302 LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context();
aoqi@0 1303 captureLocalClassDefs(def, lambdaContext);
aoqi@0 1304 }
aoqi@0 1305 super.visitNewClass(tree);
aoqi@0 1306 }
aoqi@0 1307 //where
aoqi@0 1308 void captureLocalClassDefs(Symbol csym, final LambdaTranslationContext lambdaContext) {
aoqi@0 1309 JCClassDecl localCDef = localClassDefs.get(csym);
aoqi@0 1310 if (localCDef != null && lambdaContext.freeVarProcessedLocalClasses.add(csym)) {
aoqi@0 1311 BasicFreeVarCollector fvc = lower.new BasicFreeVarCollector() {
aoqi@0 1312 @Override
aoqi@0 1313 void addFreeVars(ClassSymbol c) {
aoqi@0 1314 captureLocalClassDefs(c, lambdaContext);
aoqi@0 1315 }
aoqi@0 1316 @Override
aoqi@0 1317 void visitSymbol(Symbol sym) {
aoqi@0 1318 if (sym.kind == VAR &&
aoqi@0 1319 sym.owner.kind == MTH &&
aoqi@0 1320 ((VarSymbol)sym).getConstValue() == null) {
aoqi@0 1321 TranslationContext<?> localContext = context();
aoqi@0 1322 while (localContext != null) {
aoqi@0 1323 if (localContext.tree.getTag() == LAMBDA) {
aoqi@0 1324 JCTree block = capturedDecl(localContext.depth, sym);
aoqi@0 1325 if (block == null) break;
aoqi@0 1326 ((LambdaTranslationContext)localContext).addSymbol(sym, CAPTURED_VAR);
aoqi@0 1327 }
aoqi@0 1328 localContext = localContext.prev;
aoqi@0 1329 }
aoqi@0 1330 }
aoqi@0 1331 }
aoqi@0 1332 };
aoqi@0 1333 fvc.scan(localCDef);
aoqi@0 1334 }
aoqi@0 1335 }
aoqi@0 1336 //where
aoqi@0 1337 boolean currentlyInClass(Symbol csym) {
aoqi@0 1338 for (Frame frame : frameStack) {
aoqi@0 1339 if (frame.tree.hasTag(JCTree.Tag.CLASSDEF)) {
aoqi@0 1340 JCClassDecl cdef = (JCClassDecl) frame.tree;
aoqi@0 1341 if (cdef.sym == csym) {
aoqi@0 1342 return true;
aoqi@0 1343 }
aoqi@0 1344 }
aoqi@0 1345 }
aoqi@0 1346 return false;
aoqi@0 1347 }
aoqi@0 1348
aoqi@0 1349 /**
aoqi@0 1350 * Method references to local class constructors, may, if the local
aoqi@0 1351 * class references local variables, have implicit constructor
aoqi@0 1352 * parameters added in Lower; As a result, the invokedynamic bootstrap
aoqi@0 1353 * information added in the LambdaToMethod pass will have the wrong
aoqi@0 1354 * signature. Hooks between Lower and LambdaToMethod have been added to
aoqi@0 1355 * handle normal "new" in this case. This visitor converts potentially
aoqi@0 1356 * effected method references into a lambda containing a normal "new" of
aoqi@0 1357 * the class.
aoqi@0 1358 *
aoqi@0 1359 * @param tree
aoqi@0 1360 */
aoqi@0 1361 @Override
aoqi@0 1362 public void visitReference(JCMemberReference tree) {
aoqi@0 1363 if (tree.getMode() == ReferenceMode.NEW
aoqi@0 1364 && tree.kind != ReferenceKind.ARRAY_CTOR
aoqi@0 1365 && tree.sym.owner.isLocal()) {
aoqi@0 1366 MethodSymbol consSym = (MethodSymbol) tree.sym;
aoqi@0 1367 List<Type> ptypes = ((MethodType) consSym.type).getParameterTypes();
aoqi@0 1368 Type classType = consSym.owner.type;
aoqi@0 1369
aoqi@0 1370 // Build lambda parameters
aoqi@0 1371 // partially cloned from TreeMaker.Params until 8014021 is fixed
aoqi@0 1372 Symbol owner = owner();
aoqi@0 1373 ListBuffer<JCVariableDecl> paramBuff = new ListBuffer<JCVariableDecl>();
aoqi@0 1374 int i = 0;
aoqi@0 1375 for (List<Type> l = ptypes; l.nonEmpty(); l = l.tail) {
aoqi@0 1376 JCVariableDecl param = make.Param(make.paramName(i++), l.head, owner);
aoqi@0 1377 param.sym.pos = tree.pos;
aoqi@0 1378 paramBuff.append(param);
aoqi@0 1379 }
aoqi@0 1380 List<JCVariableDecl> params = paramBuff.toList();
aoqi@0 1381
aoqi@0 1382 // Make new-class call
aoqi@0 1383 JCNewClass nc = makeNewClass(classType, make.Idents(params));
aoqi@0 1384 nc.pos = tree.pos;
aoqi@0 1385
aoqi@0 1386 // Make lambda holding the new-class call
aoqi@0 1387 JCLambda slam = make.Lambda(params, nc);
aoqi@0 1388 slam.targets = tree.targets;
aoqi@0 1389 slam.type = tree.type;
aoqi@0 1390 slam.pos = tree.pos;
aoqi@0 1391
aoqi@0 1392 // Now it is a lambda, process as such
aoqi@0 1393 visitLambda(slam);
aoqi@0 1394 } else {
aoqi@0 1395 super.visitReference(tree);
aoqi@0 1396 contextMap.put(tree, makeReferenceContext(tree));
aoqi@0 1397 }
aoqi@0 1398 }
aoqi@0 1399
aoqi@0 1400 @Override
aoqi@0 1401 public void visitSelect(JCFieldAccess tree) {
aoqi@0 1402 if (context() != null && tree.sym.kind == VAR &&
aoqi@0 1403 (tree.sym.name == names._this ||
aoqi@0 1404 tree.sym.name == names._super)) {
aoqi@0 1405 // A select of this or super means, if we are in a lambda,
aoqi@0 1406 // we much have an instance context
aoqi@0 1407 TranslationContext<?> localContext = context();
aoqi@0 1408 while (localContext != null) {
aoqi@0 1409 if (localContext.tree.hasTag(LAMBDA)) {
aoqi@0 1410 JCClassDecl clazz = (JCClassDecl)capturedDecl(localContext.depth, tree.sym);
aoqi@0 1411 if (clazz == null) break;
aoqi@0 1412 ((LambdaTranslationContext)localContext).addSymbol(clazz.sym, CAPTURED_THIS);
aoqi@0 1413 }
aoqi@0 1414 localContext = localContext.prev;
aoqi@0 1415 }
aoqi@0 1416 }
aoqi@0 1417 super.visitSelect(tree);
aoqi@0 1418 }
aoqi@0 1419
aoqi@0 1420 @Override
aoqi@0 1421 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 1422 TranslationContext<?> context = context();
aoqi@0 1423 LambdaTranslationContext ltc = (context != null && context instanceof LambdaTranslationContext)?
aoqi@0 1424 (LambdaTranslationContext)context :
aoqi@0 1425 null;
aoqi@0 1426 if (ltc != null) {
aoqi@0 1427 if (frameStack.head.tree.hasTag(LAMBDA)) {
aoqi@0 1428 ltc.addSymbol(tree.sym, LOCAL_VAR);
aoqi@0 1429 }
aoqi@0 1430 // Check for type variables (including as type arguments).
aoqi@0 1431 // If they occur within class nested in a lambda, mark for erasure
aoqi@0 1432 Type type = tree.sym.asType();
aoqi@0 1433 if (inClassWithinLambda() && !types.isSameType(types.erasure(type), type)) {
aoqi@0 1434 ltc.addSymbol(tree.sym, TYPE_VAR);
aoqi@0 1435 }
aoqi@0 1436 }
aoqi@0 1437
aoqi@0 1438 List<Frame> prevStack = frameStack;
aoqi@0 1439 try {
aoqi@0 1440 if (tree.sym.owner.kind == MTH) {
aoqi@0 1441 frameStack.head.addLocal(tree.sym);
aoqi@0 1442 }
aoqi@0 1443 frameStack = frameStack.prepend(new Frame(tree));
aoqi@0 1444 super.visitVarDef(tree);
aoqi@0 1445 }
aoqi@0 1446 finally {
aoqi@0 1447 frameStack = prevStack;
aoqi@0 1448 }
aoqi@0 1449 }
aoqi@0 1450
aoqi@0 1451 /**
aoqi@0 1452 * Return a valid owner given the current declaration stack
aoqi@0 1453 * (required to skip synthetic lambda symbols)
aoqi@0 1454 */
aoqi@0 1455 private Symbol owner() {
aoqi@0 1456 return owner(false);
aoqi@0 1457 }
aoqi@0 1458
aoqi@0 1459 @SuppressWarnings("fallthrough")
aoqi@0 1460 private Symbol owner(boolean skipLambda) {
aoqi@0 1461 List<Frame> frameStack2 = frameStack;
aoqi@0 1462 while (frameStack2.nonEmpty()) {
aoqi@0 1463 switch (frameStack2.head.tree.getTag()) {
aoqi@0 1464 case VARDEF:
aoqi@0 1465 if (((JCVariableDecl)frameStack2.head.tree).sym.isLocal()) {
aoqi@0 1466 frameStack2 = frameStack2.tail;
aoqi@0 1467 break;
aoqi@0 1468 }
aoqi@0 1469 JCClassDecl cdecl = (JCClassDecl)frameStack2.tail.head.tree;
aoqi@0 1470 return initSym(cdecl.sym,
aoqi@0 1471 ((JCVariableDecl)frameStack2.head.tree).sym.flags() & STATIC);
aoqi@0 1472 case BLOCK:
aoqi@0 1473 JCClassDecl cdecl2 = (JCClassDecl)frameStack2.tail.head.tree;
aoqi@0 1474 return initSym(cdecl2.sym,
aoqi@0 1475 ((JCBlock)frameStack2.head.tree).flags & STATIC);
aoqi@0 1476 case CLASSDEF:
aoqi@0 1477 return ((JCClassDecl)frameStack2.head.tree).sym;
aoqi@0 1478 case METHODDEF:
aoqi@0 1479 return ((JCMethodDecl)frameStack2.head.tree).sym;
aoqi@0 1480 case LAMBDA:
aoqi@0 1481 if (!skipLambda)
aoqi@0 1482 return ((LambdaTranslationContext)contextMap
aoqi@0 1483 .get(frameStack2.head.tree)).translatedSym;
aoqi@0 1484 default:
aoqi@0 1485 frameStack2 = frameStack2.tail;
aoqi@0 1486 }
aoqi@0 1487 }
aoqi@0 1488 Assert.error();
aoqi@0 1489 return null;
aoqi@0 1490 }
aoqi@0 1491
aoqi@0 1492 private Symbol initSym(ClassSymbol csym, long flags) {
aoqi@0 1493 boolean isStatic = (flags & STATIC) != 0;
aoqi@0 1494 if (isStatic) {
aoqi@0 1495 /* static clinits are generated in Gen, so we need to use a fake
aoqi@0 1496 * one. Attr creates a fake clinit method while attributing
aoqi@0 1497 * lambda expressions used as initializers of static fields, so
aoqi@0 1498 * let's use that one.
aoqi@0 1499 */
aoqi@0 1500 MethodSymbol clinit = attr.removeClinit(csym);
aoqi@0 1501 if (clinit != null) {
aoqi@0 1502 clinits.put(csym, clinit);
aoqi@0 1503 return clinit;
aoqi@0 1504 }
aoqi@0 1505
aoqi@0 1506 /* if no clinit is found at Attr, then let's try at clinits.
aoqi@0 1507 */
aoqi@0 1508 clinit = (MethodSymbol)clinits.get(csym);
aoqi@0 1509 if (clinit == null) {
aoqi@0 1510 /* no luck, let's create a new one
aoqi@0 1511 */
aoqi@0 1512 clinit = makePrivateSyntheticMethod(STATIC,
aoqi@0 1513 names.clinit,
aoqi@0 1514 new MethodType(List.<Type>nil(), syms.voidType,
aoqi@0 1515 List.<Type>nil(), syms.methodClass),
aoqi@0 1516 csym);
aoqi@0 1517 clinits.put(csym, clinit);
aoqi@0 1518 }
aoqi@0 1519 return clinit;
aoqi@0 1520 } else {
aoqi@0 1521 //get the first constructor and treat it as the instance init sym
aoqi@0 1522 for (Symbol s : csym.members_field.getElementsByName(names.init)) {
aoqi@0 1523 return s;
aoqi@0 1524 }
aoqi@0 1525 }
aoqi@0 1526 Assert.error("init not found");
aoqi@0 1527 return null;
aoqi@0 1528 }
aoqi@0 1529
aoqi@0 1530 private JCTree directlyEnclosingLambda() {
aoqi@0 1531 if (frameStack.isEmpty()) {
aoqi@0 1532 return null;
aoqi@0 1533 }
aoqi@0 1534 List<Frame> frameStack2 = frameStack;
aoqi@0 1535 while (frameStack2.nonEmpty()) {
aoqi@0 1536 switch (frameStack2.head.tree.getTag()) {
aoqi@0 1537 case CLASSDEF:
aoqi@0 1538 case METHODDEF:
aoqi@0 1539 return null;
aoqi@0 1540 case LAMBDA:
aoqi@0 1541 return frameStack2.head.tree;
aoqi@0 1542 default:
aoqi@0 1543 frameStack2 = frameStack2.tail;
aoqi@0 1544 }
aoqi@0 1545 }
aoqi@0 1546 Assert.error();
aoqi@0 1547 return null;
aoqi@0 1548 }
aoqi@0 1549
aoqi@0 1550 private boolean inClassWithinLambda() {
aoqi@0 1551 if (frameStack.isEmpty()) {
aoqi@0 1552 return false;
aoqi@0 1553 }
aoqi@0 1554 List<Frame> frameStack2 = frameStack;
aoqi@0 1555 boolean classFound = false;
aoqi@0 1556 while (frameStack2.nonEmpty()) {
aoqi@0 1557 switch (frameStack2.head.tree.getTag()) {
aoqi@0 1558 case LAMBDA:
aoqi@0 1559 return classFound;
aoqi@0 1560 case CLASSDEF:
aoqi@0 1561 classFound = true;
aoqi@0 1562 frameStack2 = frameStack2.tail;
aoqi@0 1563 break;
aoqi@0 1564 default:
aoqi@0 1565 frameStack2 = frameStack2.tail;
aoqi@0 1566 }
aoqi@0 1567 }
aoqi@0 1568 // No lambda
aoqi@0 1569 return false;
aoqi@0 1570 }
aoqi@0 1571
aoqi@0 1572 /**
aoqi@0 1573 * Return the declaration corresponding to a symbol in the enclosing
aoqi@0 1574 * scope; the depth parameter is used to filter out symbols defined
aoqi@0 1575 * in nested scopes (which do not need to undergo capture).
aoqi@0 1576 */
aoqi@0 1577 private JCTree capturedDecl(int depth, Symbol sym) {
aoqi@0 1578 int currentDepth = frameStack.size() - 1;
aoqi@0 1579 for (Frame block : frameStack) {
aoqi@0 1580 switch (block.tree.getTag()) {
aoqi@0 1581 case CLASSDEF:
aoqi@0 1582 ClassSymbol clazz = ((JCClassDecl)block.tree).sym;
aoqi@0 1583 if (sym.isMemberOf(clazz, types)) {
aoqi@0 1584 return currentDepth > depth ? null : block.tree;
aoqi@0 1585 }
aoqi@0 1586 break;
aoqi@0 1587 case VARDEF:
aoqi@0 1588 if (((JCVariableDecl)block.tree).sym == sym &&
aoqi@0 1589 sym.owner.kind == MTH) { //only locals are captured
aoqi@0 1590 return currentDepth > depth ? null : block.tree;
aoqi@0 1591 }
aoqi@0 1592 break;
aoqi@0 1593 case BLOCK:
aoqi@0 1594 case METHODDEF:
aoqi@0 1595 case LAMBDA:
aoqi@0 1596 if (block.locals != null && block.locals.contains(sym)) {
aoqi@0 1597 return currentDepth > depth ? null : block.tree;
aoqi@0 1598 }
aoqi@0 1599 break;
aoqi@0 1600 default:
aoqi@0 1601 Assert.error("bad decl kind " + block.tree.getTag());
aoqi@0 1602 }
aoqi@0 1603 currentDepth--;
aoqi@0 1604 }
aoqi@0 1605 return null;
aoqi@0 1606 }
aoqi@0 1607
aoqi@0 1608 private TranslationContext<?> context() {
aoqi@0 1609 for (Frame frame : frameStack) {
aoqi@0 1610 TranslationContext<?> context = contextMap.get(frame.tree);
aoqi@0 1611 if (context != null) {
aoqi@0 1612 return context;
aoqi@0 1613 }
aoqi@0 1614 }
aoqi@0 1615 return null;
aoqi@0 1616 }
aoqi@0 1617
aoqi@0 1618 /**
aoqi@0 1619 * This is used to filter out those identifiers that needs to be adjusted
aoqi@0 1620 * when translating away lambda expressions
aoqi@0 1621 */
aoqi@0 1622 private boolean lambdaIdentSymbolFilter(Symbol sym) {
aoqi@0 1623 return (sym.kind == VAR || sym.kind == MTH)
aoqi@0 1624 && !sym.isStatic()
aoqi@0 1625 && sym.name != names.init;
aoqi@0 1626 }
aoqi@0 1627
aoqi@0 1628 /**
aoqi@0 1629 * This is used to filter out those new class expressions that need to
aoqi@0 1630 * be qualified with an enclosing tree
aoqi@0 1631 */
aoqi@0 1632 private boolean lambdaNewClassFilter(TranslationContext<?> context, JCNewClass tree) {
aoqi@0 1633 if (context != null
aoqi@0 1634 && tree.encl == null
aoqi@0 1635 && tree.def == null
aoqi@0 1636 && !tree.type.getEnclosingType().hasTag(NONE)) {
aoqi@0 1637 Type encl = tree.type.getEnclosingType();
aoqi@0 1638 Type current = context.owner.enclClass().type;
aoqi@0 1639 while (!current.hasTag(NONE)) {
aoqi@0 1640 if (current.tsym.isSubClass(encl.tsym, types)) {
aoqi@0 1641 return true;
aoqi@0 1642 }
aoqi@0 1643 current = current.getEnclosingType();
aoqi@0 1644 }
aoqi@0 1645 return false;
aoqi@0 1646 } else {
aoqi@0 1647 return false;
aoqi@0 1648 }
aoqi@0 1649 }
aoqi@0 1650
aoqi@0 1651 private TranslationContext<JCLambda> makeLambdaContext(JCLambda tree) {
aoqi@0 1652 return new LambdaTranslationContext(tree);
aoqi@0 1653 }
aoqi@0 1654
aoqi@0 1655 private TranslationContext<JCMemberReference> makeReferenceContext(JCMemberReference tree) {
aoqi@0 1656 return new ReferenceTranslationContext(tree);
aoqi@0 1657 }
aoqi@0 1658
aoqi@0 1659 private class Frame {
aoqi@0 1660 final JCTree tree;
aoqi@0 1661 List<Symbol> locals;
aoqi@0 1662
aoqi@0 1663 public Frame(JCTree tree) {
aoqi@0 1664 this.tree = tree;
aoqi@0 1665 }
aoqi@0 1666
aoqi@0 1667 void addLocal(Symbol sym) {
aoqi@0 1668 if (locals == null) {
aoqi@0 1669 locals = List.nil();
aoqi@0 1670 }
aoqi@0 1671 locals = locals.prepend(sym);
aoqi@0 1672 }
aoqi@0 1673 }
aoqi@0 1674
aoqi@0 1675 /**
aoqi@0 1676 * This class is used to store important information regarding translation of
aoqi@0 1677 * lambda expression/method references (see subclasses).
aoqi@0 1678 */
aoqi@0 1679 private abstract class TranslationContext<T extends JCFunctionalExpression> {
aoqi@0 1680
aoqi@0 1681 /** the underlying (untranslated) tree */
aoqi@0 1682 final T tree;
aoqi@0 1683
aoqi@0 1684 /** points to the adjusted enclosing scope in which this lambda/mref expression occurs */
aoqi@0 1685 final Symbol owner;
aoqi@0 1686
aoqi@0 1687 /** the depth of this lambda expression in the frame stack */
aoqi@0 1688 final int depth;
aoqi@0 1689
aoqi@0 1690 /** the enclosing translation context (set for nested lambdas/mref) */
aoqi@0 1691 final TranslationContext<?> prev;
aoqi@0 1692
aoqi@0 1693 /** list of methods to be bridged by the meta-factory */
aoqi@0 1694 final List<Symbol> bridges;
aoqi@0 1695
aoqi@0 1696 TranslationContext(T tree) {
aoqi@0 1697 this.tree = tree;
aoqi@0 1698 this.owner = owner();
aoqi@0 1699 this.depth = frameStack.size() - 1;
aoqi@0 1700 this.prev = context();
aoqi@0 1701 ClassSymbol csym =
aoqi@0 1702 types.makeFunctionalInterfaceClass(attrEnv, names.empty, tree.targets, ABSTRACT | INTERFACE);
aoqi@0 1703 this.bridges = types.functionalInterfaceBridges(csym);
aoqi@0 1704 }
aoqi@0 1705
aoqi@0 1706 /** does this functional expression need to be created using alternate metafactory? */
aoqi@0 1707 boolean needsAltMetafactory() {
aoqi@0 1708 return tree.targets.length() > 1 ||
aoqi@0 1709 isSerializable() ||
aoqi@0 1710 bridges.length() > 1;
aoqi@0 1711 }
aoqi@0 1712
aoqi@0 1713 /** does this functional expression require serialization support? */
aoqi@0 1714 boolean isSerializable() {
aoqi@0 1715 if (forceSerializable) {
aoqi@0 1716 return true;
aoqi@0 1717 }
aoqi@0 1718 for (Type target : tree.targets) {
aoqi@0 1719 if (types.asSuper(target, syms.serializableType.tsym) != null) {
aoqi@0 1720 return true;
aoqi@0 1721 }
aoqi@0 1722 }
aoqi@0 1723 return false;
aoqi@0 1724 }
aoqi@0 1725
aoqi@0 1726 /**
aoqi@0 1727 * @return Name of the enclosing method to be folded into synthetic
aoqi@0 1728 * method name
aoqi@0 1729 */
aoqi@0 1730 String enclosingMethodName() {
aoqi@0 1731 return syntheticMethodNameComponent(owner.name);
aoqi@0 1732 }
aoqi@0 1733
aoqi@0 1734 /**
aoqi@0 1735 * @return Method name in a form that can be folded into a
aoqi@0 1736 * component of a synthetic method name
aoqi@0 1737 */
aoqi@0 1738 String syntheticMethodNameComponent(Name name) {
aoqi@0 1739 if (name == null) {
aoqi@0 1740 return "null";
aoqi@0 1741 }
aoqi@0 1742 String methodName = name.toString();
aoqi@0 1743 if (methodName.equals("<clinit>")) {
aoqi@0 1744 methodName = "static";
aoqi@0 1745 } else if (methodName.equals("<init>")) {
aoqi@0 1746 methodName = "new";
aoqi@0 1747 }
aoqi@0 1748 return methodName;
aoqi@0 1749 }
aoqi@0 1750 }
aoqi@0 1751
aoqi@0 1752 /**
aoqi@0 1753 * This class retains all the useful information about a lambda expression;
aoqi@0 1754 * the contents of this class are filled by the LambdaAnalyzer visitor,
aoqi@0 1755 * and the used by the main translation routines in order to adjust references
aoqi@0 1756 * to captured locals/members, etc.
aoqi@0 1757 */
aoqi@0 1758 private class LambdaTranslationContext extends TranslationContext<JCLambda> {
aoqi@0 1759
aoqi@0 1760 /** variable in the enclosing context to which this lambda is assigned */
aoqi@0 1761 final Symbol self;
aoqi@0 1762
aoqi@0 1763 /** variable in the enclosing context to which this lambda is assigned */
aoqi@0 1764 final Symbol assignedTo;
aoqi@0 1765
aoqi@0 1766 Map<LambdaSymbolKind, Map<Symbol, Symbol>> translatedSymbols;
aoqi@0 1767
aoqi@0 1768 /** the synthetic symbol for the method hoisting the translated lambda */
aoqi@0 1769 Symbol translatedSym;
aoqi@0 1770
aoqi@0 1771 List<JCVariableDecl> syntheticParams;
aoqi@0 1772
aoqi@0 1773 /**
aoqi@0 1774 * to prevent recursion, track local classes processed
aoqi@0 1775 */
aoqi@0 1776 final Set<Symbol> freeVarProcessedLocalClasses;
aoqi@0 1777
aoqi@0 1778 LambdaTranslationContext(JCLambda tree) {
aoqi@0 1779 super(tree);
aoqi@0 1780 Frame frame = frameStack.head;
aoqi@0 1781 switch (frame.tree.getTag()) {
aoqi@0 1782 case VARDEF:
aoqi@0 1783 assignedTo = self = ((JCVariableDecl) frame.tree).sym;
aoqi@0 1784 break;
aoqi@0 1785 case ASSIGN:
aoqi@0 1786 self = null;
aoqi@0 1787 assignedTo = TreeInfo.symbol(((JCAssign) frame.tree).getVariable());
aoqi@0 1788 break;
aoqi@0 1789 default:
aoqi@0 1790 assignedTo = self = null;
aoqi@0 1791 break;
aoqi@0 1792 }
aoqi@0 1793
aoqi@0 1794 // This symbol will be filled-in in complete
aoqi@0 1795 this.translatedSym = makePrivateSyntheticMethod(0, null, null, owner.enclClass());
aoqi@0 1796
aoqi@0 1797 if (dumpLambdaToMethodStats) {
aoqi@0 1798 log.note(tree, "lambda.stat", needsAltMetafactory(), translatedSym);
aoqi@0 1799 }
aoqi@0 1800 translatedSymbols = new EnumMap<>(LambdaSymbolKind.class);
aoqi@0 1801
aoqi@0 1802 translatedSymbols.put(PARAM, new LinkedHashMap<Symbol, Symbol>());
aoqi@0 1803 translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>());
aoqi@0 1804 translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>());
aoqi@0 1805 translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>());
aoqi@0 1806 translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>());
aoqi@0 1807
aoqi@0 1808 freeVarProcessedLocalClasses = new HashSet<>();
aoqi@0 1809 }
aoqi@0 1810
aoqi@0 1811 /**
aoqi@0 1812 * For a serializable lambda, generate a disambiguating string
aoqi@0 1813 * which maximizes stability across deserialization.
aoqi@0 1814 *
aoqi@0 1815 * @return String to differentiate synthetic lambda method names
aoqi@0 1816 */
aoqi@0 1817 private String serializedLambdaDisambiguation() {
aoqi@0 1818 StringBuilder buf = new StringBuilder();
aoqi@0 1819 // Append the enclosing method signature to differentiate
aoqi@0 1820 // overloaded enclosing methods. For lambdas enclosed in
aoqi@0 1821 // lambdas, the generated lambda method will not have type yet,
aoqi@0 1822 // but the enclosing method's name will have been generated
aoqi@0 1823 // with this same method, so it will be unique and never be
aoqi@0 1824 // overloaded.
aoqi@0 1825 Assert.check(
aoqi@0 1826 owner.type != null ||
aoqi@0 1827 directlyEnclosingLambda() != null);
aoqi@0 1828 if (owner.type != null) {
aoqi@0 1829 buf.append(typeSig(owner.type));
aoqi@0 1830 buf.append(":");
aoqi@0 1831 }
aoqi@0 1832
aoqi@0 1833 // Add target type info
aoqi@0 1834 buf.append(types.findDescriptorSymbol(tree.type.tsym).owner.flatName());
aoqi@0 1835 buf.append(" ");
aoqi@0 1836
aoqi@0 1837 // Add variable assigned to
aoqi@0 1838 if (assignedTo != null) {
aoqi@0 1839 buf.append(assignedTo.flatName());
aoqi@0 1840 buf.append("=");
aoqi@0 1841 }
aoqi@0 1842 //add captured locals info: type, name, order
aoqi@0 1843 for (Symbol fv : getSymbolMap(CAPTURED_VAR).keySet()) {
aoqi@0 1844 if (fv != self) {
aoqi@0 1845 buf.append(typeSig(fv.type));
aoqi@0 1846 buf.append(" ");
aoqi@0 1847 buf.append(fv.flatName());
aoqi@0 1848 buf.append(",");
aoqi@0 1849 }
aoqi@0 1850 }
aoqi@0 1851
aoqi@0 1852 return buf.toString();
aoqi@0 1853 }
aoqi@0 1854
aoqi@0 1855 /**
aoqi@0 1856 * For a non-serializable lambda, generate a simple method.
aoqi@0 1857 *
aoqi@0 1858 * @return Name to use for the synthetic lambda method name
aoqi@0 1859 */
aoqi@0 1860 private Name lambdaName() {
aoqi@0 1861 return names.lambda.append(names.fromString(enclosingMethodName() + "$" + lambdaCount++));
aoqi@0 1862 }
aoqi@0 1863
aoqi@0 1864 /**
aoqi@0 1865 * For a serializable lambda, generate a method name which maximizes
aoqi@0 1866 * name stability across deserialization.
aoqi@0 1867 *
aoqi@0 1868 * @return Name to use for the synthetic lambda method name
aoqi@0 1869 */
aoqi@0 1870 private Name serializedLambdaName() {
aoqi@0 1871 StringBuilder buf = new StringBuilder();
aoqi@0 1872 buf.append(names.lambda);
aoqi@0 1873 // Append the name of the method enclosing the lambda.
aoqi@0 1874 buf.append(enclosingMethodName());
aoqi@0 1875 buf.append('$');
aoqi@0 1876 // Append a hash of the disambiguating string : enclosing method
aoqi@0 1877 // signature, etc.
aoqi@0 1878 String disam = serializedLambdaDisambiguation();
aoqi@0 1879 buf.append(Integer.toHexString(disam.hashCode()));
aoqi@0 1880 buf.append('$');
aoqi@0 1881 // The above appended name components may not be unique, append
aoqi@0 1882 // a count based on the above name components.
aoqi@0 1883 buf.append(syntheticMethodNameCounts.getIndex(buf));
aoqi@0 1884 String result = buf.toString();
aoqi@0 1885 //System.err.printf("serializedLambdaName: %s -- %s\n", result, disam);
aoqi@0 1886 return names.fromString(result);
aoqi@0 1887 }
aoqi@0 1888
aoqi@0 1889 /**
aoqi@0 1890 * Translate a symbol of a given kind into something suitable for the
aoqi@0 1891 * synthetic lambda body
aoqi@0 1892 */
aoqi@0 1893 Symbol translate(Name name, final Symbol sym, LambdaSymbolKind skind) {
aoqi@0 1894 Symbol ret;
aoqi@0 1895 switch (skind) {
aoqi@0 1896 case CAPTURED_THIS:
aoqi@0 1897 ret = sym; // self represented
aoqi@0 1898 break;
aoqi@0 1899 case TYPE_VAR:
aoqi@0 1900 // Just erase the type var
aoqi@0 1901 ret = new VarSymbol(sym.flags(), name,
aoqi@0 1902 types.erasure(sym.type), sym.owner);
aoqi@0 1903
aoqi@0 1904 /* this information should also be kept for LVT generation at Gen
aoqi@0 1905 * a Symbol with pos < startPos won't be tracked.
aoqi@0 1906 */
aoqi@0 1907 ((VarSymbol)ret).pos = ((VarSymbol)sym).pos;
aoqi@0 1908 break;
aoqi@0 1909 case CAPTURED_VAR:
aoqi@0 1910 ret = new VarSymbol(SYNTHETIC | FINAL | PARAMETER, name, types.erasure(sym.type), translatedSym) {
aoqi@0 1911 @Override
aoqi@0 1912 public Symbol baseSymbol() {
aoqi@0 1913 //keep mapping with original captured symbol
aoqi@0 1914 return sym;
aoqi@0 1915 }
aoqi@0 1916 };
aoqi@0 1917 break;
aoqi@0 1918 case LOCAL_VAR:
aoqi@0 1919 ret = new VarSymbol(sym.flags() & FINAL, name, sym.type, translatedSym);
aoqi@0 1920 ((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
aoqi@0 1921 break;
aoqi@0 1922 case PARAM:
aoqi@0 1923 ret = new VarSymbol((sym.flags() & FINAL) | PARAMETER, name, types.erasure(sym.type), translatedSym);
aoqi@0 1924 ((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
aoqi@0 1925 break;
aoqi@0 1926 default:
aoqi@0 1927 ret = makeSyntheticVar(FINAL, name, types.erasure(sym.type), translatedSym);
aoqi@0 1928 ((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
aoqi@0 1929 }
aoqi@0 1930 if (ret != sym) {
aoqi@0 1931 ret.setDeclarationAttributes(sym.getRawAttributes());
aoqi@0 1932 ret.setTypeAttributes(sym.getRawTypeAttributes());
aoqi@0 1933 }
aoqi@0 1934 return ret;
aoqi@0 1935 }
aoqi@0 1936
aoqi@0 1937 void addSymbol(Symbol sym, LambdaSymbolKind skind) {
aoqi@0 1938 Map<Symbol, Symbol> transMap = getSymbolMap(skind);
aoqi@0 1939 Name preferredName;
aoqi@0 1940 switch (skind) {
aoqi@0 1941 case CAPTURED_THIS:
aoqi@0 1942 preferredName = names.fromString("encl$" + transMap.size());
aoqi@0 1943 break;
aoqi@0 1944 case CAPTURED_VAR:
aoqi@0 1945 preferredName = names.fromString("cap$" + transMap.size());
aoqi@0 1946 break;
aoqi@0 1947 case LOCAL_VAR:
aoqi@0 1948 preferredName = sym.name;
aoqi@0 1949 break;
aoqi@0 1950 case PARAM:
aoqi@0 1951 preferredName = sym.name;
aoqi@0 1952 break;
aoqi@0 1953 case TYPE_VAR:
aoqi@0 1954 preferredName = sym.name;
aoqi@0 1955 break;
aoqi@0 1956 default: throw new AssertionError();
aoqi@0 1957 }
aoqi@0 1958 if (!transMap.containsKey(sym)) {
aoqi@0 1959 transMap.put(sym, translate(preferredName, sym, skind));
aoqi@0 1960 }
aoqi@0 1961 }
aoqi@0 1962
aoqi@0 1963 Map<Symbol, Symbol> getSymbolMap(LambdaSymbolKind skind) {
aoqi@0 1964 Map<Symbol, Symbol> m = translatedSymbols.get(skind);
aoqi@0 1965 Assert.checkNonNull(m);
aoqi@0 1966 return m;
aoqi@0 1967 }
aoqi@0 1968
aoqi@0 1969 JCTree translate(JCIdent lambdaIdent) {
aoqi@0 1970 for (Map<Symbol, Symbol> m : translatedSymbols.values()) {
aoqi@0 1971 if (m.containsKey(lambdaIdent.sym)) {
aoqi@0 1972 Symbol tSym = m.get(lambdaIdent.sym);
aoqi@0 1973 JCTree t = make.Ident(tSym).setType(lambdaIdent.type);
aoqi@0 1974 tSym.setTypeAttributes(lambdaIdent.sym.getRawTypeAttributes());
aoqi@0 1975 return t;
aoqi@0 1976 }
aoqi@0 1977 }
aoqi@0 1978 return null;
aoqi@0 1979 }
aoqi@0 1980
aoqi@0 1981 /**
aoqi@0 1982 * The translatedSym is not complete/accurate until the analysis is
aoqi@0 1983 * finished. Once the analysis is finished, the translatedSym is
aoqi@0 1984 * "completed" -- updated with type information, access modifiers,
aoqi@0 1985 * and full parameter list.
aoqi@0 1986 */
aoqi@0 1987 void complete() {
aoqi@0 1988 if (syntheticParams != null) {
aoqi@0 1989 return;
aoqi@0 1990 }
aoqi@0 1991 boolean inInterface = translatedSym.owner.isInterface();
aoqi@0 1992 boolean thisReferenced = !getSymbolMap(CAPTURED_THIS).isEmpty();
aoqi@0 1993
aoqi@0 1994 // If instance access isn't needed, make it static.
aoqi@0 1995 // Interface instance methods must be default methods.
aoqi@0 1996 // Lambda methods are private synthetic.
aoqi@0 1997 translatedSym.flags_field = SYNTHETIC | LAMBDA_METHOD |
aoqi@0 1998 PRIVATE |
aoqi@0 1999 (thisReferenced? (inInterface? DEFAULT : 0) : STATIC);
aoqi@0 2000
aoqi@0 2001 //compute synthetic params
aoqi@0 2002 ListBuffer<JCVariableDecl> params = new ListBuffer<>();
aoqi@0 2003
aoqi@0 2004 // The signature of the method is augmented with the following
aoqi@0 2005 // synthetic parameters:
aoqi@0 2006 //
aoqi@0 2007 // 1) reference to enclosing contexts captured by the lambda expression
aoqi@0 2008 // 2) enclosing locals captured by the lambda expression
aoqi@0 2009 for (Symbol thisSym : getSymbolMap(CAPTURED_VAR).values()) {
aoqi@0 2010 params.append(make.VarDef((VarSymbol) thisSym, null));
aoqi@0 2011 }
aoqi@0 2012 for (Symbol thisSym : getSymbolMap(PARAM).values()) {
aoqi@0 2013 params.append(make.VarDef((VarSymbol) thisSym, null));
aoqi@0 2014 }
aoqi@0 2015 syntheticParams = params.toList();
aoqi@0 2016
aoqi@0 2017 // Compute and set the lambda name
aoqi@0 2018 translatedSym.name = isSerializable()
aoqi@0 2019 ? serializedLambdaName()
aoqi@0 2020 : lambdaName();
aoqi@0 2021
aoqi@0 2022 //prepend synthetic args to translated lambda method signature
aoqi@0 2023 translatedSym.type = types.createMethodTypeWithParameters(
aoqi@0 2024 generatedLambdaSig(),
aoqi@0 2025 TreeInfo.types(syntheticParams));
aoqi@0 2026 }
aoqi@0 2027
aoqi@0 2028 Type generatedLambdaSig() {
aoqi@0 2029 return types.erasure(tree.getDescriptorType(types));
aoqi@0 2030 }
aoqi@0 2031 }
aoqi@0 2032
aoqi@0 2033 /**
aoqi@0 2034 * This class retains all the useful information about a method reference;
aoqi@0 2035 * the contents of this class are filled by the LambdaAnalyzer visitor,
aoqi@0 2036 * and the used by the main translation routines in order to adjust method
aoqi@0 2037 * references (i.e. in case a bridge is needed)
aoqi@0 2038 */
aoqi@0 2039 private class ReferenceTranslationContext extends TranslationContext<JCMemberReference> {
aoqi@0 2040
aoqi@0 2041 final boolean isSuper;
aoqi@0 2042 final Symbol bridgeSym;
aoqi@0 2043 final Symbol sigPolySym;
aoqi@0 2044
aoqi@0 2045 ReferenceTranslationContext(JCMemberReference tree) {
aoqi@0 2046 super(tree);
aoqi@0 2047 this.isSuper = tree.hasKind(ReferenceKind.SUPER);
aoqi@0 2048 this.bridgeSym = needsBridge()
aoqi@0 2049 ? makePrivateSyntheticMethod(isSuper ? 0 : STATIC,
aoqi@0 2050 referenceBridgeName(), null,
aoqi@0 2051 owner.enclClass())
aoqi@0 2052 : null;
aoqi@0 2053 this.sigPolySym = isSignaturePolymorphic()
aoqi@0 2054 ? makePrivateSyntheticMethod(tree.sym.flags(),
aoqi@0 2055 tree.sym.name,
aoqi@0 2056 bridgedRefSig(),
aoqi@0 2057 tree.sym.enclClass())
aoqi@0 2058 : null;
aoqi@0 2059 if (dumpLambdaToMethodStats) {
aoqi@0 2060 String key = bridgeSym == null ?
aoqi@0 2061 "mref.stat" : "mref.stat.1";
aoqi@0 2062 log.note(tree, key, needsAltMetafactory(), bridgeSym);
aoqi@0 2063 }
aoqi@0 2064 }
aoqi@0 2065
aoqi@0 2066 /**
aoqi@0 2067 * Get the opcode associated with this method reference
aoqi@0 2068 */
aoqi@0 2069 int referenceKind() {
aoqi@0 2070 return LambdaToMethod.this.referenceKind(needsBridge()
aoqi@0 2071 ? bridgeSym
aoqi@0 2072 : tree.sym);
aoqi@0 2073 }
aoqi@0 2074
aoqi@0 2075 boolean needsVarArgsConversion() {
aoqi@0 2076 return tree.varargsElement != null;
aoqi@0 2077 }
aoqi@0 2078
aoqi@0 2079 /**
aoqi@0 2080 * Generate a disambiguating string to increase stability (important
aoqi@0 2081 * if serialized)
aoqi@0 2082 *
aoqi@0 2083 * @return String to differentiate synthetic lambda method names
aoqi@0 2084 */
aoqi@0 2085 private String referenceBridgeDisambiguation() {
aoqi@0 2086 StringBuilder buf = new StringBuilder();
aoqi@0 2087 // Append the enclosing method signature to differentiate
aoqi@0 2088 // overloaded enclosing methods.
aoqi@0 2089 if (owner.type != null) {
aoqi@0 2090 buf.append(typeSig(owner.type));
aoqi@0 2091 buf.append(":");
aoqi@0 2092 }
aoqi@0 2093
aoqi@0 2094 // Append qualifier type
aoqi@0 2095 buf.append(classSig(tree.sym.owner.type));
aoqi@0 2096
aoqi@0 2097 // Note static/instance
aoqi@0 2098 buf.append(tree.sym.isStatic()? " S " : " I ");
aoqi@0 2099
aoqi@0 2100 // Append referenced signature
aoqi@0 2101 buf.append(typeSig(tree.sym.erasure(types)));
aoqi@0 2102
aoqi@0 2103 return buf.toString();
aoqi@0 2104 }
aoqi@0 2105
aoqi@0 2106 /**
aoqi@0 2107 * Construct a unique stable name for the method reference bridge
aoqi@0 2108 *
aoqi@0 2109 * @return Name to use for the synthetic method name
aoqi@0 2110 */
aoqi@0 2111 private Name referenceBridgeName() {
aoqi@0 2112 StringBuilder buf = new StringBuilder();
aoqi@0 2113 // Append lambda ID, this is semantically significant
aoqi@0 2114 buf.append(names.lambda);
aoqi@0 2115 // Note that it is a method reference bridge
aoqi@0 2116 buf.append("MR$");
aoqi@0 2117 // Append the enclosing method name
aoqi@0 2118 buf.append(enclosingMethodName());
aoqi@0 2119 buf.append('$');
aoqi@0 2120 // Append the referenced method name
aoqi@0 2121 buf.append(syntheticMethodNameComponent(tree.sym.name));
aoqi@0 2122 buf.append('$');
aoqi@0 2123 // Append a hash of the disambiguating string : enclosing method
aoqi@0 2124 // signature, etc.
aoqi@0 2125 String disam = referenceBridgeDisambiguation();
aoqi@0 2126 buf.append(Integer.toHexString(disam.hashCode()));
aoqi@0 2127 buf.append('$');
aoqi@0 2128 // The above appended name components may not be unique, append
aoqi@0 2129 // a count based on the above name components.
aoqi@0 2130 buf.append(syntheticMethodNameCounts.getIndex(buf));
aoqi@0 2131 String result = buf.toString();
aoqi@0 2132 return names.fromString(result);
aoqi@0 2133 }
aoqi@0 2134
aoqi@0 2135 /**
aoqi@0 2136 * @return Is this an array operation like clone()
aoqi@0 2137 */
aoqi@0 2138 boolean isArrayOp() {
aoqi@0 2139 return tree.sym.owner == syms.arrayClass;
aoqi@0 2140 }
aoqi@0 2141
aoqi@0 2142 boolean receiverAccessible() {
aoqi@0 2143 //hack needed to workaround 292 bug (7087658)
aoqi@0 2144 //when 292 issue is fixed we should remove this and change the backend
aoqi@0 2145 //code to always generate a method handle to an accessible method
aoqi@0 2146 return tree.ownerAccessible;
aoqi@0 2147 }
aoqi@0 2148
aoqi@0 2149 /**
aoqi@0 2150 * The VM does not support access across nested classes (8010319).
aoqi@0 2151 * Were that ever to change, this should be removed.
aoqi@0 2152 */
aoqi@0 2153 boolean isPrivateInOtherClass() {
aoqi@0 2154 return (tree.sym.flags() & PRIVATE) != 0 &&
aoqi@0 2155 !types.isSameType(
aoqi@0 2156 types.erasure(tree.sym.enclClass().asType()),
aoqi@0 2157 types.erasure(owner.enclClass().asType()));
aoqi@0 2158 }
aoqi@0 2159
aoqi@0 2160 /**
aoqi@0 2161 * Signature polymorphic methods need special handling.
aoqi@0 2162 * e.g. MethodHandle.invoke() MethodHandle.invokeExact()
aoqi@0 2163 */
aoqi@0 2164 final boolean isSignaturePolymorphic() {
aoqi@0 2165 return tree.sym.kind == MTH &&
aoqi@0 2166 types.isSignaturePolymorphic((MethodSymbol)tree.sym);
aoqi@0 2167 }
aoqi@0 2168
aoqi@0 2169 /**
aoqi@0 2170 * Does this reference needs a bridge (i.e. var args need to be
aoqi@0 2171 * expanded or "super" is used)
aoqi@0 2172 */
aoqi@0 2173 final boolean needsBridge() {
aoqi@0 2174 return isSuper || needsVarArgsConversion() || isArrayOp() ||
aoqi@0 2175 isPrivateInOtherClass() ||
aoqi@0 2176 !receiverAccessible();
aoqi@0 2177 }
aoqi@0 2178
aoqi@0 2179 Type generatedRefSig() {
aoqi@0 2180 return types.erasure(tree.sym.type);
aoqi@0 2181 }
aoqi@0 2182
aoqi@0 2183 Type bridgedRefSig() {
aoqi@0 2184 return types.erasure(types.findDescriptorSymbol(tree.targets.head.tsym).type);
aoqi@0 2185 }
aoqi@0 2186 }
aoqi@0 2187 }
aoqi@0 2188 // </editor-fold>
aoqi@0 2189
aoqi@0 2190 /*
aoqi@0 2191 * These keys provide mappings for various translated lambda symbols
aoqi@0 2192 * and the prevailing order must be maintained.
aoqi@0 2193 */
aoqi@0 2194 enum LambdaSymbolKind {
aoqi@0 2195 PARAM, // original to translated lambda parameters
aoqi@0 2196 LOCAL_VAR, // original to translated lambda locals
aoqi@0 2197 CAPTURED_VAR, // variables in enclosing scope to translated synthetic parameters
aoqi@0 2198 CAPTURED_THIS, // class symbols to translated synthetic parameters (for captured member access)
aoqi@0 2199 TYPE_VAR; // original to translated lambda type variables
aoqi@0 2200 }
aoqi@0 2201
aoqi@0 2202 /**
aoqi@0 2203 * ****************************************************************
aoqi@0 2204 * Signature Generation
aoqi@0 2205 * ****************************************************************
aoqi@0 2206 */
aoqi@0 2207
aoqi@0 2208 private String typeSig(Type type) {
aoqi@0 2209 L2MSignatureGenerator sg = new L2MSignatureGenerator();
aoqi@0 2210 sg.assembleSig(type);
aoqi@0 2211 return sg.toString();
aoqi@0 2212 }
aoqi@0 2213
aoqi@0 2214 private String classSig(Type type) {
aoqi@0 2215 L2MSignatureGenerator sg = new L2MSignatureGenerator();
aoqi@0 2216 sg.assembleClassSig(type);
aoqi@0 2217 return sg.toString();
aoqi@0 2218 }
aoqi@0 2219
aoqi@0 2220 /**
aoqi@0 2221 * Signature Generation
aoqi@0 2222 */
aoqi@0 2223 private class L2MSignatureGenerator extends Types.SignatureGenerator {
aoqi@0 2224
aoqi@0 2225 /**
aoqi@0 2226 * An output buffer for type signatures.
aoqi@0 2227 */
aoqi@0 2228 StringBuilder sb = new StringBuilder();
aoqi@0 2229
aoqi@0 2230 L2MSignatureGenerator() {
aoqi@0 2231 super(types);
aoqi@0 2232 }
aoqi@0 2233
aoqi@0 2234 @Override
aoqi@0 2235 protected void append(char ch) {
aoqi@0 2236 sb.append(ch);
aoqi@0 2237 }
aoqi@0 2238
aoqi@0 2239 @Override
aoqi@0 2240 protected void append(byte[] ba) {
aoqi@0 2241 sb.append(new String(ba));
aoqi@0 2242 }
aoqi@0 2243
aoqi@0 2244 @Override
aoqi@0 2245 protected void append(Name name) {
aoqi@0 2246 sb.append(name.toString());
aoqi@0 2247 }
aoqi@0 2248
aoqi@0 2249 @Override
aoqi@0 2250 public String toString() {
aoqi@0 2251 return sb.toString();
aoqi@0 2252 }
aoqi@0 2253 }
aoqi@0 2254 }

mercurial