< prev index next >

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

Print this page

 355             boolean init;
 356             if ((init = (owner.name == names.init)) || owner.name == names.clinit) {
 357                 owner = owner.owner;
 358                 apportionTypeAnnotations(tree,
 359                         init ? owner::getInitTypeAttributes : owner::getClassInitTypeAttributes,
 360                         init ? owner::setInitTypeAttributes : owner::setClassInitTypeAttributes,
 361                         sym::appendUniqueTypeAttributes);
 362             }
 363             if (localContext.self != null && localContext.self.getKind() == ElementKind.FIELD) {
 364                 owner = localContext.self;
 365                 apportionTypeAnnotations(tree,
 366                         owner::getRawTypeAttributes,
 367                         owner::setTypeAttributes,
 368                         sym::appendUniqueTypeAttributes);
 369             }
 370         }
 371 
 372         //create the method declaration hoisting the lambda body
 373         JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
 374                 sym.name,
 375                 make.QualIdent(lambdaType.getReturnType().tsym),
 376                 List.nil(),
 377                 localContext.syntheticParams,
 378                 lambdaType.getThrownTypes() == null ?
 379                     List.nil() :
 380                     make.Types(lambdaType.getThrownTypes()),
 381                 null,
 382                 null);
 383         lambdaDecl.sym = sym;
 384         lambdaDecl.type = lambdaType;
 385 
 386         //translate lambda body
 387         //As the lambda body is translated, all references to lambda locals,
 388         //captured variables, enclosing members are adjusted accordingly
 389         //to refer to the static method parameters (rather than i.e. accessing
 390         //captured members directly).
 391         lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));
 392 
 393         boolean dedupe = false;
 394         if (deduplicateLambdas && !debugLinesOrVars && !localContext.isSerializable()) {
 395             DedupedLambda dedupedLambda = new DedupedLambda(lambdaDecl.sym, lambdaDecl.body);

1846                 this.owner = owner(true);
1847                 this.depth = frameStack.size() - 1;
1848                 this.prev = context();
1849                 ClassSymbol csym =
1850                         types.makeFunctionalInterfaceClass(attrEnv, names.empty, tree.target, ABSTRACT | INTERFACE);
1851                 this.bridges = types.functionalInterfaceBridges(csym);
1852             }
1853 
1854             /** does this functional expression need to be created using alternate metafactory? */
1855             boolean needsAltMetafactory() {
1856                 return tree.target.isIntersection() ||
1857                         isSerializable() ||
1858                         bridges.length() > 1;
1859             }
1860 
1861             /** does this functional expression require serialization support? */
1862             boolean isSerializable() {
1863                 if (forceSerializable) {
1864                     return true;
1865                 }
1866                 return types.asSuper(tree.target, syms.serializableType.tsym) != null;
1867             }
1868 
1869             /**
1870              * @return Name of the enclosing method to be folded into synthetic
1871              * method name
1872              */
1873             String enclosingMethodName() {
1874                 return syntheticMethodNameComponent(owner.name);
1875             }
1876 
1877             /**
1878              * @return Method name in a form that can be folded into a
1879              * component of a synthetic method name
1880              */
1881             String syntheticMethodNameComponent(Name name) {
1882                 if (name == null) {
1883                     return "null";
1884                 }
1885                 String methodName = name.toString();
1886                 if (methodName.equals("<clinit>")) {

2323                         TypeVar tv = (TypeVar) t;
2324                         return isIntersectionOrUnionType(tv.getUpperBound());
2325                 }
2326                 return false;
2327             }
2328 
2329             /**
2330              * Does this reference need to be converted to a lambda
2331              * (i.e. var args need to be expanded or "super" is used)
2332              */
2333             final boolean needsConversionToLambda() {
2334                 return interfaceParameterIsIntersectionOrUnionType() ||
2335                         isSuper ||
2336                         needsVarArgsConversion() ||
2337                         isArrayOp() ||
2338                         (!nestmateLambdas && isPrivateInOtherClass()) ||
2339                         isProtectedInSuperClassOfEnclosingClassInOtherPackage(tree.sym, owner) ||
2340                         !receiverAccessible() ||
2341                         (tree.getMode() == ReferenceMode.NEW &&
2342                           tree.kind != ReferenceKind.ARRAY_CTOR &&
2343                           (tree.sym.owner.isDirectlyOrIndirectlyLocal() || tree.sym.owner.isInner()));
2344             }
2345 
2346             Type generatedRefSig() {
2347                 return types.erasure(tree.sym.type);
2348             }
2349 
2350             Type bridgedRefSig() {
2351                 return types.erasure(types.findDescriptorSymbol(tree.target.tsym).type);
2352             }
2353         }
2354     }
2355     // </editor-fold>
2356 
2357     /*
2358      * These keys provide mappings for various translated lambda symbols
2359      * and the prevailing order must be maintained.
2360      */
2361     enum LambdaSymbolKind {
2362         PARAM,          // original to translated lambda parameters
2363         LOCAL_VAR,      // original to translated lambda locals

 355             boolean init;
 356             if ((init = (owner.name == names.init)) || owner.name == names.clinit) {
 357                 owner = owner.owner;
 358                 apportionTypeAnnotations(tree,
 359                         init ? owner::getInitTypeAttributes : owner::getClassInitTypeAttributes,
 360                         init ? owner::setInitTypeAttributes : owner::setClassInitTypeAttributes,
 361                         sym::appendUniqueTypeAttributes);
 362             }
 363             if (localContext.self != null && localContext.self.getKind() == ElementKind.FIELD) {
 364                 owner = localContext.self;
 365                 apportionTypeAnnotations(tree,
 366                         owner::getRawTypeAttributes,
 367                         owner::setTypeAttributes,
 368                         sym::appendUniqueTypeAttributes);
 369             }
 370         }
 371 
 372         //create the method declaration hoisting the lambda body
 373         JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
 374                 sym.name,
 375                 make.QualIdent(lambdaType.getReturnType().tsym).setType(lambdaType.getReturnType()),
 376                 List.nil(),
 377                 localContext.syntheticParams,
 378                 lambdaType.getThrownTypes() == null ?
 379                     List.nil() :
 380                     make.Types(lambdaType.getThrownTypes()),
 381                 null,
 382                 null);
 383         lambdaDecl.sym = sym;
 384         lambdaDecl.type = lambdaType;
 385 
 386         //translate lambda body
 387         //As the lambda body is translated, all references to lambda locals,
 388         //captured variables, enclosing members are adjusted accordingly
 389         //to refer to the static method parameters (rather than i.e. accessing
 390         //captured members directly).
 391         lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));
 392 
 393         boolean dedupe = false;
 394         if (deduplicateLambdas && !debugLinesOrVars && !localContext.isSerializable()) {
 395             DedupedLambda dedupedLambda = new DedupedLambda(lambdaDecl.sym, lambdaDecl.body);

1846                 this.owner = owner(true);
1847                 this.depth = frameStack.size() - 1;
1848                 this.prev = context();
1849                 ClassSymbol csym =
1850                         types.makeFunctionalInterfaceClass(attrEnv, names.empty, tree.target, ABSTRACT | INTERFACE);
1851                 this.bridges = types.functionalInterfaceBridges(csym);
1852             }
1853 
1854             /** does this functional expression need to be created using alternate metafactory? */
1855             boolean needsAltMetafactory() {
1856                 return tree.target.isIntersection() ||
1857                         isSerializable() ||
1858                         bridges.length() > 1;
1859             }
1860 
1861             /** does this functional expression require serialization support? */
1862             boolean isSerializable() {
1863                 if (forceSerializable) {
1864                     return true;
1865                 }
1866                 return types.asSuper(tree.target.referenceProjectionOrSelf(), syms.serializableType.tsym) != null;
1867             }
1868 
1869             /**
1870              * @return Name of the enclosing method to be folded into synthetic
1871              * method name
1872              */
1873             String enclosingMethodName() {
1874                 return syntheticMethodNameComponent(owner.name);
1875             }
1876 
1877             /**
1878              * @return Method name in a form that can be folded into a
1879              * component of a synthetic method name
1880              */
1881             String syntheticMethodNameComponent(Name name) {
1882                 if (name == null) {
1883                     return "null";
1884                 }
1885                 String methodName = name.toString();
1886                 if (methodName.equals("<clinit>")) {

2323                         TypeVar tv = (TypeVar) t;
2324                         return isIntersectionOrUnionType(tv.getUpperBound());
2325                 }
2326                 return false;
2327             }
2328 
2329             /**
2330              * Does this reference need to be converted to a lambda
2331              * (i.e. var args need to be expanded or "super" is used)
2332              */
2333             final boolean needsConversionToLambda() {
2334                 return interfaceParameterIsIntersectionOrUnionType() ||
2335                         isSuper ||
2336                         needsVarArgsConversion() ||
2337                         isArrayOp() ||
2338                         (!nestmateLambdas && isPrivateInOtherClass()) ||
2339                         isProtectedInSuperClassOfEnclosingClassInOtherPackage(tree.sym, owner) ||
2340                         !receiverAccessible() ||
2341                         (tree.getMode() == ReferenceMode.NEW &&
2342                           tree.kind != ReferenceKind.ARRAY_CTOR &&
2343                           (tree.sym.owner.isDirectlyOrIndirectlyLocal() || tree.sym.owner.isInner() || tree.sym.owner.isValueClass()));
2344             }
2345 
2346             Type generatedRefSig() {
2347                 return types.erasure(tree.sym.type);
2348             }
2349 
2350             Type bridgedRefSig() {
2351                 return types.erasure(types.findDescriptorSymbol(tree.target.tsym).type);
2352             }
2353         }
2354     }
2355     // </editor-fold>
2356 
2357     /*
2358      * These keys provide mappings for various translated lambda symbols
2359      * and the prevailing order must be maintained.
2360      */
2361     enum LambdaSymbolKind {
2362         PARAM,          // original to translated lambda parameters
2363         LOCAL_VAR,      // original to translated lambda locals
< prev index next >