8004182: Add support for profiles in javac
authorjjg
Mon, 21 Jan 2013 01:27:42 -0500
changeset 15724 3063fb01c8a1
parent 15723 58a73dac9ee4
child 15725 e0516b913894
8004182: Add support for profiles in javac Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
langtools/src/share/classes/com/sun/tools/javac/code/Flags.java
langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/src/share/classes/com/sun/tools/javac/jvm/Profile.java
langtools/src/share/classes/com/sun/tools/javac/jvm/Target.java
langtools/src/share/classes/com/sun/tools/javac/main/Main.java
langtools/src/share/classes/com/sun/tools/javac/main/Option.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/src/share/classes/com/sun/tools/javac/resources/javac.properties
langtools/src/share/classes/com/sun/tools/javac/sym/CreateSymbols.java
langtools/src/share/classes/com/sun/tools/javac/sym/Profiles.java
langtools/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java
langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java
langtools/test/tools/javac/diags/examples/NotInProfile.java
langtools/test/tools/javac/profiles/ProfileOptionTest.java
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Mon Jan 21 01:27:42 2013 -0500
@@ -154,10 +154,11 @@
                 throw new IllegalStateException();
         } else {
             initContext();
+            compilerMain.log = Log.instance(context);
             compilerMain.setOptions(Options.instance(context));
             compilerMain.filenames = new LinkedHashSet<File>();
             Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args), classNames);
-            if (!filenames.isEmpty())
+            if (filenames != null && !filenames.isEmpty())
                 throw new IllegalArgumentException("Malformed arguments " + toString(filenames, " "));
             compiler = JavaCompiler.instance(context);
             compiler.keepComments = true;
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Flags.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Flags.java	Mon Jan 21 01:27:42 2013 -0500
@@ -264,6 +264,11 @@
      */
     public static final long AUXILIARY = 1L<<44;
 
+    /**
+     * Flag that marks that a symbol is not available in the current profile
+     */
+    public static final long NOT_IN_PROFILE = 1L<<45;
+
     /** Modifier masks.
      */
     public static final int
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java	Mon Jan 21 01:27:42 2013 -0500
@@ -156,6 +156,7 @@
     public final Type deprecatedType;
     public final Type suppressWarningsType;
     public final Type inheritedType;
+    public final Type profileType;
     public final Type proprietaryType;
     public final Type systemType;
     public final Type autoCloseableType;
@@ -360,6 +361,22 @@
 
     }
 
+    // Enter a synthetic class that is used to mark classes in ct.sym.
+    // This class does not have a class file.
+    private Type enterSyntheticAnnotation(String name) {
+        ClassType type = (ClassType)enterClass(name);
+        ClassSymbol sym = (ClassSymbol)type.tsym;
+        sym.completer = null;
+        sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
+        sym.erasure_field = type;
+        sym.members_field = new Scope(sym);
+        type.typarams_field = List.nil();
+        type.allparams_field = List.nil();
+        type.supertype_field = annotationType;
+        type.interfaces_field = List.nil();
+        return type;
+    }
+
     /** Constructor; enters all predefined identifiers and operators
      *  into symbol table.
      */
@@ -521,17 +538,13 @@
         // Enter a synthetic class that is used to mark internal
         // proprietary classes in ct.sym.  This class does not have a
         // class file.
-        ClassType proprietaryType = (ClassType)enterClass("sun.Proprietary+Annotation");
-        this.proprietaryType = proprietaryType;
-        ClassSymbol proprietarySymbol = (ClassSymbol)proprietaryType.tsym;
-        proprietarySymbol.completer = null;
-        proprietarySymbol.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
-        proprietarySymbol.erasure_field = proprietaryType;
-        proprietarySymbol.members_field = new Scope(proprietarySymbol);
-        proprietaryType.typarams_field = List.nil();
-        proprietaryType.allparams_field = List.nil();
-        proprietaryType.supertype_field = annotationType;
-        proprietaryType.interfaces_field = List.nil();
+        proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
+
+        // Enter a synthetic class that is used to provide profile info for
+        // classes in ct.sym.  This class does not have a class file.
+        profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
+        MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
+        profileType.tsym.members().enter(m);
 
         // Enter a class for arrays.
         // The class implements java.lang.Cloneable and java.io.Serializable.
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jan 21 01:27:42 2013 -0500
@@ -3274,6 +3274,7 @@
             if (sym.name != names.init) {
                 chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym);
                 chk.checkSunAPI(tree.pos(), sym);
+                chk.checkProfile(tree.pos(), sym);
             }
 
             // Test (3): if symbol is a variable, check that its type and
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Jan 21 01:27:42 2013 -0500
@@ -79,6 +79,7 @@
     private boolean enableSunApiLintControl;
     private final TreeInfo treeinfo;
     private final JavaFileManager fileManager;
+    private final Profile profile;
 
     // The set of lint options currently in effect. It is initialized
     // from the context, and then is set/reset as needed by Attr as it
@@ -106,7 +107,7 @@
         enter = Enter.instance(context);
         deferredAttr = DeferredAttr.instance(context);
         infer = Infer.instance(context);
-        this.types = Types.instance(context);
+        types = Types.instance(context);
         diags = JCDiagnostic.Factory.instance(context);
         Options options = Options.instance(context);
         lint = Lint.instance(context);
@@ -129,6 +130,8 @@
         Target target = Target.instance(context);
         syntheticNameChar = target.syntheticNameChar();
 
+        profile = Profile.instance(context);
+
         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
         boolean verboseSunApi = lint.isEnabled(LintCategory.SUNAPI);
@@ -3006,6 +3009,12 @@
         }
     }
 
+    void checkProfile(final DiagnosticPosition pos, final Symbol s) {
+        if (profile != Profile.DEFAULT && (s.flags() & NOT_IN_PROFILE) != 0) {
+            log.error(pos, "not.in.profile", s, profile);
+        }
+    }
+
 /* *************************************************************************
  * Check for recursive annotation elements.
  **************************************************************************/
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Mon Jan 21 01:27:42 2013 -0500
@@ -134,6 +134,11 @@
      **/
     public boolean preferSource;
 
+    /**
+     * The currently selected profile.
+     */
+    public final Profile profile;
+
     /** The log to use for verbose output
      */
     final Log log;
@@ -284,16 +289,20 @@
         annotate = Annotate.instance(context);
         verbose        = options.isSet(VERBOSE);
         checkClassFile = options.isSet("-checkclassfile");
+
         Source source = Source.instance(context);
         allowGenerics    = source.allowGenerics();
         allowVarargs     = source.allowVarargs();
         allowAnnotations = source.allowAnnotations();
         allowSimplifiedVarargs = source.allowSimplifiedVarargs();
         allowDefaultMethods = source.allowDefaultMethods();
+
         saveParameterNames = options.isSet("save-parameter-names");
         cacheCompletionFailure = options.isUnset("dev");
         preferSource = "source".equals(options.get("-Xprefer"));
 
+        profile = Profile.instance(context);
+
         completionFailureName =
             options.isSet("failcomplete")
             ? names.fromString(options.get("failcomplete"))
@@ -1357,7 +1366,18 @@
                 CompoundAnnotationProxy proxy = readCompoundAnnotation();
                 if (proxy.type.tsym == syms.proprietaryType.tsym)
                     sym.flags_field |= PROPRIETARY;
-                else
+                else if (proxy.type.tsym == syms.profileType.tsym) {
+                    if (profile != Profile.DEFAULT) {
+                        for (Pair<Name,Attribute> v: proxy.values) {
+                            if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
+                                Attribute.Constant c = (Attribute.Constant) v.snd;
+                                if (c.type == syms.intType && ((Integer) c.value) > profile.value) {
+                                    sym.flags_field |= NOT_IN_PROFILE;
+                                }
+                            }
+                        }
+                    }
+                } else
                     proxies.append(proxy);
             }
             annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/Profile.java	Mon Jan 21 01:27:42 2013 -0500
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2002, 2013, 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.jvm;
+
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.Options;
+import java.util.EnumSet;
+import java.util.Set;
+
+import static com.sun.tools.javac.main.Option.PROFILE;
+
+/** The target profile.
+ *
+ *  <p><b>This is NOT part of any supported API.
+ *  If you write code that depends on this, you do so at your own risk.
+ *  This code and its internal interfaces are subject to change or
+ *  deletion without notice.</b>
+ */
+public enum Profile {
+    COMPACT1("compact1", 1, Target.JDK1_8),
+    COMPACT2("compact2", 2, Target.JDK1_8),
+    COMPACT3("compact3", 3, Target.JDK1_8),
+
+    DEFAULT {
+        @Override
+        public boolean isValid(Target t) {
+            return true;
+        }
+    };
+
+    private static final Context.Key<Profile> profileKey =
+        new Context.Key<Profile>();
+
+    public static Profile instance(Context context) {
+        Profile instance = context.get(profileKey);
+        if (instance == null) {
+            Options options = Options.instance(context);
+            String profileString = options.get(PROFILE);
+            if (profileString != null) instance = lookup(profileString);
+            if (instance == null) instance = DEFAULT;
+            context.put(profileKey, instance);
+        }
+        return instance;
+    }
+
+    public final String name;
+    public final int value;
+    final Set<Target> targets;
+
+    Profile() {
+        name = null;
+        value = Integer.MAX_VALUE;
+        targets = null;
+    }
+
+    Profile(String name, int value, Target t, Target... targets) {
+        this.name = name;
+        this.value = value;
+        this.targets = EnumSet.of(t, targets);
+    }
+
+    public static Profile lookup(String name) {
+        // the set of values is small enough to do linear search
+        for (Profile p: values()) {
+            if (name.equals(p.name))
+                return p;
+        }
+        return null;
+    }
+
+    public static Profile lookup(int value) {
+        // the set of values is small enough to do linear search
+        for (Profile p: values()) {
+            if (value == p.value)
+                return p;
+        }
+        return null;
+    }
+
+    public boolean isValid(Target t) {
+        return targets.contains(t);
+    }
+}
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/Target.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/Target.java	Mon Jan 21 01:27:42 2013 -0500
@@ -31,7 +31,7 @@
 import com.sun.tools.javac.code.Symbol;
 import com.sun.tools.javac.util.*;
 
-import static com.sun.tools.javac.main.Option.*;
+import static com.sun.tools.javac.main.Option.TARGET;
 
 /** The classfile version target.
  *
--- a/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Mon Jan 21 01:27:42 2013 -0500
@@ -49,6 +49,7 @@
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.file.CacheFSInfo;
 import com.sun.tools.javac.file.JavacFileManager;
+import com.sun.tools.javac.jvm.Profile;
 import com.sun.tools.javac.jvm.Target;
 import com.sun.tools.javac.processing.AnnotationProcessingError;
 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
@@ -76,7 +77,7 @@
 
     /** The log to use for diagnostic output.
      */
-    Log log;
+    public Log log;
 
     /**
      * If true, certain errors will cause an exception, such as command line
@@ -165,6 +166,7 @@
         this.ownName = name;
         this.out = out;
     }
+
     /** A table of all options that's passed to the JavaCompiler constructor.  */
     private Options options = null;
 
@@ -307,6 +309,15 @@
             }
         }
 
+        String profileString = options.get(PROFILE);
+        if (profileString != null) {
+            Profile profile = Profile.lookup(profileString);
+            if (!profile.isValid(target)) {
+                warning("warn.profile.target.conflict", profileString, target.name);
+                return null;
+            }
+        }
+
         // handle this here so it works even if no other options given
         String showClass = options.get("showClass");
         if (showClass != null) {
--- a/langtools/src/share/classes/com/sun/tools/javac/main/Option.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/Option.java	Mon Jan 21 01:27:42 2013 -0500
@@ -40,6 +40,7 @@
 import com.sun.tools.javac.code.Lint;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.code.Type;
+import com.sun.tools.javac.jvm.Profile;
 import com.sun.tools.javac.jvm.Target;
 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
 import com.sun.tools.javac.util.Log;
@@ -218,6 +219,18 @@
         }
     },
 
+    PROFILE("-profile", "opt.arg.profile", "opt.profile", STANDARD, BASIC) {
+        @Override
+        public boolean process(OptionHelper helper, String option, String operand) {
+            Profile profile = Profile.lookup(operand);
+            if (profile == null) {
+                helper.error("err.invalid.profile", operand);
+                return true;
+            }
+            return super.process(helper, option, operand);
+        }
+    },
+
     VERSION("-version", "opt.version", STANDARD, INFO) {
         @Override
         public boolean process(OptionHelper helper, String option) {
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jan 21 01:27:42 2013 -0500
@@ -492,6 +492,10 @@
 compiler.err.illegal.forward.ref=\
     illegal forward reference
 
+# 0: symbol, 1: string
+compiler.err.not.in.profile=\
+    {0} is not available in profile ''{1}''
+
 # 0: symbol
 compiler.warn.forward.ref=\
     reference to variable ''{0}'' before it has been initialized
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/javac.properties	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/javac.properties	Mon Jan 21 01:27:42 2013 -0500
@@ -69,6 +69,8 @@
     Pass <flag> directly to the runtime system
 javac.opt.encoding=\
     Specify character encoding used by source files
+javac.opt.profile=\
+    Check that API used is available in the specified profile
 javac.opt.target=\
     Generate class files for specific VM version
 javac.opt.source=\
@@ -97,6 +99,8 @@
     <directory>
 javac.opt.arg.encoding=\
     <encoding>
+javac.opt.arg.profile=\
+    <profile>
 javac.opt.arg.release=\
     <release>
 javac.opt.arg.number=\
@@ -175,6 +179,8 @@
      key in annotation processor option ''{0}'' is not a dot-separated sequence of identifiers
 javac.err.invalid.flag=\
     invalid flag: {0}
+javac.err.invalid.profile=\
+    invalid profile: {0}
 javac.err.invalid.target=\
     invalid target release: {0}
 javac.err.no.source.files=\
@@ -191,6 +197,8 @@
     source release {0} requires target release {1}
 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.dir.not.found=\
     directory not found: {0}
 javac.err.file.not.found=\
--- a/langtools/src/share/classes/com/sun/tools/javac/sym/CreateSymbols.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/sym/CreateSymbols.java	Mon Jan 21 01:27:42 2013 -0500
@@ -34,11 +34,11 @@
 import com.sun.tools.javac.code.Symtab;
 import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.code.Types;
-import com.sun.tools.javac.jvm.ClassReader;
 import com.sun.tools.javac.jvm.ClassWriter;
 import com.sun.tools.javac.jvm.Pool;
 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
 import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Names;
 import com.sun.tools.javac.util.Pair;
 
 import java.io.File;
@@ -47,6 +47,7 @@
 import java.util.EnumSet;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.ResourceBundle;
 import java.util.Set;
 
@@ -85,7 +86,10 @@
  *
  * @author Peter von der Ah\u00e9
  */
-@SupportedOptions({"com.sun.tools.javac.sym.Jar","com.sun.tools.javac.sym.Dest"})
+@SupportedOptions({
+    "com.sun.tools.javac.sym.Jar",
+    "com.sun.tools.javac.sym.Dest",
+    "com.sun.tools.javac.sym.Profiles"})
 @SupportedAnnotationTypes("*")
 public class CreateSymbols extends AbstractProcessor {
 
@@ -106,6 +110,7 @@
             processingEnv.getMessager()
                 .printMessage(Diagnostic.Kind.ERROR, e.getLocalizedMessage());
         } catch (Throwable t) {
+            t.printStackTrace();
             Throwable cause = t.getCause();
             if (cause == null)
                 cause = t;
@@ -121,12 +126,17 @@
         Set<String> documented = new HashSet<String>();
         Set<PackageSymbol> packages =
             ((JavacProcessingEnvironment)processingEnv).getSpecifiedPackages();
-        String jarName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Jar");
+        Map<String,String> pOptions = processingEnv.getOptions();
+        String jarName = pOptions.get("com.sun.tools.javac.sym.Jar");
         if (jarName == null)
             throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR");
-        String destName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Dest");
+        String destName = pOptions.get("com.sun.tools.javac.sym.Dest");
         if (destName == null)
             throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR");
+        String profileSpec=pOptions.get("com.sun.tools.javac.sym.Profiles");
+        if (profileSpec == null)
+            throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Profiles=PROFILES_SPEC");
+        Profiles profiles = Profiles.read(new File(profileSpec));
 
         for (PackageSymbol psym : packages) {
             String name = psym.getQualifiedName().toString();
@@ -166,12 +176,19 @@
             tool.getTask(null, fm, null, options, null, null);
         com.sun.tools.javac.main.JavaCompiler compiler =
             com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
-        ClassReader reader = ClassReader.instance(task.getContext());
         ClassWriter writer = ClassWriter.instance(task.getContext());
         Symtab syms = Symtab.instance(task.getContext());
-        Attribute.Compound proprietary =
+        Names names = Names.instance(task.getContext());
+        Attribute.Compound proprietaryAnno =
             new Attribute.Compound(syms.proprietaryType,
                                    List.<Pair<Symbol.MethodSymbol,Attribute>>nil());
+        Attribute.Compound[] profileAnnos = new Attribute.Compound[profiles.getProfileCount() + 1];
+        Symbol.MethodSymbol profileValue = (MethodSymbol) syms.profileType.tsym.members().lookup(names.value).sym;
+        for (int i = 1; i < profileAnnos.length; i++) {
+            profileAnnos[i] = new Attribute.Compound(syms.profileType,
+                    List.<Pair<Symbol.MethodSymbol, Attribute>>of(
+                    new Pair<Symbol.MethodSymbol, Attribute>(profileValue, new Attribute.Constant(syms.intType, i))));
+        }
 
         Type.moreInfo = true;
         Types types = Types.instance(task.getContext());
@@ -208,8 +225,11 @@
             }
             ClassSymbol cs = (ClassSymbol) sym;
             if (addLegacyAnnotation) {
-                cs.annotations.prepend(List.of(proprietary));
+                cs.annotations.prepend(List.of(proprietaryAnno));
             }
+            int p = profiles.getProfile(cs.fullname.toString().replace(".", "/"));
+            if (0 < p && p < profileAnnos.length)
+                cs.annotations.prepend(List.of(profileAnnos[p]));
             writeClass(pool, cs, writer);
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/sym/Profiles.java	Mon Jan 21 01:27:42 2013 -0500
@@ -0,0 +1,298 @@
+/*
+ * Copyright (c) 2006, 2013, 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.sym;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import com.sun.tools.javac.util.Assert;
+
+/**
+ * Provide details about profile contents.
+ *
+ * <p><b>This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own
+ * risk.  This code and its internal interfaces are subject to change
+ * or deletion without notice.</b></p>
+ */
+public abstract class Profiles {
+    // for debugging
+    public static void main(String[] args) throws IOException {
+        Profiles p = Profiles.read(new File(args[0]));
+        if (args.length >= 2) {
+            Map<Integer,Set<String>> lists = new TreeMap<Integer,Set<String>>();
+            for (int i = 1; i <= 4; i++)
+                lists.put(i, new TreeSet<String>());
+
+            File rt_jar_lst = new File(args[1]);
+            for (String line: Files.readAllLines(rt_jar_lst.toPath(), Charset.defaultCharset())) {
+                if (line.endsWith(".class")) {
+                    String type = line.substring(0, line.length() - 6);
+                    int profile = p.getProfile(type);
+                    for (int i = profile; i <= 4; i++)
+                        lists.get(i).add(type);
+                }
+            }
+
+            for (int i = 1; i <= 4; i++) {
+                BufferedWriter out = new BufferedWriter(new FileWriter(i + ".txt"));
+                try {
+                    for (String type: lists.get(i)) {
+                        out.write(type);
+                        out.newLine();
+                    }
+                } finally {
+                    out.close();
+                }
+            }
+        }
+    }
+
+    public static Profiles read(File file) throws IOException {
+        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
+        try {
+            Properties p = new Properties();
+            p.load(in);
+            if (p.containsKey("java/lang/Object"))
+                return new SimpleProfiles(p);
+            else
+                return new MakefileProfiles(p);
+        } finally {
+            in.close();
+        }
+    }
+
+    public abstract int getProfileCount();
+
+    public abstract int getProfile(String typeName);
+
+    public abstract Set<String> getPackages(int profile);
+
+    private static class MakefileProfiles extends Profiles {
+        static class Package {
+            final Package parent;
+            final String name;
+
+            Map<String, Package> subpackages = new TreeMap<String, Package>();
+
+            int profile;
+            Map<String, Integer> includedTypes = new TreeMap<String,Integer>();
+            Map<String, Integer> excludedTypes = new TreeMap<String,Integer>();
+
+            Package(Package parent, String name) {
+                this.parent = parent;
+                this.name = name;
+            }
+
+            int getProfile() {
+                return (parent == null) ? profile : Math.max(parent.getProfile(), profile);
+            }
+
+            int getProfile(String simpleTypeName) {
+                Integer i;
+                if ((i = includedTypes.get(simpleTypeName)) != null)
+                    return i;
+                if ((i = includedTypes.get("*")) != null)
+                    return i;
+                if ((i = excludedTypes.get(simpleTypeName)) != null)
+                    return i + 1;
+                if ((i = excludedTypes.get("*")) != null)
+                    return i + 1;
+                return getProfile();
+            }
+
+            String getName() {
+                return (parent == null) ? name : (parent.getName() + "/" + name);
+            }
+
+            void getPackages(int profile, Set<String> results) {
+                int prf = getProfile();
+                if (prf != 0 && profile >= prf)
+                    results.add(getName());
+                for (Package pkg: subpackages.values())
+                    pkg.getPackages(profile, results);
+            }
+        }
+
+        final static Map<String, Package> packages = new TreeMap<String, Package>();
+        int maxProfile;
+
+        MakefileProfiles(Properties p) {
+            int profile = 1;
+            while (true) {
+                String inclPackages = p.getProperty("PROFILE_" + profile + "_RTJAR_INCLUDE_PACKAGES");
+                if (inclPackages == null)
+                    break;
+                for (String pkg: inclPackages.substring(1).trim().split("\\s+")) {
+                    if (pkg.endsWith("/"))
+                        pkg = pkg.substring(0, pkg.length() - 1);
+                    includePackage(profile, pkg);
+                }
+                String inclTypes =  p.getProperty("PROFILE_" + profile + "_RTJAR_INCLUDE_TYPES");
+                if (inclTypes != null) {
+                    for (String type: inclTypes.replace("$$", "$").split("\\s+")) {
+                        if (type.endsWith(".class"))
+                            includeType(profile, type.substring(0, type.length() - 6));
+                    }
+                }
+                String exclTypes =  p.getProperty("PROFILE_" + profile + "_RTJAR_EXCLUDE_TYPES");
+                if (exclTypes != null) {
+                    for (String type: exclTypes.replace("$$", "$").split("\\s+")) {
+                        if (type.endsWith(".class"))
+                            excludeType(profile, type.substring(0, type.length() - 6));
+                    }
+                }
+                maxProfile = profile;
+                profile++;
+            }
+        }
+
+        @Override
+        public int getProfileCount() {
+            return maxProfile;
+        }
+
+        @Override
+        public int getProfile(String typeName) {
+            int sep = typeName.lastIndexOf("/");
+            String packageName = typeName.substring(0, sep);
+            String simpleName = typeName.substring(sep + 1);
+
+            Package p = getPackage(packageName);
+            return p.getProfile(simpleName);
+        }
+
+        @Override
+        public Set<String> getPackages(int profile) {
+            Set<String> results = new TreeSet<String>();
+            for (Package p: packages.values())
+                p.getPackages(profile, results);
+            return results;
+        }
+
+        private void includePackage(int profile, String packageName) {
+//            System.err.println("include package " + packageName);
+            Package p = getPackage(packageName);
+            Assert.check(p.profile == 0);
+            p.profile = profile;
+        }
+
+        private void includeType(int profile, String typeName) {
+//            System.err.println("include type " + typeName);
+            int sep = typeName.lastIndexOf("/");
+            String packageName = typeName.substring(0, sep);
+            String simpleName = typeName.substring(sep + 1);
+
+            Package p = getPackage(packageName);
+            Assert.check(!p.includedTypes.containsKey(simpleName));
+            p.includedTypes.put(simpleName, profile);
+        }
+
+        private void excludeType(int profile, String typeName) {
+//            System.err.println("exclude type " + typeName);
+            int sep = typeName.lastIndexOf("/");
+            String packageName = typeName.substring(0, sep);
+            String simpleName = typeName.substring(sep + 1);
+
+            Package p = getPackage(packageName);
+            Assert.check(!p.excludedTypes.containsKey(simpleName));
+            p.excludedTypes.put(simpleName, profile);
+        }
+
+        private Package getPackage(String packageName) {
+            int sep = packageName.lastIndexOf("/");
+            Package parent;
+            Map<String, Package> parentSubpackages;
+            String simpleName;
+            if (sep == -1) {
+                parent = null;
+                parentSubpackages = packages;
+                simpleName = packageName;
+            } else {
+                parent = getPackage(packageName.substring(0, sep));
+                parentSubpackages = parent.subpackages;
+                simpleName = packageName.substring(sep + 1);
+            }
+
+            Package p = parentSubpackages.get(simpleName);
+            if (p == null) {
+                parentSubpackages.put(simpleName, p = new Package(parent, simpleName));
+            }
+            return p;
+        }
+    }
+
+    private static class SimpleProfiles extends Profiles {
+        private final Map<String, Integer> map;
+        private final int profileCount;
+
+        SimpleProfiles(Properties p) {
+            int max = 0;
+            map = new HashMap<String, Integer>();
+            for (Map.Entry<Object,Object> e: p.entrySet()) {
+                String typeName = (String) e.getKey();
+                int profile = Integer.valueOf((String) e.getValue());
+                map.put(typeName, profile);
+                max = Math.max(max, profile);
+            }
+            profileCount = max;
+        }
+
+        @Override
+        public int getProfileCount() {
+            return profileCount;
+        }
+
+        @Override
+        public int getProfile(String typeName) {
+            return map.get(typeName);
+        }
+
+        @Override
+        public Set<String> getPackages(int profile) {
+            Set<String> results = new TreeSet<String>();
+            for (Map.Entry<String,Integer> e: map.entrySet()) {
+                String tn = e.getKey();
+                int prf = e.getValue();
+                int sep = tn.lastIndexOf("/");
+                if (sep > 0 && profile >= prf)
+                    results.add(tn);
+            }
+            return results;
+        }
+    }
+}
--- a/langtools/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java	Mon Jan 21 01:27:42 2013 -0500
@@ -45,6 +45,7 @@
 import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.code.Type.CapturedType;
 import com.sun.tools.javac.file.BaseFileObject;
+import com.sun.tools.javac.jvm.Profile;
 import com.sun.tools.javac.tree.JCTree.*;
 import com.sun.tools.javac.tree.Pretty;
 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
@@ -197,6 +198,9 @@
         else if (arg instanceof JavaFileObject) {
             return ((JavaFileObject)arg).getName();
         }
+        else if (arg instanceof Profile) {
+            return ((Profile)arg).name;
+        }
         else if (arg instanceof Formattable) {
             return ((Formattable)arg).toString(l, messages);
         }
--- a/langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java	Mon Jan 21 00:45:35 2013 -0500
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java	Mon Jan 21 01:27:42 2013 -0500
@@ -24,6 +24,7 @@
  */
 package com.sun.tools.javac.util;
 
+import java.util.EnumMap;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -96,7 +97,7 @@
         this.diags = JCDiagnostic.Factory.instance(context);
         this.types = Types.instance(context);
         this.messages = JavacMessages.instance(context);
-        whereClauses = new LinkedHashMap<WhereClauseKind, Map<Type, JCDiagnostic>>();
+        whereClauses = new EnumMap<WhereClauseKind, Map<Type, JCDiagnostic>>(WhereClauseKind.class);
         configuration = new RichConfiguration(Options.instance(context), formatter);
         for (WhereClauseKind kind : WhereClauseKind.values())
             whereClauses.put(kind, new LinkedHashMap<Type, JCDiagnostic>());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/NotInProfile.java	Mon Jan 21 01:27:42 2013 -0500
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010, 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.
+ */
+
+// key: compiler.err.not.in.profile
+// options: -profile compact1
+
+class NotInProfile {
+    Class<?> c = java.awt.Frame.class;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/profiles/ProfileOptionTest.java	Mon Jan 21 01:27:42 2013 -0500
@@ -0,0 +1,268 @@
+/*
+ * Copyright (c) 2011, 2012, 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
+ * @bug 8004182
+ * @summary Add support for profiles in javac
+ */
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+
+import com.sun.source.util.JavacTask;
+import com.sun.tools.javac.api.JavacTool;
+import com.sun.tools.javac.jvm.Profile;
+import com.sun.tools.javac.jvm.Target;
+
+
+public class ProfileOptionTest {
+    public static void main(String... args) throws Exception {
+        new ProfileOptionTest().run();
+    }
+
+    private final JavaCompiler javac = JavacTool.create();
+    private final StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
+
+
+    // ---------- Test cases, invoked reflectively via run. ----------
+
+    @Test
+    void testInvalidProfile_CommandLine() throws Exception {
+        JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
+        String badName = "foo";
+        List<String> opts = Arrays.asList("-profile", badName);
+        StringWriter sw = new StringWriter();
+        try {
+            JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
+                Arrays.asList(fo));
+            throw new Exception("expected exception not thrown");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    @Test
+    void testInvalidProfile_API() throws Exception {
+        String badName = "foo";
+        String[] opts = { "-profile", badName };
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javac.Main.compile(opts, pw);
+
+        // sadly, command line errors are not (yet?) reported to
+        // the diag listener
+        String out = sw.toString();
+        if (!out.isEmpty())
+            System.err.println(out.trim());
+
+        if (!out.contains("invalid profile: " + badName)) {
+            error("expected message not found");
+        }
+    }
+
+    @Test
+    void testTargetProfileCombinations() throws Exception {
+        JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
+        for (Target t: Target.values()) {
+            switch (t) {
+                case JDK1_1: case JDK1_2: // no equivalent -source
+                case JDK1_4_1: case JDK1_4_2: case JSR14: // transitional values
+                    continue;
+            }
+
+            for (Profile p: Profile.values()) {
+                List<String> opts = new ArrayList<String>();
+                opts.addAll(Arrays.asList("-source", t.name, "-target", t.name));
+                opts.add("-Xlint:-options"); // dont warn about no -bootclasspath
+                if (p != Profile.DEFAULT)
+                    opts.addAll(Arrays.asList("-profile", p.name));
+                StringWriter sw = new StringWriter();
+                JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
+                        Arrays.asList(fo));
+                task.analyze();
+
+                // sadly, command line errors are not (yet?) reported to
+                // the diag listener
+                String out = sw.toString();
+                if (!out.isEmpty())
+                    System.err.println(out.trim());
+
+                switch (t) {
+                    case JDK1_8:
+                        if (!out.isEmpty())
+                            error("unexpected output from compiler");
+                        break;
+                    default:
+                        if (p != Profile.DEFAULT
+                                && !out.contains("profile " + p.name
+                                    + " is not valid for target release " + t.name)) {
+                            error("expected message not found");
+                        }
+                }
+            }
+        }
+    }
+
+    @Test
+    void testClassesInProfiles() throws Exception {
+        for (Profile p: Profile.values()) {
+            for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
+                for (JavaFileObject fo: e.getValue()) {
+                    DiagnosticCollector<JavaFileObject> dl =
+                            new DiagnosticCollector<JavaFileObject>();
+                    List<String> opts = (p == Profile.DEFAULT)
+                            ? Collections.<String>emptyList()
+                            : Arrays.asList("-profile", p.name);
+                    JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
+                            Arrays.asList(fo));
+                    task.analyze();
+
+                    List<String> expectDiagCodes = (p.value >= e.getKey().value)
+                            ? Collections.<String>emptyList()
+                            : Arrays.asList("compiler.err.not.in.profile");
+
+                    checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
+                }
+            }
+        }
+    }
+
+    Map<Profile, List<JavaFileObject>> testClasses =
+            new EnumMap<Profile, List<JavaFileObject>>(Profile.class);
+
+    void initTestClasses() {
+        // The following table assumes the existence of specific classes
+        // in specific profiles, as defined in the Java SE 8 spec.
+        init(Profile.COMPACT1,
+                java.lang.String.class);
+
+        init(Profile.COMPACT2,
+                javax.xml.XMLConstants.class);
+
+        init(Profile.COMPACT3,
+                javax.script.Bindings.class,
+                com.sun.security.auth.PolicyFile.class); // specifically included in 3
+
+        init(Profile.DEFAULT,
+                java.beans.BeanInfo.class,
+                javax.management.remote.rmi._RMIServer_Stub.class); // specifically excluded in 3
+    }
+
+    void init(Profile p, Class<?>... classes) {
+        List<JavaFileObject> srcs = new ArrayList<JavaFileObject>();
+        for (Class<?> c: classes) {
+            String name = "T" + c.getSimpleName();
+            String src =
+                    "class T" + name + "{" + "\n" +
+                    "    Class<?> c = " + c.getName() + ".class;\n" +
+                    "}";
+            srcs.add(new StringJavaFileObject(name + ".java", src));
+        }
+        testClasses.put(p, srcs);
+    }
+
+    void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) {
+        System.err.print(msg);
+        if (diags.isEmpty())
+            System.err.println(" OK");
+        else {
+            System.err.println();
+            System.err.println(diags);
+        }
+
+        List<String> foundDiagCodes = new ArrayList<String>();
+        for (Diagnostic<? extends JavaFileObject> d: diags)
+            foundDiagCodes.add(d.getCode());
+
+        if (!foundDiagCodes.equals(expectDiagCodes)) {
+            System.err.println("Found diag codes:    " + foundDiagCodes);
+            System.err.println("Expected diag codes: " + expectDiagCodes);
+            error("expected diagnostics not found");
+        }
+    }
+
+    /** Marker annotation for test cases. */
+    @Retention(RetentionPolicy.RUNTIME)
+    @interface Test { }
+
+    /** Run all test cases. */
+    void run() throws Exception {
+        initTestClasses();
+
+        for (Method m: getClass().getDeclaredMethods()) {
+            Annotation a = m.getAnnotation(Test.class);
+            if (a != null) {
+                System.err.println(m.getName());
+                try {
+                    m.invoke(this, new Object[] { });
+                } catch (InvocationTargetException e) {
+                    Throwable cause = e.getCause();
+                    throw (cause instanceof Exception) ? ((Exception) cause) : e;
+                }
+                System.err.println();
+            }
+        }
+
+        if (errors > 0)
+            throw new Exception(errors + " errors occurred");
+    }
+
+    void error(String msg) {
+        System.err.println("Error: " + msg);
+        errors++;
+    }
+
+    int errors;
+
+    private static class StringJavaFileObject extends SimpleJavaFileObject {
+        StringJavaFileObject(String name, String text) {
+            super(URI.create(name), JavaFileObject.Kind.SOURCE);
+            this.text = text;
+        }
+        @Override
+        public CharSequence getCharContent(boolean b) {
+            return text;
+        }
+        private String text;
+    }
+}