8199194: Add javac support for preview features

Add support for preview features and related command line options.

Reviewed-by: jjg
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java
index b0e981c..213abeb 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java
@@ -287,7 +287,12 @@
         /**
          * Warn about potentially unsafe vararg methods
          */
-        VARARGS("varargs");
+        VARARGS("varargs"),
+
+        /**
+         * Warn about use of preview features.
+         */
+        PREVIEW("preview");
 
         LintCategory(String option) {
             this(option, false);
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java
new file mode 100644
index 0000000..84cff05
--- /dev/null
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java
@@ -0,0 +1,204 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+package com.sun.tools.javac.code;
+
+import com.sun.tools.javac.code.Lint.LintCategory;
+import com.sun.tools.javac.code.Source.Feature;
+import com.sun.tools.javac.comp.Infer;
+import com.sun.tools.javac.jvm.Target;
+import com.sun.tools.javac.resources.CompilerProperties.Errors;
+import com.sun.tools.javac.resources.CompilerProperties.Warnings;
+import com.sun.tools.javac.util.Assert;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
+import com.sun.tools.javac.util.JCDiagnostic.Error;
+import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
+import com.sun.tools.javac.util.Log;
+import com.sun.tools.javac.util.MandatoryWarningHandler;
+import com.sun.tools.javac.util.Name;
+import com.sun.tools.javac.util.Options;
+
+import javax.tools.JavaFileObject;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static com.sun.tools.javac.main.Option.PREVIEW;
+
+/**
+ * Helper class to handle preview language features. This class maps certain language features
+ * (see {@link Feature} into 'preview' features; the mapping is completely ad-hoc, so as to allow
+ * for maximum flexibility, which allows to migrate preview feature into supported features with ease.
+ *
+ * This class acts as a centralized point against which usages of preview features are reported by
+ * clients (e.g. other javac classes). Internally, this class collects all such usages and generates
+ * diagnostics to inform the user of such usages. Such diagnostics can be enabled using the
+ * {@link LintCategory#PREVIEW} lint category, and are suppressible by usual means.
+ */
+public class Preview {
+
+    /** flag: are preview featutres enabled */
+    private final boolean enabled;
+
+    /** the diag handler to manage preview feature usage diagnostics */
+    private final MandatoryWarningHandler previewHandler;
+
+    /** test flag: should all features be considered as preview features? */
+    private final boolean forcePreview;
+
+    /** a mapping from classfile numbers to Java SE versions */
+    private final Map<Integer, Source> majorVersionToSource;
+
+
+    private final Lint lint;
+    private final Log log;
+
+    private static final Context.Key<Preview> previewKey = new Context.Key<>();
+
+    public static Preview instance(Context context) {
+        Preview instance = context.get(previewKey);
+        if (instance == null) {
+            instance = new Preview(context);
+        }
+        return instance;
+    }
+
+    Preview(Context context) {
+        context.put(previewKey, this);
+        Options options = Options.instance(context);
+        enabled = options.isSet(PREVIEW);
+        log = Log.instance(context);
+        lint = Lint.instance(context);
+        this.previewHandler =
+                new MandatoryWarningHandler(log, lint.isEnabled(LintCategory.PREVIEW), true, "preview", LintCategory.PREVIEW);
+        forcePreview = options.isSet("forcePreview");
+        majorVersionToSource = initMajorVersionToSourceMap();
+    }
+
+    private Map<Integer, Source> initMajorVersionToSourceMap() {
+        Map<Integer, Source> majorVersionToSource = new HashMap<>();
+        for (Target t : Target.values()) {
+            int major = t.majorVersion;
+            Source source = Source.lookup(t.name);
+            if (source != null) {
+                majorVersionToSource.put(major, source);
+            }
+        }
+        return majorVersionToSource;
+   }
+
+    /**
+     * Report usage of a preview feature. Usages reported through this method will affect the
+     * set of sourcefiles with dependencies on preview features.
+     * @param pos the position at which the preview feature was used.
+     * @param feature the preview feature used.
+     */
+    public void warnPreview(int pos, Feature feature) {
+        warnPreview(new SimpleDiagnosticPosition(pos), feature);
+    }
+
+    /**
+     * Report usage of a preview feature. Usages reported through this method will affect the
+     * set of sourcefiles with dependencies on preview features.
+     * @param pos the position at which the preview feature was used.
+     * @param feature the preview feature used.
+     */
+    public void warnPreview(DiagnosticPosition pos, Feature feature) {
+        Assert.check(isEnabled());
+        Assert.check(isPreview(feature));
+        if (!lint.isSuppressed(LintCategory.PREVIEW)) {
+            previewHandler.report(pos, feature.isPlural() ?
+                    Warnings.PreviewFeatureUsePlural(feature.nameFragment()) :
+                    Warnings.PreviewFeatureUse(feature.nameFragment()));
+        }
+    }
+
+    /**
+     * Report usage of a preview feature in classfile.
+     * @param classfile the name of the classfile with preview features enabled
+     * @param majorVersion the major version found in the classfile.
+     */
+    public void warnPreview(JavaFileObject classfile, int majorVersion) {
+        Assert.check(isEnabled());
+        if (!lint.isSuppressed(LintCategory.PREVIEW)) {
+            previewHandler.report(null,
+                    Warnings.PreviewFeatureUseClassfile(classfile, majorVersionToSource.get(majorVersion).name));
+        }
+    }
+
+    /**
+     * Are preview features enabled?
+     * @return true, if preview features are enabled.
+     */
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    /**
+     * Is given feature a preview feature?
+     * @param feature the feature to be tested.
+     * @return true, if given feature is a preview feature.
+     */
+    public boolean isPreview(Feature feature) {
+        //Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
+        //When real preview features will be added, this method can be implemented to return 'true'
+        //for those selected features, and 'false' for all the others.
+        return forcePreview;
+    }
+
+    /**
+     * Generate an error key which captures the fact that a given preview feature could not be used
+     * due to the preview feature support being disabled.
+     * @param feature the feature for which the diagnostic has to be generated.
+     * @return the diagnostic.
+     */
+    public Error disabledError(Feature feature) {
+        Assert.check(!isEnabled());
+        return feature.isPlural() ?
+                Errors.PreviewFeatureDisabledPlural(feature.nameFragment()) :
+                Errors.PreviewFeatureDisabled(feature.nameFragment());
+    }
+
+    /**
+     * Generate an error key which captures the fact that a preview classfile cannot be loaded
+     * due to the preview feature support being disabled.
+     * @param classfile the name of the classfile with preview features enabled
+     * @param majorVersion the major version found in the classfile.
+     */
+    public Error disabledError(JavaFileObject classfile, int majorVersion) {
+        Assert.check(!isEnabled());
+        return Errors.PreviewFeatureDisabledClassfile(classfile, majorVersionToSource.get(majorVersion).name);
+    }
+
+    /**
+     * Report any deferred diagnostics.
+     */
+    public void reportDeferredDiagnostics() {
+        previewHandler.reportDeferredDiagnostic();
+    }
+}
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java
index 822583e..9f64311 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java
@@ -217,6 +217,16 @@
                     source.compareTo(maxLevel) <= 0;
         }
 
+        public boolean isPlural() {
+            Assert.checkNonNull(optKind);
+            return optKind == DiagKind.PLURAL;
+        }
+
+        public Fragment nameFragment() {
+            Assert.checkNonNull(optFragment);
+            return optFragment;
+        }
+
         public Fragment fragment(String sourceName) {
             Assert.checkNonNull(optFragment);
             return optKind == DiagKind.NORMAL ?
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassFile.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassFile.java
index 54f316e..4daeb73 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassFile.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassFile.java
@@ -105,6 +105,8 @@
     public final static int MAX_LOCALS = 0xffff;
     public final static int MAX_STACK = 0xffff;
 
+    public final static int PREVIEW_MINOR_VERSION = 0xffff;
+
     public enum Version {
         V45_3(45, 3), // base level for all attributes
         V49(49, 0),   // JDK 1.5: enum, generics, annotations
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
index 557d042..5a1d3b9 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
@@ -148,6 +148,11 @@
 
     DeferredCompletionFailureHandler dcfh;
 
+    /**
+     * Support for preview language features.
+     */
+    Preview preview;
+
     /** The current scope where type variables are entered.
      */
     protected WriteableScope typevars;
@@ -270,6 +275,7 @@
         verbose         = options.isSet(Option.VERBOSE);
 
         Source source = Source.instance(context);
+        preview = Preview.instance(context);
         allowSimplifiedVarargs = Feature.SIMPLIFIED_VARARGS.allowedInSource(source);
         allowModules     = Feature.MODULES.allowedInSource(source);
 
@@ -2786,6 +2792,14 @@
                                    Integer.toString(maxMinor));
         }
 
+        if (minorVersion == ClassFile.PREVIEW_MINOR_VERSION) {
+            if (!preview.isEnabled()) {
+                log.error(preview.disabledError(currentClassFile, majorVersion));
+            } else {
+                preview.warnPreview(c.classfile, majorVersion);
+            }
+        }
+
         indexPool();
         if (signatureBuffer.length < bp) {
             int ns = Integer.highestOneBit(bp) << 1;
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
index e582e7b..f658b3f 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
@@ -49,7 +49,6 @@
 import com.sun.tools.javac.jvm.Pool.Method;
 import com.sun.tools.javac.jvm.Pool.MethodHandle;
 import com.sun.tools.javac.jvm.Pool.Variable;
-import com.sun.tools.javac.main.Option;
 import com.sun.tools.javac.util.*;
 
 import static com.sun.tools.javac.code.Flags.*;
@@ -89,6 +88,10 @@
      */
     private boolean debugstackmap;
 
+    /** Preview language level.
+     */
+    private Preview preview;
+
     /**
      * Target class version.
      */
@@ -178,6 +181,7 @@
         log = Log.instance(context);
         names = Names.instance(context);
         options = Options.instance(context);
+        preview = Preview.instance(context);
         target = Target.instance(context);
         source = Source.instance(context);
         types = Types.instance(context);
@@ -1819,7 +1823,11 @@
         acount += writeExtraClassAttributes(c);
 
         poolbuf.appendInt(JAVA_MAGIC);
-        poolbuf.appendChar(target.minorVersion);
+        if (preview.isEnabled()) {
+            poolbuf.appendChar(ClassFile.PREVIEW_MINOR_VERSION);
+        } else {
+            poolbuf.appendChar(target.minorVersion);
+        }
         poolbuf.appendChar(target.majorVersion);
 
         writePool(c.pool);
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java
index 70d7be3..99853cc 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java
@@ -536,6 +536,20 @@
             }
         }
 
+        if (options.isSet(Option.PREVIEW)) {
+            if (sourceString == null) {
+                //enable-preview must be used with explicit -source or --release
+                error("err.preview.without.source.or.release");
+                return false;
+            } else if (source != Source.DEFAULT) {
+                //enable-preview must be used with latest source version
+                error("err.preview.not.latest",
+                        sourceString,
+                        Source.DEFAULT.name);
+                return false;
+            }
+        }
+
         String profileString = options.get(Option.PROFILE);
         if (profileString != null) {
             Profile profile = Profile.lookup(profileString);
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
index 954bb9c..54d5676 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
@@ -275,6 +275,10 @@
      */
     protected Source source;
 
+    /** The preview language version.
+     */
+    protected Preview preview;
+
     /** The module for code generation.
      */
     protected Gen gen;
@@ -405,6 +409,7 @@
             log.error(Errors.CantAccess(ex.sym, ex.getDetailValue()));
         }
         source = Source.instance(context);
+        preview = Preview.instance(context);
         attr = Attr.instance(context);
         analyzer = Analyzer.instance(context);
         chk = Check.instance(context);
@@ -1725,6 +1730,7 @@
                 log.warning(Warnings.ProcUseProcOrImplicit);
         }
         chk.reportDeferredDiagnostics();
+        preview.reportDeferredDiagnostics();
         if (log.compressedOutput) {
             log.mandatoryNote(null, Notes.CompressedDiags);
         }
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
index 23bfdec..8c7a3da 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
@@ -330,6 +330,8 @@
         }
     },
 
+    PREVIEW("--enable-preview", "opt.preview", STANDARD, BASIC),
+
     PROFILE("-profile", "opt.arg.profile", "opt.profile", STANDARD, BASIC) {
         @Override
         public void process(OptionHelper helper, String option, String operand) throws InvalidValueException {
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java
index df1d5bb..35fd224 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java
@@ -25,6 +25,7 @@
 
 package com.sun.tools.javac.parser;
 
+import com.sun.tools.javac.code.Preview;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.code.Source.Feature;
 import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
@@ -53,6 +54,9 @@
      */
     private Source source;
 
+    /** The preview language setting. */
+    private Preview preview;
+
     /** The log to be used for error reporting.
      */
     private final Log log;
@@ -115,12 +119,20 @@
         this.log = fac.log;
         this.tokens = fac.tokens;
         this.source = fac.source;
+        this.preview = fac.preview;
         this.reader = reader;
     }
 
-    private void checkSourceLevel(int pos, Feature feature) {
-        if (!feature.allowedInSource(source)) {
+    protected void checkSourceLevel(int pos, Feature feature) {
+        if (preview.isPreview(feature) && !preview.isEnabled()) {
+            //preview feature without --preview flag, error
+            lexError(DiagnosticFlag.SOURCE_LEVEL, pos, preview.disabledError(feature));
+        } else if (!feature.allowedInSource(source)) {
+            //incompatible source level, error
             lexError(DiagnosticFlag.SOURCE_LEVEL, pos, feature.error(source.name));
+        } else if (preview.isPreview(feature)) {
+            //use of preview feature, warn
+            preview.warnPreview(pos, feature);
         }
     }
 
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
index 05d81f9..82dddd4 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
@@ -95,6 +95,9 @@
     /** The Source language setting. */
     private Source source;
 
+    /** The Preview language setting. */
+    private Preview preview;
+
     /** The name table. */
     private Names names;
 
@@ -169,6 +172,7 @@
         this.log = fac.log;
         this.names = fac.names;
         this.source = fac.source;
+        this.preview = fac.preview;
         this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true);
         this.keepDocComments = keepDocComments;
         this.parseModuleInfo = parseModuleInfo;
@@ -4219,8 +4223,15 @@
     }
 
     protected void checkSourceLevel(int pos, Feature feature) {
-        if (!feature.allowedInSource(source)) {
+        if (preview.isPreview(feature) && !preview.isEnabled()) {
+            //preview feature without --preview flag, error
+            log.error(DiagnosticFlag.SOURCE_LEVEL, pos, preview.disabledError(feature));
+        } else if (!feature.allowedInSource(source)) {
+            //incompatible source level, error
             log.error(DiagnosticFlag.SOURCE_LEVEL, pos, feature.error(source.name));
+        } else if (preview.isPreview(feature)) {
+            //use of preview feature, warn
+            preview.warnPreview(pos, feature);
         }
     }
 
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java
index e9c53c9..e699a81 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ParserFactory.java
@@ -27,6 +27,7 @@
 
 import java.util.Locale;
 
+import com.sun.tools.javac.code.Preview;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.tree.DocTreeMaker;
 import com.sun.tools.javac.tree.TreeMaker;
@@ -62,6 +63,7 @@
     final Log log;
     final Tokens tokens;
     final Source source;
+    final Preview preview;
     final Names names;
     final Options options;
     final ScannerFactory scannerFactory;
@@ -76,6 +78,7 @@
         this.names = Names.instance(context);
         this.tokens = Tokens.instance(context);
         this.source = Source.instance(context);
+        this.preview = Preview.instance(context);
         this.options = Options.instance(context);
         this.scannerFactory = ScannerFactory.instance(context);
         this.locale = context.get(Locale.class);
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ScannerFactory.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ScannerFactory.java
index 838f13b..c15a1e3 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ScannerFactory.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/ScannerFactory.java
@@ -27,6 +27,7 @@
 
 import java.nio.CharBuffer;
 
+import com.sun.tools.javac.code.Preview;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.Log;
@@ -56,6 +57,7 @@
     final Log log;
     final Names names;
     final Source source;
+    final Preview preview;
     final Tokens tokens;
 
     /** Create a new scanner factory. */
@@ -64,6 +66,7 @@
         this.log = Log.instance(context);
         this.names = Names.instance(context);
         this.source = Source.instance(context);
+        this.preview = Preview.instance(context);
         this.tokens = Tokens.instance(context);
     }
 
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
index 7d5891b..169529a 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
@@ -1547,6 +1547,25 @@
 compiler.note.unchecked.plural.additional=\
     Some input files additionally use unchecked or unsafe operations.
 
+# 0: file name
+compiler.note.preview.filename=\
+    {0} uses preview language features.
+
+compiler.note.preview.plural=\
+    Some input files use preview language features.
+
+# The following string may appear after one of the above deprecation
+# messages.
+compiler.note.preview.recompile=\
+    Recompile with -Xlint:preview for details.
+
+# 0: file name
+compiler.note.preview.filename.additional=\
+    {0} has additional uses of preview language features.
+
+compiler.note.preview.plural.additional=\
+    Some input files additionally use preview language features.
+
 # Notes related to annotation processing
 
 # Print a client-generated note; assumed to be localized, no translation required
@@ -2665,6 +2684,34 @@
    {0} are not supported in -source {1}\n\
     (use -source {2} or higher to enable {0})
 
+# 0: message segment (feature)
+compiler.err.preview.feature.disabled=\
+   {0} is a preview feature and is disabled by default.\n\
+   (use --enable-preview to enable {0})
+
+# 0: message segment (feature)
+compiler.err.preview.feature.disabled.plural=\
+   {0} are a preview feature and are disabled by default.\n\
+   (use --enable-preview to enable {0})
+
+# 0: file object (classfile), 1: string (expected version)
+compiler.err.preview.feature.disabled.classfile=\
+   classfile for {0} uses preview features of Java SE {1}.\n\
+   (use --enable-preview to allow loading of classfiles which contain preview features)
+
+# 0: message segment (feature)
+compiler.warn.preview.feature.use=\
+   {0} is a preview feature and may be removed in a future release.
+
+# 0: message segment (feature)
+compiler.warn.preview.feature.use.plural=\
+   {0} are a preview feature and may be removed in a future release.
+
+# 0: file object (classfile), 1: string (expected version)
+compiler.warn.preview.feature.use.classfile=\
+   classfile for {0} uses preview features of Java SE {1}.
+
+
 compiler.misc.feature.modules=\
     modules
 
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
index 67e77e3..169f697 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
@@ -252,6 +252,9 @@
 javac.opt.Xlint.desc.varargs=\
     Warn about potentially unsafe vararg methods
 
+javac.opt.Xlint.desc.preview=\
+    Warn about use of preview language features
+
 javac.opt.Xdoclint=\
     Enable recommended checks for problems in javadoc comments
 # L10N: do not localize: all none
@@ -293,6 +296,8 @@
     Search classpath and sourcepath for classes before the bootclasspath instead of after
 javac.opt.prefer=\
     Specify which file to read when both a source file and class file are found for an implicitly compiled class
+javac.opt.preview=\
+    Enable preview language features. To be used in conjunction with either -source or --release.
 javac.opt.AT=\
     Read options and filenames from file
 javac.opt.diags=\
@@ -368,6 +373,11 @@
     target release {0} conflicts with default source release {1}
 javac.warn.profile.target.conflict=\
     profile {0} is not valid for target release {1}
+javac.err.preview.not.latest=\
+    invalid source release {0} with --enable-preview\n\
+    (preview language features are only supported for release {1})
+javac.err.preview.without.source.or.release=\
+    --enable-preview must be used with either -source or --release
 javac.err.file.not.found=\
     file not found: {0}
 javac.err.file.not.directory=\
diff --git a/test/langtools/tools/javac/diags/examples.not-yet.txt b/test/langtools/tools/javac/diags/examples.not-yet.txt
index ff90131..a3d227a 100644
--- a/test/langtools/tools/javac/diags/examples.not-yet.txt
+++ b/test/langtools/tools/javac/diags/examples.not-yet.txt
@@ -127,6 +127,8 @@
 compiler.warn.access.to.member.from.serializable.lambda # in order to generate it we need to modify a restricted package
 compiler.warn.invalid.path                              # this warning is generated only in Windows systems
 compiler.note.multiple.elements                         # needs user code
+compiler.err.preview.feature.disabled.classfile         # preview feature support: needs compilation against classfile
+compiler.warn.preview.feature.use.classfile             # preview feature support: needs compilation against classfile
 
 # The following module-related messages will have to stay on the not-yet list for various reasons:
 compiler.warn.locn.unknown.file.on.module.path                # Never issued ATM (short circuited with an if (false))
diff --git a/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabled.java b/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabled.java
new file mode 100644
index 0000000..3aaeccc
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabled.java
@@ -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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.err.preview.feature.disabled
+// key: compiler.misc.feature.diamond
+// options: -XDforcePreview
+
+import java.util.ArrayList;
+
+class PreviewFeatureDisabled {
+    void m() {
+        new ArrayList<>();
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabledPlural.java b/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabledPlural.java
new file mode 100644
index 0000000..734dab0
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewFeatureDisabledPlural.java
@@ -0,0 +1,34 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.err.preview.feature.disabled.plural
+// key: compiler.misc.feature.lambda
+// options: -XDforcePreview
+
+class PreviewFeatureDisabledPlural {
+    void m() {
+        Runnable r = () -> {};
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewFeatureUse.java b/test/langtools/tools/javac/diags/examples/PreviewFeatureUse.java
new file mode 100644
index 0000000..b280d90
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewFeatureUse.java
@@ -0,0 +1,39 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+//key: compiler.warn.preview.feature.use
+//key: compiler.warn.preview.feature.use.plural
+//key: compiler.misc.feature.diamond
+//key: compiler.misc.feature.lambda
+//options: -Xlint:preview -XDforcePreview -source 11 --enable-preview
+
+import java.util.ArrayList;
+
+class PreviewFeatureUse {
+    void test() {
+        new ArrayList<>();
+        Runnable r = () -> {};
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewFilename.java b/test/langtools/tools/javac/diags/examples/PreviewFilename.java
new file mode 100644
index 0000000..7a58e96
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewFilename.java
@@ -0,0 +1,35 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.note.preview.filename
+// key: compiler.note.preview.recompile
+// options: -XDforcePreview  -source 11 --enable-preview
+
+import java.util.ArrayList;
+import java.util.List;
+
+class PreviewFilename {
+    List<String> ls = new ArrayList<>();
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewFilenameAdditional.java b/test/langtools/tools/javac/diags/examples/PreviewFilenameAdditional.java
new file mode 100644
index 0000000..b813fd0
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewFilenameAdditional.java
@@ -0,0 +1,38 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.note.preview.filename.additional
+// key: compiler.warn.preview.feature.use
+// key: compiler.misc.feature.diamond
+// options: -Xlint:preview -Xmaxwarns 1 -XDforcePreview  -source 11 --enable-preview
+
+import java.util.ArrayList;
+
+class PreviewFilenameAdditional {
+    void test() {
+        new ArrayList<>();
+        new ArrayList<>();
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewPlural/Bar.java b/test/langtools/tools/javac/diags/examples/PreviewPlural/Bar.java
new file mode 100644
index 0000000..a2d6205
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewPlural/Bar.java
@@ -0,0 +1,28 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+class Bar {
+    Runnable r = () -> {};
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewPlural/PreviewPlural.java b/test/langtools/tools/javac/diags/examples/PreviewPlural/PreviewPlural.java
new file mode 100644
index 0000000..a43f435
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewPlural/PreviewPlural.java
@@ -0,0 +1,37 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.note.preview.plural
+// key: compiler.note.preview.recompile
+// options: -XDforcePreview  -source 11 --enable-preview
+
+import java.util.ArrayList;
+
+class PreviewPlural {
+    void test() {
+        new Bar();
+        new ArrayList<>();
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/Bar.java b/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/Bar.java
new file mode 100644
index 0000000..0b24356
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/Bar.java
@@ -0,0 +1,33 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+import java.util.ArrayList;
+
+class Bar {
+    Runnable r = () -> {};
+    void test() {
+        new ArrayList<>();
+    }
+}
diff --git a/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/PreviewPluralAdditional.java b/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/PreviewPluralAdditional.java
new file mode 100644
index 0000000..2bcb2da
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/PreviewPluralAdditional/PreviewPluralAdditional.java
@@ -0,0 +1,38 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+// key: compiler.note.preview.plural.additional
+// key: compiler.warn.preview.feature.use.plural
+// key: compiler.misc.feature.lambda
+// options: -Xlint:preview -Xmaxwarns 1 -XDforcePreview  -source 11 --enable-preview
+
+import java.util.ArrayList;
+
+class PreviewPlural {
+    void test() {
+        new Bar();
+        new ArrayList<>();
+    }
+}
diff --git a/test/langtools/tools/javac/preview/PreviewOptionTest.java b/test/langtools/tools/javac/preview/PreviewOptionTest.java
new file mode 100644
index 0000000..80db6ba
--- /dev/null
+++ b/test/langtools/tools/javac/preview/PreviewOptionTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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
+ * @bug 8199194
+ * @summary smoke test for enable-preview command line flag
+ * @modules jdk.compiler/com.sun.tools.javac.code
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.sun.tools.javac.code.Source;
+
+public class PreviewOptionTest {
+    public static void main(String... args) throws Exception {
+        PreviewOptionTest t = new PreviewOptionTest();
+        t.run();
+    }
+
+    public void run() throws Exception {
+        try (FileWriter out = new FileWriter("Test.java")) {
+            out.write("class Test { }");
+        }
+
+        testWithNoFlags();
+
+        List<Source> versionsToTest = Stream.of(Source.values())
+                .filter(s -> s.compareTo(Source.MIN) >= 0)
+                .collect(Collectors.toList());
+
+        versionsToTest.stream().forEach(this::testWithSourceFlag);
+        versionsToTest.stream().forEach(this::testWithReleaseFlag);
+
+        if (errors > 0)
+            throw new Exception(errors + " errors occurred");
+    }
+
+    void testWithNoFlags() {
+        testInternal(null, null, true);
+    }
+
+    void testWithSourceFlag(Source source) {
+        testInternal(source, null, source != Source.DEFAULT);
+    }
+
+    void testWithReleaseFlag(Source release) {
+        //Todo: the condition below should say "release != Source.DEFAULT", but we can't do that
+        //since --release 11 is not supported yet.
+        testInternal(null, release, true);
+    }
+
+    void testInternal(Source source, Source release, boolean shouldFail) {
+        System.err.println("Test: source:" + source + ", release:" + release + " " + shouldFail + " " + shouldFail);
+        List<String> args = new ArrayList<>();
+        args.add("--enable-preview");
+        if (source != null) {
+            args.add("-source");
+            args.add(source.name);
+        }
+        if (release != null) {
+            args.add("--release");
+            args.add(release.name);
+        }
+        args.add("Test.java");
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
+        pw.close();
+        boolean hasErrors = rc != 0;
+        if (hasErrors != shouldFail) {
+            if (hasErrors) {
+                String out = sw.toString();
+                error("error not expected but found:\n" + out);
+            } else {
+                error("error expected but not found");
+            }
+        }
+    }
+
+    void error(String msg) {
+        System.err.println("error: " + msg);
+        errors++;
+    }
+
+    int errors;
+}
diff --git a/test/langtools/tools/javac/preview/classReaderTest/Bar.java b/test/langtools/tools/javac/preview/classReaderTest/Bar.java
new file mode 100644
index 0000000..65a0687
--- /dev/null
+++ b/test/langtools/tools/javac/preview/classReaderTest/Bar.java
@@ -0,0 +1,28 @@
+/*
+ * 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+public class Bar {
+    Runnable r = () -> {};
+}
diff --git a/test/langtools/tools/javac/preview/classReaderTest/Client.java b/test/langtools/tools/javac/preview/classReaderTest/Client.java
new file mode 100644
index 0000000..91ac53e
--- /dev/null
+++ b/test/langtools/tools/javac/preview/classReaderTest/Client.java
@@ -0,0 +1,14 @@
+/*
+ * @test /nodynamioccopyright/
+ * @bug 8199194
+ * @summary smoke test for --enabled-preview classreader support
+ * @compile -XDforcePreview --enable-preview -source 11 Bar.java
+ * @compile/fail/ref=Client.nopreview.out -Xlint:preview -XDrawDiagnostics Client.java
+ * @compile/fail/ref=Client.preview.out -Werror -Xlint:preview -XDrawDiagnostics --enable-preview -source 11 Client.java
+ */
+
+public class Client {
+    void test() {
+        new Bar();
+    }
+}
diff --git a/test/langtools/tools/javac/preview/classReaderTest/Client.nopreview.out b/test/langtools/tools/javac/preview/classReaderTest/Client.nopreview.out
new file mode 100644
index 0000000..967077d5
--- /dev/null
+++ b/test/langtools/tools/javac/preview/classReaderTest/Client.nopreview.out
@@ -0,0 +1,2 @@
+- compiler.err.preview.feature.disabled.classfile: Bar.class, 11
+1 error
diff --git a/test/langtools/tools/javac/preview/classReaderTest/Client.preview.out b/test/langtools/tools/javac/preview/classReaderTest/Client.preview.out
new file mode 100644
index 0000000..43212d9
--- /dev/null
+++ b/test/langtools/tools/javac/preview/classReaderTest/Client.preview.out
@@ -0,0 +1,4 @@
+- compiler.warn.preview.feature.use.classfile: Bar.class, 11
+- compiler.err.warnings.and.werror
+1 error
+1 warning