8199194: Add javac support for preview features

Add support for preview features and related command line options.

Reviewed-by: jjg
This commit is contained in:
Maurizio Cimadamore 2018-04-12 13:55:20 +01:00
parent 594fb594ce
commit d9440e4e39
30 changed files with 838 additions and 6 deletions

View File

@ -287,7 +287,12 @@ public class Lint
/**
* Warn about potentially unsafe vararg methods
*/
VARARGS("varargs");
VARARGS("varargs"),
/**
* Warn about use of preview features.
*/
PREVIEW("preview");
LintCategory(String option) {
this(option, false);

View File

@ -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();
}
}

View File

@ -217,6 +217,16 @@ public enum Source {
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 ?

View File

@ -105,6 +105,8 @@ public class ClassFile {
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

View File

@ -148,6 +148,11 @@ public class ClassReader {
DeferredCompletionFailureHandler dcfh;
/**
* Support for preview language features.
*/
Preview preview;
/** The current scope where type variables are entered.
*/
protected WriteableScope typevars;
@ -270,6 +275,7 @@ public class ClassReader {
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 @@ public class ClassReader {
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;

View File

@ -49,7 +49,6 @@ import com.sun.tools.javac.jvm.Pool.DynamicMethod;
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 @@ public class ClassWriter extends ClassFile {
*/
private boolean debugstackmap;
/** Preview language level.
*/
private Preview preview;
/**
* Target class version.
*/
@ -178,6 +181,7 @@ public class ClassWriter extends ClassFile {
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 @@ public class ClassWriter extends ClassFile {
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);

View File

@ -536,6 +536,20 @@ public class Arguments {
}
}
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);

View File

@ -275,6 +275,10 @@ public class JavaCompiler {
*/
protected Source source;
/** The preview language version.
*/
protected Preview preview;
/** The module for code generation.
*/
protected Gen gen;
@ -405,6 +409,7 @@ public class JavaCompiler {
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 @@ public class JavaCompiler {
log.warning(Warnings.ProcUseProcOrImplicit);
}
chk.reportDeferredDiagnostics();
preview.reportDeferredDiagnostics();
if (log.compressedOutput) {
log.mandatoryNote(null, Notes.CompressedDiags);
}

View File

@ -330,6 +330,8 @@ public enum Option {
}
},
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 {

View File

@ -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 @@ public class JavaTokenizer {
*/
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 @@ public class JavaTokenizer {
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);
}
}

View File

@ -95,6 +95,9 @@ public class JavacParser implements Parser {
/** The Source language setting. */
private Source source;
/** The Preview language setting. */
private Preview preview;
/** The name table. */
private Names names;
@ -169,6 +172,7 @@ public class JavacParser implements Parser {
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 @@ public class JavacParser implements Parser {
}
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);
}
}

View File

@ -27,6 +27,7 @@ package com.sun.tools.javac.parser;
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 @@ public class ParserFactory {
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 @@ public class ParserFactory {
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);

View File

@ -27,6 +27,7 @@ package com.sun.tools.javac.parser;
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 @@ public class ScannerFactory {
final Log log;
final Names names;
final Source source;
final Preview preview;
final Tokens tokens;
/** Create a new scanner factory. */
@ -64,6 +66,7 @@ public class ScannerFactory {
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);
}

View File

@ -1547,6 +1547,25 @@ compiler.note.unchecked.filename.additional=\
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 @@ compiler.misc.feature.not.supported.in.source.plural=\
{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

View File

@ -252,6 +252,9 @@ javac.opt.Xlint.desc.unchecked=\
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 @@ javac.opt.userpathsfirst=\
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 @@ javac.warn.target.default.source.conflict=\
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=\

View File

@ -127,6 +127,8 @@ compiler.misc.bad.const.pool.entry # constant pool entry ha
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))

View File

@ -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<>();
}
}

View File

@ -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 = () -> {};
}
}

View File

@ -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 = () -> {};
}
}

View File

@ -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<>();
}

View File

@ -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<>();
}
}

View File

@ -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 = () -> {};
}

View File

@ -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<>();
}
}

View File

@ -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<>();
}
}

View File

@ -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<>();
}
}

View File

@ -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;
}

View File

@ -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 = () -> {};
}

View File

@ -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();
}
}

View File

@ -0,0 +1,2 @@
- compiler.err.preview.feature.disabled.classfile: Bar.class, 11
1 error

View File

@ -0,0 +1,4 @@
- compiler.warn.preview.feature.use.classfile: Bar.class, 11
- compiler.err.warnings.and.werror
1 error
1 warning