Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7159016

Static import of member in processor-generated class fails in JDK 7

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 8
    • 7, 7u1
    • tools
    • b43
    • generic, x86
    • generic, linux
    • Verified

    Backports

      Description

        Compile and run the following test case:

        ---%<---
        import java.io.File;
        import java.io.FileWriter;
        import java.io.IOException;
        import java.io.Writer;
        import java.util.Collections;
        import java.util.Set;
        import javax.annotation.processing.AbstractProcessor;
        import javax.annotation.processing.RoundEnvironment;
        import javax.annotation.processing.SupportedAnnotationTypes;
        import javax.annotation.processing.SupportedSourceVersion;
        import javax.lang.model.SourceVersion;
        import javax.lang.model.element.TypeElement;
        import javax.tools.Diagnostic;
        import javax.tools.JavaCompiler;
        import javax.tools.ToolProvider;
        public class Demo {
            public static void main(String[] args) throws Exception {
                File src = new File(System.getProperty("java.io.tmpdir"), "C.java");
                Writer w = new FileWriter(src);
                try {
                    w.write("import static p.Generated.m;\nclass C {{m();}}\n");
                    w.flush();
                } finally {
                    w.close();
                }
                JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
                JavaCompiler.CompilationTask task = jc.getTask(null, null, null, null, null, jc.getStandardFileManager(null, null, null).getJavaFileObjects(src));
                task.setProcessors(Collections.singleton(new Proc()));
                System.out.println("success? " + task.call());
            }
            @SupportedAnnotationTypes("*")
            @SupportedSourceVersion(SourceVersion.RELEASE_6)
            private static class Proc extends AbstractProcessor {
                int written;
                @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
                    if (roundEnv.processingOver() || written++ > 0) {
                        return false;
                    }
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "writing Generated.java");
                    try {
                        Writer w = processingEnv.getFiler().createSourceFile("p.Generated").openWriter();
                        try {
                            w.write("package p; public class Generated {public static void m() {}}");
                        } finally {
                            w.close();
                        }
                    } catch (IOException x) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, x.toString());
                    }
                    return true;
                }
            }
        }
        ---%<---

        Output under JDK 6 is noisy but compilation succeeds:

        ---%<---
        java version "1.6.0_31"
        Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
        Java HotSpot(TM) Server VM (build 20.6-b01, mixed mode)

        /tmp/C.java:1: package p does not exist
        import static p.Generated.m;
                       ^
        /tmp/C.java:1: static import only from classes and interfaces
        import static p.Generated.m;
        ^
        Note: writing Generated.java
        success? true
        ---%<---

        But using JDK 7 it fails:

        ---%<---
        java version "1.7.0_03"
        Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
        Java HotSpot(TM) Server VM (build 22.1-b02, mixed mode)

        /tmp/C.java:1: error: package p does not exist
        import static p.Generated.m;
                       ^
        /tmp/C.java:1: error: static import only from classes and interfaces
        import static p.Generated.m;
        ^
        /tmp/C.java:2: error: cannot find symbol
        class C {{m();}}
                  ^
          symbol: method m()
          location: class C
        3 errors
        success? false
        ---%<---

        (Confirmed bug also in 7u1, 7u2, and 7u4b18.)

        Attachments

          Issue Links

            Activity

              People

                jjh James Holmlund (Inactive)
                jglick Jesse Glick (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: