Java源码示例:com.sun.tools.javac.jvm.Target

示例1
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例2
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例3
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例4
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例5
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例6
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例7
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例8
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例9
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例10
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例11
public void run() throws Exception {
    try (FileWriter out = new FileWriter("C.java")) {
        out.write("class C { }");
    }

    for (Source s: Source.values()) {
        test(s, null, "source", getKind(s, Source.MIN));
    }

    for (Target t: Target.values()) {
        test(Source.values()[0], t, "target", getKind(t, Target.MIN));
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
示例12
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例13
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例14
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例15
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例16
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    reader = ClassReader.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    allowEnums = source.allowEnums();
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例17
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例18
protected Attr(Context context) {
    context.put(attrKey, this);

    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    memberEnter = MemberEnter.instance(context);
    make = TreeMaker.instance(context);
    enter = Enter.instance(context);
    infer = Infer.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    types = Types.instance(context);
    diags = JCDiagnostic.Factory.instance(context);
    annotate = Annotate.instance(context);
    deferredLintHandler = DeferredLintHandler.instance(context);

    Options options = Options.instance(context);

    Source source = Source.instance(context);
    allowGenerics = source.allowGenerics();
    allowVarargs = source.allowVarargs();
    allowEnums = source.allowEnums();
    allowBoxing = source.allowBoxing();
    allowCovariantReturns = source.allowCovariantReturns();
    allowAnonOuterThis = source.allowAnonOuterThis();
    allowStringsInSwitch = source.allowStringsInSwitch();
    sourceName = source.name;
    relax = (options.isSet("-retrofit") ||
             options.isSet("-relax"));
    findDiamonds = options.get("findDiamond") != null &&
             source.allowDiamond();
    useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
}
 
示例19
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    operators = Operators.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());
    dollarCloseResource = names.
        fromString(target.syntheticNameChar() + "closeResource");

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例20
/**
 * Returns a qualified name of the {@link TypeElement} represented by this
 * {@link ElementHandle}. When the {@link ElementHandle} doesn't represent
 * a {@link TypeElement} it throws a {@link IllegalStateException}
 * @return the qualified name
 * @throws an {@link IllegalStateException} when this {@link ElementHandle} 
 * isn't creatred for the {@link TypeElement}.
 */
public @NonNull String getQualifiedName () throws IllegalStateException {
    if ((this.kind.isClass() && !isArray(signatures[0])) ||
            this.kind.isInterface() ||
            this.kind == ElementKind.MODULE ||
            this.kind == ElementKind.OTHER) {
        return this.signatures[0].replace (Target.DEFAULT.syntheticNameChar(),'.');    //NOI18N
    }
    else {
        throw new IllegalStateException ();
    }
}
 
示例21
protected Attr(Context context) {
    context.put(attrKey, this);

    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    chk = Check.instance(context);
    memberEnter = MemberEnter.instance(context);
    make = TreeMaker.instance(context);
    enter = Enter.instance(context);
    infer = Infer.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    types = Types.instance(context);
    diags = JCDiagnostic.Factory.instance(context);
    annotate = Annotate.instance(context);
    deferredLintHandler = DeferredLintHandler.instance(context);

    Options options = Options.instance(context);

    Source source = Source.instance(context);
    allowGenerics = source.allowGenerics();
    allowVarargs = source.allowVarargs();
    allowEnums = source.allowEnums();
    allowBoxing = source.allowBoxing();
    allowCovariantReturns = source.allowCovariantReturns();
    allowAnonOuterThis = source.allowAnonOuterThis();
    allowStringsInSwitch = source.allowStringsInSwitch();
    sourceName = source.name;
    relax = (options.isSet("-retrofit") ||
             options.isSet("-relax"));
    findDiamonds = options.get("findDiamond") != null &&
             source.allowDiamond();
    useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
}
 
示例22
protected Lower(Context context) {
    context.put(lowerKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    operators = Operators.instance(context);
    chk = Check.instance(context);
    attr = Attr.instance(context);
    make = TreeMaker.instance(context);
    writer = ClassWriter.instance(context);
    cfolder = ConstFold.instance(context);
    target = Target.instance(context);
    source = Source.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    dollarAssertionsDisabled = names.
        fromString(target.syntheticNameChar() + "assertionsDisabled");
    classDollar = names.
        fromString("class" + target.syntheticNameChar());
    dollarCloseResource = names.
        fromString(target.syntheticNameChar() + "closeResource");

    types = Types.instance(context);
    Options options = Options.instance(context);
    debugLower = options.isSet("debuglower");
    pkginfoOpt = PkgInfo.get(options);
}
 
示例23
@Test
public void testUnsafe(Path base) throws IOException {
    Path src = base.resolve("src");
    tb.writeJavaFiles(src,
                      "module m { requires jdk.unsupported; }",
                      "package test; public class Test { sun.misc.Unsafe unsafe; } ");
    Path classes = base.resolve("classes");
    tb.createDirectories(classes);

    List<String> log;
    List<String> expected = Arrays.asList(
            "Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
            "1 warning"
    );

    log = new JavacTask(tb)
            .options("-XDrawDiagnostics")
            .outdir(classes)
            .files(tb.findJavaFiles(src))
            .run(Expect.SUCCESS)
            .writeAll()
            .getOutputLines(Task.OutputKind.DIRECT);

    if (!expected.equals(log)) {
        throw new AssertionError("Unexpected output: " + log);
    }

    log = new JavacTask(tb)
            .options("-XDrawDiagnostics",
                     "--release", Target.DEFAULT.multiReleaseValue())
            .outdir(classes)
            .files(tb.findJavaFiles(src))
            .run(Expect.SUCCESS)
            .writeAll()
            .getOutputLines(Task.OutputKind.DIRECT);

    if (!expected.equals(log)) {
        throw new AssertionError("Unexpected output: " + log);
    }
}
 
示例24
@Test
public void testUnsafeUnnamed(Path base) throws IOException {
    Path src = base.resolve("src");
    tb.writeJavaFiles(src,
                      "package test; public class Test { sun.misc.Unsafe unsafe; } ");
    Path classes = base.resolve("classes");
    tb.createDirectories(classes);

    List<String> log;
    List<String> expected = Arrays.asList(
            "Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
            "1 warning"
    );

    log = new JavacTask(tb)
            .options("-XDrawDiagnostics")
            .outdir(classes)
            .files(tb.findJavaFiles(src))
            .run(Expect.SUCCESS)
            .writeAll()
            .getOutputLines(Task.OutputKind.DIRECT);

    if (!expected.equals(log)) {
        throw new AssertionError("Unexpected output: " + log);
    }

    log = new JavacTask(tb)
            .options("-XDrawDiagnostics",
                     "--release", Target.DEFAULT.multiReleaseValue())
            .outdir(classes)
            .files(tb.findJavaFiles(src))
            .run(Expect.SUCCESS)
            .writeAll()
            .getOutputLines(Task.OutputKind.DIRECT);

    if (!expected.equals(log)) {
        throw new AssertionError("Unexpected output: " + log);
    }
}
 
示例25
void test(Source s, Target t, String select, Kind kind) {
    System.err.println("Test: source:" + s + ", target:" + t + " " + select + " " + kind);
    List<String> args = new ArrayList<>();
    args.add("-XDrawDiagnostics");
    args.add("-source");
    args.add(s.name);
    if (t != null) {
        args.add("-target");
        args.add(t.name);
    }
    args.add("C.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();
    String out = sw.toString();
    System.err.print(out);

    switch (kind) {
        case INVALID:
            check(out, "removed." + select, true);
            check(out, "obsolete." + select, false);
            break;

        case OBSOLETE:
            check(out, "removed." + select, false);
            check(out, "obsolete." + select, true);
            break;

        case VALID:
            check(out, "removed." + select, false);
            check(out, "obsolete." + select, false);
            break;
    }

    System.err.println();
}
 
示例26
private static void test(String classname, String expected) throws Exception {
    File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
    ClassFile cf = ClassFile.read(classfile);

    cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
             Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
            cf.this_class, cf.super_class, cf.interfaces, cf.fields,
            cf.methods, cf.attributes);

    new ClassWriter().write(cf, classfile);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);
    Symtab syms = Symtab.instance(task.getContext());

    task.ensureEntered();

    try {
        Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(syms.unnamedModule, classname);

        clazz.complete();
    } catch (BadClassFile f) {
        JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
        assertEquals(expected, embeddedDiag.getCode());
        assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
        assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
    }
}
 
示例27
@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
                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");
                    }
            }
        }
    }
}
 
示例28
@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
                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");
                    }
            }
        }
    }
}
 
示例29
public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected
    int ac = 0;
    while (ac < flags.length) {
        String flag = flags[ac];
        ac++;

        Option option = null;

        if (flag.length() > 0) {
            // quick hack to speed up file processing:
            // if the option does not begin with '-', there is no need to check
            // most of the compiler options.
            int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length - 1;
            for (int j = firstOptionToCheck; j < recognizedOptions.length; j++) {
                if (recognizedOptions[j].matches(flag)) {
                    option = recognizedOptions[j];
                    break;
                }
            }
        }

        if (option == null) {
            error("err.invalid.flag", flag);
            return null;
        }

        if (option.hasArg()) {
            if (ac == flags.length) {
                error("err.req.arg", flag);
                return null;
            }
            String operand = flags[ac];
            ac++;
            if (option.process(options, flag, operand))
                return null;
        } else {
            if (option.process(options, flag))
                return null;
        }
    }

    if (this.classnames != null && classNames != null) {
        this.classnames.addAll(Arrays.asList(classNames));
    }

    if (!checkDirectory(D))
        return null;
    if (!checkDirectory(S))
        return null;

    String sourceString = options.get(SOURCE);
    Source source = (sourceString != null)
            ? Source.lookup(sourceString)
            : Source.DEFAULT;
    String targetString = options.get(TARGET);
    Target target = (targetString != null)
            ? Target.lookup(targetString)
            : Target.DEFAULT;
    // We don't check source/target consistency for CLDC, as J2ME
    // profiles are not aligned with J2SE targets; moreover, a
    // single CLDC target may have many profiles.  In addition,
    // this is needed for the continued functioning of the JSR14
    // prototype.
    if (Character.isDigit(target.name.charAt(0))) {
        if (target.compareTo(source.requiredTarget()) < 0) {
            if (targetString != null) {
                if (sourceString == null) {
                    warning("warn.target.default.source.conflict",
                            targetString,
                            source.requiredTarget().name);
                } else {
                    warning("warn.source.target.conflict",
                            sourceString,
                            source.requiredTarget().name);
                }
                return null;
            } else {
                target = source.requiredTarget();
                options.put("-target", target.name);
            }
        } else {
            if (targetString == null && !source.allowGenerics()) {
                target = Target.JDK1_4;
                options.put("-target", target.name);
            }
        }
    }

    // handle this here so it works even if no other options given
    String showClass = options.get("showClass");
    if (showClass != null) {
        if (showClass.equals("showClass")) // no value given for option
            showClass = "com.sun.tools.javac.Main";
        showClass(showClass);
    }

    return filenames;
}
 
示例30
@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
                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");
                    }
            }
        }
    }
}