changeset 52760:36302405aa09 amber-demo

manual merge with concise methods
author vromero
date Fri, 05 Oct 2018 13:27:09 -0700
parents 9a7a275389fc ad90da891ae0
children 62f972dd087f
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java
diffstat 19 files changed, 605 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java	Fri Oct 05 13:27:09 2018 -0700
@@ -330,6 +330,11 @@
      */
     public static final long MATCH_BINDING = 1L<<59;
 
+    /**
+     * Flag for concise methods
+     */
+    public static final long CONCISE = 1L<<60;
+
     /** Modifier masks.
      */
     public static final int
@@ -450,7 +455,8 @@
         POTENTIALLY_AMBIGUOUS(Flags.POTENTIALLY_AMBIGUOUS),
         ANONCONSTR_BASED(Flags.ANONCONSTR_BASED),
         MATCH_BINDING(Flags.MATCH_BINDING),
-        RECORD(Flags.RECORD);
+        RECORD(Flags.RECORD),
+        CONCISE(Flags.CONCISE);
 
         Flag(long flag) {
             this.value = flag;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Fri Oct 05 13:27:09 2018 -0700
@@ -185,7 +185,8 @@
         SWITCH_RULE(JDK12, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
         SWITCH_EXPRESSION(JDK12, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
         RAW_STRING_LITERALS(JDK12, Fragments.FeatureRawStringLiterals, DiagKind.PLURAL),
-        RECORDS(JDK10);
+        RECORDS(JDK10),
+        CONCISE_METHOD_BODIES(JDK12, Fragments.FeatureConciseMethodBodies, DiagKind.PLURAL);
 
         enum DiagKind {
             NORMAL,
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Fri Oct 05 13:27:09 2018 -0700
@@ -30,7 +30,6 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Queue;
-import java.util.function.Predicate;
 
 import com.sun.source.tree.LambdaExpressionTree;
 import com.sun.source.tree.NewClassTree;
@@ -38,16 +37,20 @@
 import com.sun.tools.javac.code.Kinds.Kind;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.code.Source.Feature;
+import com.sun.tools.javac.code.Symbol;
 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.code.Types;
 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
+import com.sun.tools.javac.resources.CompilerProperties.Notes;
 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.tree.JCTree.JCBlock;
 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
 import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
 import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
+import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
+import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
 import com.sun.tools.javac.tree.JCTree.JCForLoop;
 import com.sun.tools.javac.tree.JCTree.JCIf;
 import com.sun.tools.javac.tree.JCTree.JCLambda;
@@ -55,6 +58,7 @@
 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
 import com.sun.tools.javac.tree.JCTree.JCNewClass;
+import com.sun.tools.javac.tree.JCTree.JCReturn;
 import com.sun.tools.javac.tree.JCTree.JCStatement;
 import com.sun.tools.javac.tree.JCTree.JCSwitch;
 import com.sun.tools.javac.tree.JCTree.JCTry;
@@ -76,9 +80,11 @@
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.ListBuffer;
 import com.sun.tools.javac.util.Log;
+import com.sun.tools.javac.util.Names;
 import com.sun.tools.javac.util.Options;
 import com.sun.tools.javac.util.Position;
 
+import static com.sun.tools.javac.code.Flags.ANONCONSTR;
 import static com.sun.tools.javac.code.Flags.GENERATEDCONSTR;
 import static com.sun.tools.javac.code.TypeTag.CLASS;
 import static com.sun.tools.javac.tree.JCTree.Tag.APPLY;
@@ -87,6 +93,7 @@
 import static com.sun.tools.javac.tree.JCTree.Tag.METHODDEF;
 import static com.sun.tools.javac.tree.JCTree.Tag.NEWCLASS;
 import static com.sun.tools.javac.tree.JCTree.Tag.NULLCHK;
+import static com.sun.tools.javac.tree.JCTree.Tag.SELECT;
 import static com.sun.tools.javac.tree.JCTree.Tag.TYPEAPPLY;
 import static com.sun.tools.javac.tree.JCTree.Tag.VARDEF;
 
@@ -104,6 +111,7 @@
     final ArgumentAttr argumentAttr;
     final TreeMaker make;
     final AnalyzerCopier copier;
+    final Names names;
     private final boolean allowDiamondWithAnonymousClassCreation;
 
     final EnumSet<AnalyzerMode> analyzerModes;
@@ -124,6 +132,7 @@
         argumentAttr = ArgumentAttr.instance(context);
         make = TreeMaker.instance(context);
         copier = new AnalyzerCopier();
+        names = Names.instance(context);
         Options options = Options.instance(context);
         String findOpt = options.get("find");
         //parse modes
@@ -140,6 +149,7 @@
         DIAMOND("diamond", Feature.DIAMOND),
         LAMBDA("lambda", Feature.LAMBDA),
         METHOD("method", Feature.GRAPH_INFERENCE),
+        CONCISE_METHOD("concise_method", Feature.CONCISE_METHOD_BODIES),
         LOCAL("local", Feature.LOCAL_VARIABLE_TYPE_INFERENCE);
 
         final String opt;
@@ -198,10 +208,14 @@
             return analyzerModes.contains(mode);
         }
 
+        boolean accepts(S tree) {
+            return tree.hasTag(tag);
+        }
+
         /**
          * Should this analyzer be rewriting the given tree?
          */
-        abstract boolean match(S tree);
+        abstract boolean match(S tree, Env<AttrContext> env);
 
         /**
          * Rewrite a given AST node into a new one(s)
@@ -224,7 +238,7 @@
         }
 
         @Override
-        boolean match(JCNewClass tree) {
+        boolean match(JCNewClass tree, Env<AttrContext> env) {
             return tree.clazz.hasTag(TYPEAPPLY) &&
                     !TreeInfo.isDiamond(tree) &&
                     (tree.def == null || allowDiamondWithAnonymousClassCreation);
@@ -278,7 +292,7 @@
         }
 
         @Override
-        boolean match (JCNewClass tree){
+        boolean match (JCNewClass tree, Env<AttrContext> env){
             Type clazztype = tree.clazz.type;
             return tree.def != null &&
                     clazztype.hasTag(CLASS) &&
@@ -319,6 +333,113 @@
     }
 
     /**
+     * This analyzer checks if anonymous instance creation expression can replaced by lambda.
+     */
+    class ConciseMethodsAnalyzer extends StatementAnalyzer<JCTree, JCTree> {
+
+        Map<JCTree, JCTree> treeToPosMap = new HashMap<>();
+
+        ConciseMethodsAnalyzer() {
+            super(AnalyzerMode.CONCISE_METHOD, METHODDEF);
+        }
+
+        @Override
+        boolean accepts(JCTree tree) {
+            return tree.hasTag(Tag.RETURN) || tree.hasTag(Tag.EXEC);
+        }
+
+        @Override
+        boolean match (JCTree tree, Env<AttrContext> env) {
+            boolean result = false;
+            JCMethodDecl methodDec = env.enclMethod;
+            if (methodDec != null &&
+                    ((methodDec.mods.flags & Flags.SYNTHETIC) == 0) &&
+                    ((methodDec.mods.flags & Flags.ABSTRACT) == 0) &&
+//                    ((methodDec.mods.flags & Flags.GENERATEDCONSTR) == 0) &&
+//                    ((methodDec.mods.flags & Flags.ANONCONSTR) == 0) &&
+                    methodDec.body.stats.size() == 1 &&
+                    methodDec.name != names.init) {
+                JCStatement stat = methodDec.body.stats.head;
+                JCMethodInvocation apply = findInvocation(stat);
+                if (apply != null) {
+                    Symbol qualifier = null;
+                    List<Symbol> paramsSyms = TreeInfo.symbols(methodDec.params);
+                    List<Symbol> argsSyms = TreeInfo.symbols(apply.args);
+                    if (apply.meth.hasTag(SELECT)) {
+                        qualifier = TreeInfo.symbol(((JCFieldAccess) apply.meth).selected);
+                        qualifier = methodDec.params.isEmpty() || qualifier != methodDec.params.head.sym ? null : qualifier;
+                    }
+                    if (methodDec.params.size() == apply.args.size() + (qualifier != null ? 1 : 0)) {
+                        argsSyms = qualifier != null ? argsSyms.prepend(qualifier) : argsSyms;
+                        result = paramsSyms.equals(argsSyms);
+                        if (result) {
+                            treeToPosMap.put(tree, methodDec);
+                        }
+                    }
+                } else {
+                    JCNewClass constructor = findConstructor(stat);
+                    if (constructor != null && constructor.def == null) {
+                        if (methodDec.params.size() == constructor.args.size()) {
+                            List<Symbol> paramsSyms = TreeInfo.symbols(methodDec.params);
+                            List<Symbol> argsSyms = TreeInfo.symbols(constructor.args);
+                            result = paramsSyms.equals(argsSyms);
+                            if (result) {
+                                treeToPosMap.put(tree, methodDec);
+                            }
+                        }
+                    }
+                }
+            }
+            return result;
+        }
+        //where
+            private JCMethodInvocation findInvocation(JCStatement stat) {
+                if (stat.hasTag(Tag.RETURN)) {
+                    JCReturn retrn = (JCReturn)stat;
+                    if (retrn.expr != null && retrn.expr.hasTag(APPLY)) {
+                        return (JCMethodInvocation)retrn.expr;
+                    }
+                } else if (stat.hasTag(Tag.EXEC)) {
+                    JCExpressionStatement exprStat = (JCExpressionStatement)stat;
+                    if (exprStat.expr.hasTag(APPLY)) {
+                        return (JCMethodInvocation)exprStat.expr;
+                    }
+                }
+                return null;
+            }
+
+            private JCNewClass findConstructor(JCStatement stat) {
+                if (stat.hasTag(Tag.RETURN)) {
+                    JCReturn retrn = (JCReturn)stat;
+                    if (retrn.expr != null && retrn.expr.hasTag(NEWCLASS)) {
+                        return (JCNewClass)retrn.expr;
+                    }
+                } else if (stat.hasTag(Tag.EXEC)) {
+                    JCExpressionStatement exprStat = (JCExpressionStatement)stat;
+                    if (exprStat.expr.hasTag(NEWCLASS)) {
+                        return (JCNewClass)exprStat.expr;
+                    }
+                }
+                return null;
+            }
+
+        @Override
+        List<JCTree> rewrite(JCTree oldTree){
+            return List.of(oldTree);
+        }
+
+        @Override
+        void process (JCTree oldTree, JCTree newTree, boolean hasErrors){
+            if (!hasErrors) {
+                // this version is for experiments only, printing warnings breaks the OpenJDK build
+                //log.note(treeToPosMap.get(oldTree), Notes.PotentialConciseMethodFound(oldTree.toString()));
+                log.warning(treeToPosMap.get(oldTree), Warnings.PotentialConciseMethodFound);
+            }
+            treeToPosMap.remove(oldTree);
+        }
+    }
+
+    /**
      * This analyzer checks if generic method call has redundant type arguments.
      */
     class RedundantTypeArgAnalyzer extends StatementAnalyzer<JCMethodInvocation, JCMethodInvocation> {
@@ -328,7 +449,7 @@
         }
 
         @Override
-        boolean match (JCMethodInvocation tree){
+        boolean match (JCMethodInvocation tree, Env<AttrContext> env){
             return tree.typeargs != null &&
                     tree.typeargs.nonEmpty();
         }
@@ -391,7 +512,7 @@
             super(VARDEF);
         }
 
-        boolean match(JCVariableDecl tree){
+        boolean match(JCVariableDecl tree, Env<AttrContext> env){
             return tree.sym.owner.kind == Kind.MTH &&
                     tree.init != null && !isImplicitlyTyped(tree) &&
                     attr.canInferLocalVarType(tree) == null;
@@ -416,7 +537,7 @@
         }
 
         @Override
-        boolean match(JCEnhancedForLoop tree){
+        boolean match(JCEnhancedForLoop tree, Env<AttrContext> env){
             return !isImplicitlyTyped(tree.var);
         }
         @Override
@@ -438,7 +559,8 @@
             new LambdaAnalyzer(),
             new RedundantTypeArgAnalyzer(),
             new RedundantLocalVarTypeAnalyzer(),
-            new RedundantLocalVarTypeAnalyzerForEach()
+            new RedundantLocalVarTypeAnalyzerForEach(),
+            new ConciseMethodsAnalyzer(),
     };
 
     /**
@@ -601,8 +723,8 @@
             if (tree != null) {
                 for (StatementAnalyzer<JCTree, JCTree> analyzer : analyzers) {
                     if (analyzer.isEnabled() &&
-                            tree.hasTag(analyzer.tag) &&
-                            analyzer.match(tree)) {
+                            analyzer.accepts(tree) &&
+                            analyzer.match(tree, env)) {
                         for (JCTree t : analyzer.rewrite(tree)) {
                             rewritings.add(new RewritingContext(originalTree, tree, t, analyzer, env));
                         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Oct 05 13:27:09 2018 -0700
@@ -1092,6 +1092,8 @@
                 }
             } else if ((tree.mods.flags & NATIVE) != 0) {
                 log.error(tree.pos(), Errors.NativeMethCantHaveBody);
+            } else if ((tree.mods.flags & CONCISE) != 0 && tree.name == names.init) {
+                log.error(tree.pos(), Errors.ConstructorsCantHaveConciseBody);
             } else {
                 // Add an implicit super() call unless an explicit call to
                 // super(...) or this(...) is given
@@ -1121,7 +1123,15 @@
                 annotate.flush();
 
                 // Attribute method body.
-                attribStat(tree.body, localEnv);
+                if (tree.conciseMethodRef != null) {
+                    localEnv.info.conciseMethod = tree;
+                    if (!tree.conciseMethodRef.hasTag(REFERENCE)) {
+                        log.error(tree.conciseMethodRef, Errors.OnlyMethodReferencesAllowed);
+                    }
+                    attribTree(tree.conciseMethodRef, localEnv, statInfo);
+                } else {
+                    attribStat(tree.body, localEnv);
+                }
             }
 
             localEnv.info.scope.leave();
@@ -3248,13 +3258,15 @@
 
     @Override
     public void visitReference(final JCMemberReference that) {
-        if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
-            if (pt().hasTag(NONE) && (env.info.enclVar == null || !env.info.enclVar.type.isErroneous())) {
-                //method reference only allowed in assignment or method invocation/cast context
-                log.error(that.pos(), Errors.UnexpectedMref);
-            }
-            result = that.type = types.createErrorType(pt());
-            return;
+        if (env.info.conciseMethod == null) {
+            if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
+                if (pt().hasTag(NONE) && (env.info.enclVar == null || !env.info.enclVar.type.isErroneous())) {
+                    //method reference only allowed in assignment or method invocation/cast context
+                    log.error(that.pos(), Errors.UnexpectedMref);
+                }
+                result = that.type = types.createErrorType(pt());
+                return;
+            }
         }
         final Env<AttrContext> localEnv = env.dup(that);
         try {
@@ -3299,9 +3311,16 @@
             boolean isTargetSerializable =
                     resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK &&
                     isSerializable(pt());
-            TargetInfo targetInfo = getTargetInfo(that, resultInfo, null);
-            Type currentTarget = targetInfo.target;
-            Type desc = targetInfo.descriptor;
+            Type currentTarget;
+            Type desc;
+            if (localEnv.info.conciseMethod == null) {
+                TargetInfo targetInfo = getTargetInfo(that, resultInfo, null);
+                currentTarget = targetInfo.target;
+                desc = targetInfo.descriptor;
+            } else {
+                currentTarget = pt();
+                desc = localEnv.info.conciseMethod.sym.type;
+            }
 
             setFunctionalInfo(localEnv, that, pt(), desc, currentTarget, resultInfo.checkContext);
             List<Type> argtypes = desc.getParameterTypes();
@@ -3315,7 +3334,7 @@
             List<Type> saved_undet = resultInfo.checkContext.inferenceContext().save();
             try {
                 refResult = rs.resolveMemberReference(localEnv, that, that.expr.type,
-                        that.name, argtypes, typeargtypes, targetInfo.descriptor, referenceCheck,
+                        that.name, argtypes, typeargtypes, desc, referenceCheck,
                         resultInfo.checkContext.inferenceContext(), rs.basicReferenceChooser);
             } finally {
                 resultInfo.checkContext.inferenceContext().rollback(saved_undet);
@@ -3452,7 +3471,11 @@
             if (!isSpeculativeRound) {
                 checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), desc, currentTarget);
             }
-            result = check(that, currentTarget, KindSelector.VAL, resultInfo);
+            if (localEnv.info.conciseMethod == null) {
+                result = check(that, currentTarget, KindSelector.VAL, resultInfo);
+            } else {
+                result = currentTarget;
+            }
         } catch (Types.FunctionDescriptorLookupError ex) {
             JCDiagnostic cause = ex.getDiagnostic();
             resultInfo.checkContext.report(that, cause);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Fri Oct 05 13:27:09 2018 -0700
@@ -30,6 +30,7 @@
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.code.Scope.WriteableScope;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
 
 /** Contains information specific to the attribute and enter
  *  passes, to be used in place of the generic field in environments.
@@ -120,6 +121,10 @@
 
     MethodSymbol recordImplicitConstructor;
 
+    /** method that acts as a wrapper for a method reference
+     */
+    JCMethodDecl conciseMethod;
+
     /** Duplicate this context, replacing scope field and copying all others.
      */
     AttrContext dup(WriteableScope scope) {
@@ -141,6 +146,7 @@
         info.isNewClass = isNewClass;
         info.preferredTreeForDiagnostics = preferredTreeForDiagnostics;
         info.visitingServiceImplementation = visitingServiceImplementation;
+        info.conciseMethod = conciseMethod;
         info.recordImplicitConstructor = recordImplicitConstructor;
         return info;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Oct 05 13:27:09 2018 -0700
@@ -528,7 +528,7 @@
                 alive = true;
                 scanStat(tree.body);
 
-                if (alive && !tree.sym.type.getReturnType().hasTag(VOID))
+                if (tree.conciseMethodRef == null && alive && !tree.sym.type.getReturnType().hasTag(VOID))
                     log.error(TreeInfo.diagEndPos(tree.body), Errors.MissingRetStmt);
 
                 List<PendingExit> exits = pendingExits.toList();
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Oct 05 13:27:09 2018 -0700
@@ -106,6 +106,7 @@
     private final boolean debugLower;
     private final boolean disableProtectedAccessors; // experimental
     private final PkgInfo pkginfoOpt;
+    private final TransTypes transTypes;
 
     protected Lower(Context context) {
         context.put(lowerKey, this);
@@ -130,6 +131,7 @@
             fromString(target.syntheticNameChar() + "closeResource");
 
         types = Types.instance(context);
+        transTypes = TransTypes.instance(context);
         Options options = Options.instance(context);
         debugLower = options.isSet("debuglower");
         pkginfoOpt = PkgInfo.get(options);
@@ -2845,6 +2847,55 @@
                 lambdaTranslationMap = (tree.sym.flags() & SYNTHETIC) != 0 &&
                         tree.sym.name.startsWith(names.lambda) ?
                         makeTranslationMap(tree) : null;
+                if (tree.conciseMethodRef != null) {
+                    JCFieldAccess qualifier = null;
+                    JCMethodInvocation apply = null;
+                    JCExpression expr = null;
+                    JCMemberReference reference = (JCMemberReference)tree.conciseMethodRef;
+                    switch (reference.kind) {
+                        case STATIC:
+                            qualifier = make.Select(access(make.QualIdent(reference.expr.type.tsym)), reference.name);
+                            apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, make.Idents(tree.params));
+                            break;
+                        case UNBOUND:
+                            qualifier = make.Select(access(make.QualIdent(tree.params.head.sym)), reference.name);
+                            apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, List.nil());
+                            break;
+                        case BOUND: case SUPER:
+                            qualifier = make.Select(access(reference.expr), reference.name);
+                            apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, make.Idents(tree.params));
+                            break;
+                        case TOPLEVEL: case IMPLICIT_INNER:
+                            Symbol constSym = accessConstructor(reference, reference.expr.type.tsym);
+                            expr = makeNewClass(constSym.type, make.Idents(tree.params));
+                            break;
+                        case ARRAY_CTOR:
+                            expr = make.NewArray(
+                                    make.Type(types.elemtype(reference.expr.type)),
+                                    List.of(make.Ident(tree.params.head.sym)), null)
+                                    .setType(new ArrayType(types.elemtype(reference.expr.type), syms.arrayClass));
+                            break;
+                        default:
+                            qualifier = make.Select(make.QualIdent(tree.params.head.sym), reference.name);
+                            apply = make.at(tree.body.pos).Apply(List.nil(), qualifier, List.nil());
+                    }
+                    switch (reference.kind) {
+                        case STATIC: case UNBOUND: case BOUND: case SUPER:
+                            qualifier.type = tree.type;
+                            qualifier.sym = reference.sym;
+                            Type mt = reference.sym.erasure(types);
+                            apply.setType(mt.asMethodType().restype);
+                            expr = transTypes.coerce(attrEnv, apply, types.erasure(tree.type.asMethodType().restype));
+                            break;
+                    }
+                    if (!tree.type.asMethodType().restype.hasTag(VOID)) {
+                        JCReturn _return = make.at(tree.body.pos).Return(expr);
+                        tree.body = make.at(tree.body.pos).Block(0, List.of(_return));
+                    } else {
+                        JCExpressionStatement exprStm = make.at(tree.body.pos).Exec(expr);
+                        tree.body = make.at(tree.body.pos).Block(0, List.of(exprStm));
+                    }
+                }
                 super.visitMethodDef(tree);
             } finally {
                 lambdaTranslationMap = prevLambdaTranslationMap;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Oct 05 13:27:09 2018 -0700
@@ -4157,16 +4157,34 @@
                 }
             }
             JCBlock body = null;
-            JCExpression defaultValue;
+            JCExpression defaultValue = null;
+            JCExpression conciseMethodRef = null;
             if (token.kind == LBRACE) {
                 body = block();
-                defaultValue = null;
+            } else if (token.kind == ARROW || token.kind == EQ) {
+                boolean arrowForm = token.kind == ARROW;
+                int bodyPos = token.pos;
+                checkSourceLevel(bodyPos, Feature.CONCISE_METHOD_BODIES);
+                accept(token.kind);
+                JCExpression expr = parseExpression();
+                if (arrowForm) {
+                    if (!isVoid) {
+                        JCReturn _return = toP(F.at(bodyPos).Return(expr));
+                        body = F.at(bodyPos).Block(0, List.of(_return));
+                    } else {
+                        JCExpressionStatement exprStm = toP(F.at(bodyPos).Exec(expr));
+                        body = F.at(bodyPos).Block(0, List.of(exprStm));
+                    }
+                } else {
+                    conciseMethodRef = expr;
+                    body = F.at(bodyPos).Block(0, List.nil());
+                }
+                mods.flags |= Flags.CONCISE;
+                body.endpos = token.pos;
             } else {
                 if (token.kind == DEFAULT) {
                     accept(DEFAULT);
                     defaultValue = annotationValue();
-                } else {
-                    defaultValue = null;
                 }
                 accept(SEMI);
                 if (token.pos <= endPosTable.errorEndPos) {
@@ -4181,7 +4199,7 @@
             JCMethodDecl result =
                     toP(F.at(pos).MethodDef(mods, name, type, typarams,
                                             receiverParam, params, thrown,
-                                            body, defaultValue));
+                                            body, defaultValue, conciseMethodRef));
             attach(result, dc);
             return result;
         } finally {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Oct 05 13:27:09 2018 -0700
@@ -831,6 +831,8 @@
 compiler.err.native.meth.cant.have.body=\
     native methods cannot have a body
 
+compiler.err.constructors.cant.have.concise.body=\
+    constructors cannot have a concise body
 
 # 0: message segment
 compiler.misc.incompatible.type.in.conditional=\
@@ -2843,6 +2845,9 @@
 compiler.misc.feature.raw.string.literals=\
     raw string literals
 
+compiler.misc.feature.concise.method.bodies=\
+    concise method bodies
+
 compiler.warn.underscore.as.identifier=\
     as of release 9, ''_'' is a keyword, and may not be used as an identifier
 
@@ -3529,3 +3534,16 @@
 
 compiler.err.preview.without.source.or.release=\
     --enable-preview must be used with either -source or --release
+
+# concise methods
+
+# 0: string
+compiler.note.potential.concise.method.found=\
+    This method''s body: {0}\n\
+    can be turned into a concise method
+
+compiler.warn.potential.concise.method.found=\
+    This method''s body can be turned into a concise method
+
+compiler.err.only.method.references.allowed=\
+    the expression after the '=' in a concise method must be a method reference
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Fri Oct 05 13:27:09 2018 -0700
@@ -859,6 +859,11 @@
         public JCExpression defaultValue;
         /** method symbol */
         public MethodSymbol sym;
+        /** is this a concise method? reserved for those concise methods which are just wrappers
+         *  around a method reference
+         */
+        public JCExpression conciseMethodRef;
+
         protected JCMethodDecl(JCModifiers mods,
                             Name name,
                             JCExpression restype,
@@ -868,7 +873,8 @@
                             List<JCExpression> thrown,
                             JCBlock body,
                             JCExpression defaultValue,
-                            MethodSymbol sym)
+                            MethodSymbol sym,
+                            JCExpression conciseMethodRef)
         {
             this.mods = mods;
             this.name = name;
@@ -882,6 +888,7 @@
             this.body = body;
             this.defaultValue = defaultValue;
             this.sym = sym;
+            this.conciseMethodRef = conciseMethodRef;
         }
         @Override
         public void accept(Visitor v) { v.visitMethodDef(this); }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Fri Oct 05 13:27:09 2018 -0700
@@ -182,8 +182,21 @@
         }
     }
 
-    /** Is this tree an identifier, possibly qualified by 'this'?
+    /** Is this tree an identifier, possibly qualified by a chain of identifiers?
      */
+    public static boolean isIdentOrIdentDotIdent(JCTree tree) {
+        switch (tree.getTag()) {
+            case PARENS:
+                return isIdentOrThisDotIdent(skipParens(tree));
+            case IDENT:
+                return true;
+            case SELECT:
+                return isIdentOrIdentDotIdent(((JCFieldAccess)tree).selected);
+            default:
+                return false;
+        }
+    }
+
     public static boolean isIdentOrThisDotIdent(JCTree tree) {
         switch (tree.getTag()) {
             case PARENS:
@@ -920,11 +933,24 @@
             return symbol(((JCAnnotatedType) tree).underlyingType);
         case REFERENCE:
             return ((JCMemberReference) tree).sym;
+        case VARDEF:
+            return ((JCVariableDecl)tree).sym;
         default:
             return null;
         }
     }
 
+    public static List<Symbol> symbols(List<? extends JCTree> trees) {
+        ListBuffer<Symbol> lb = new ListBuffer<>();
+        for (JCTree tree : trees) {
+            Symbol sym = symbol(tree);
+            if (sym != null) {
+                lb.add(sym);
+            }
+        }
+        return lb.toList();
+    }
+
     /** Return true if this is a nonstatic selection. */
     public static boolean nonstaticSelect(JCTree tree) {
         tree = skipParens(tree);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Fri Oct 05 13:15:31 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Fri Oct 05 13:27:09 2018 -0700
@@ -182,7 +182,21 @@
                                JCExpression defaultValue) {
         return MethodDef(
                 mods, name, restype, typarams, null, params,
-                thrown, body, defaultValue);
+                thrown, body, defaultValue, null);
+    }
+
+    public JCMethodDecl MethodDef(JCModifiers mods,
+                               Name name,
+                               JCExpression restype,
+                               List<JCTypeParameter> typarams,
+                               List<JCVariableDecl> params,
+                               List<JCExpression> thrown,
+                               JCBlock body,
+                               JCExpression defaultValue,
+                               JCExpression conciseMethodRef) {
+        return MethodDef(
+                mods, name, restype, typarams, null, params,
+                thrown, body, defaultValue, conciseMethodRef);
     }
 
     public JCMethodDecl MethodDef(JCModifiers mods,
@@ -204,11 +218,38 @@
                                        thrown,
                                        body,
                                        defaultValue,
+                                       null,
                                        null);
         tree.pos = pos;
         return tree;
     }
 
+    public JCMethodDecl MethodDef(JCModifiers mods,
+                               Name name,
+                               JCExpression restype,
+                               List<JCTypeParameter> typarams,
+                               JCVariableDecl recvparam,
+                               List<JCVariableDecl> params,
+                               List<JCExpression> thrown,
+                               JCBlock body,
+                               JCExpression defaultValue,
+                               JCExpression conciseMethodRef)
+    {
+        JCMethodDecl tree = new JCMethodDecl(mods,
+                                       name,
+                                       restype,
+                                       typarams,
+                                       recvparam,
+                                       params,
+                                       thrown,
+                                       body,
+                                       defaultValue,
+                                       null,
+                                       conciseMethodRef);
+        tree.pos = pos;
+        return tree;
+    }
+
     public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init) {
         return VarDef(mods, name, vartype, init, null);
     }
@@ -984,7 +1025,8 @@
                 Types(mtype.getThrownTypes()),
                 body,
                 null,
-                m).setPos(pos).setType(mtype);
+                m,
+                null).setPos(pos).setType(mtype);
     }
 
     /** Create a type parameter tree from its name and type.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsFinderTest.java	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,51 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary smoke test for the concise method finder
+ * @compile/fail/ref=ConciseMethodsFinderTest.out -Werror -XDrawDiagnostics -XDfind=concise_method ConciseMethodsFinderTest.java
+ */
+
+class ConciseMethodsFinderTest {
+
+    class Inner {
+        String si;
+    }
+
+    Inner inner;
+
+    Inner getInner() { return inner; }
+
+    ConciseMethodsFinderTest(String s, String p) {}
+
+    // this one is a candidate
+    int length(String s) {
+        return s.length();
+    }
+
+    // this one is a candidate
+    int length2() {
+        return inner.si.length();
+    }
+
+    // this one is a candidate
+    int length3() {
+        return getInner().si.length();
+    }
+
+    // this one is a candidate
+    static ConciseMethodsFinderTest makeTest1(String s, String k) {
+        return new ConciseMethodsFinderTest(s, k);
+    }
+
+    // not a candidate
+    static ConciseMethodsFinderTest makeTest2(String s) {
+        return new ConciseMethodsFinderTest(s, "");
+    }
+
+    // not a candidate
+    int length2(String s, boolean b) { return s.length(); }
+
+    // this one is a candidate
+    String test() {
+        return new Object(){}.toString();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsFinderTest.out	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,8 @@
+ConciseMethodsFinderTest.java:20:9: compiler.warn.potential.concise.method.found
+ConciseMethodsFinderTest.java:25:9: compiler.warn.potential.concise.method.found
+ConciseMethodsFinderTest.java:30:9: compiler.warn.potential.concise.method.found
+ConciseMethodsFinderTest.java:35:37: compiler.warn.potential.concise.method.found
+ConciseMethodsFinderTest.java:48:12: compiler.warn.potential.concise.method.found
+- compiler.err.warnings.and.werror
+1 error
+5 warnings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.java	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,23 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary smoke test for the concise method feature
+ * @compile/fail/ref=ConciseMethodsNegTest01.out -XDrawDiagnostics ConciseMethodsNegTest01.java
+ */
+
+class ConciseMethodsNegTest01 {
+    int length(String s) = s::length;
+
+    abstract int length2(String s) = String::length;
+
+    native int length3(String s) = String::length;
+
+    static ConciseMethodsNegTest01 make() {
+        return new ConciseMethodsNegTest01();
+    }
+
+    ConciseMethodsNegTest01() = ConciseMethodsNegTest01::make;
+
+    private static void all() {}
+
+    public static void foo() = ConciseMethodsNegTest01.all();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsNegTest01.out	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,7 @@
+ConciseMethodsNegTest01.java:7:1: compiler.err.does.not.override.abstract: ConciseMethodsNegTest01, length2(java.lang.String), ConciseMethodsNegTest01
+ConciseMethodsNegTest01.java:8:28: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, length, compiler.misc.no.args, java.lang.String, kindname.class, java.lang.String, (compiler.misc.arg.length.mismatch)))
+ConciseMethodsNegTest01.java:10:18: compiler.err.abstract.meth.cant.have.body
+ConciseMethodsNegTest01.java:12:16: compiler.err.native.meth.cant.have.body
+ConciseMethodsNegTest01.java:18:5: compiler.err.constructors.cant.have.concise.body
+ConciseMethodsNegTest01.java:22:59: compiler.err.only.method.references.allowed
+6 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/ConciseMethodsPosTest01.java	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary smoke test for concise methods
+ * @modules jdk.compiler/com.sun.tools.javac.util
+ */
+
+import com.sun.tools.javac.util.Assert;
+
+public class ConciseMethodsPosTest01 {
+    String hello = "hello";
+
+    int length(String s) -> s.length();
+    void printLength(String s) -> System.out.println(s.length());
+
+    int length2(String s) = String::length;
+
+    String dowahoo(int x) = this::wahoo;
+
+    String wahoo(int x) {
+        return "wahoo " + x;
+    }
+
+    static ConciseMethodsPosTest01 make() = ConciseMethodsPosTest01::new;
+
+    class TT {
+        int i;
+        public TT(int i) {
+            this.i = i;
+        }
+    }
+
+    TT makeTT(int i) = TT::new;
+
+    int[] intArray(int i) = int[]::new;
+
+    public static void main(String... args) {
+        ConciseMethodsPosTest01 t = new ConciseMethodsPosTest01();
+        Assert.check(t.length("hey") == 3);
+        Assert.check(t.length2("hey") == 3);
+
+        Assert.check(t.dowahoo(1).equals("wahoo 1"));
+
+        ConciseMethodsPosTest01 t2 = ConciseMethodsPosTest01.make();
+        Assert.check(t2.hello.equals("hello"));
+
+        TT tt = t.makeTT(5);
+        Assert.check(tt.i == 5);
+
+        int[] arr = t.intArray(2);
+        for (int i = 0; i < 2; i++) {
+            arr[i] = i;
+            Assert.check(arr[i] == i);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/NPEQualifierIsCall.java	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary the verifier is failing when a field of the enclosing class is accessed from a concise method
+ * @compile NPEQualifierIsCall.java
+ */
+
+public class NPEQualifierIsCall implements Runnable {
+    public void run() -> null;
+
+    Runnable f(Runnable r) -> r;
+
+    void m() = f(this)::run;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/concise_methods/VerifierErrorAccessingFieldEnclosingClass.java	Fri Oct 05 13:27:09 2018 -0700
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary the verifier is failing when a field of the enclosing class is accessed from a concise method
+ * @modules jdk.compiler/com.sun.tools.javac.util
+ */
+
+import java.util.*;
+import static java.util.Objects.requireNonNull;
+
+public class VerifierErrorAccessingFieldEnclosingClass {
+    private final ArrayList<String> list = new ArrayList<>();
+
+    public void add(String student) -> list.add(requireNonNull(student));
+
+    public List<String> asList() -> new AbstractList<>() {
+        public int size() = list::size;
+        public String get(int index) = list::get;
+    };
+
+    public static void main(String[] args) {
+        var test = new VerifierErrorAccessingFieldEnclosingClass();
+        System.out.println(test.asList());
+    }
+}