7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s

Wed, 14 Sep 2011 18:26:57 -0700

author
jjg
date
Wed, 14 Sep 2011 18:26:57 -0700
changeset 1092
826ae6a2f27d
parent 1091
a6e2c1840ea1
child 1093
c0835c8489b0

7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/processing/JavacFiler.java file | annotate | diff | comparison | revisions
test/tools/javac/file/T7068437.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Wed Sep 14 15:49:54 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Wed Sep 14 18:26:57 2011 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -455,9 +455,24 @@
    1.11          // TODO: Only support reading resources in selected output
    1.12          // locations?  Only allow reading of non-source, non-class
    1.13          // files from the supported input locations?
    1.14 -        FileObject fileObject = fileManager.getFileForInput(location,
    1.15 +
    1.16 +        // In the following, getFileForInput is the "obvious" method
    1.17 +        // to use, but it does not have the "obvious" semantics for
    1.18 +        // SOURCE_OUTPUT and CLASS_OUTPUT. Conversely, getFileForOutput
    1.19 +        // does not have the correct semantics for any "path" location
    1.20 +        // with more than one component. So, for now, we use a hybrid
    1.21 +        // invocation.
    1.22 +        FileObject fileObject;
    1.23 +        if (location.isOutputLocation()) {
    1.24 +            fileObject = fileManager.getFileForOutput(location,
    1.25 +                    pkg.toString(),
    1.26 +                    relativeName.toString(),
    1.27 +                    null);
    1.28 +        } else {
    1.29 +            fileObject = fileManager.getFileForInput(location,
    1.30                      pkg.toString(),
    1.31                      relativeName.toString());
    1.32 +        }
    1.33          if (fileObject == null) {
    1.34              String name = (pkg.length() == 0)
    1.35                      ? relativeName.toString() : (pkg + "/" + relativeName);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/file/T7068437.java	Wed Sep 14 18:26:57 2011 -0700
     2.3 @@ -0,0 +1,136 @@
     2.4 +/*
     2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 7068437
    2.30 + * @summary Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
    2.31 + */
    2.32 +
    2.33 +import java.io.FileNotFoundException;
    2.34 +import java.io.IOException;
    2.35 +import java.io.Writer;
    2.36 +import java.util.Arrays;
    2.37 +import java.util.Collections;
    2.38 +import java.util.Map;
    2.39 +import java.util.Set;
    2.40 +import javax.annotation.processing.AbstractProcessor;
    2.41 +import javax.annotation.processing.Filer;
    2.42 +import javax.annotation.processing.Messager;
    2.43 +import javax.annotation.processing.RoundEnvironment;
    2.44 +import javax.annotation.processing.SupportedAnnotationTypes;
    2.45 +import javax.annotation.processing.SupportedOptions;
    2.46 +import javax.annotation.processing.SupportedSourceVersion;
    2.47 +import javax.lang.model.SourceVersion;
    2.48 +import javax.lang.model.element.TypeElement;
    2.49 +import javax.tools.Diagnostic.Kind;
    2.50 +import javax.tools.JavaCompiler;
    2.51 +import javax.tools.JavaCompiler.CompilationTask;
    2.52 +import javax.tools.StandardLocation;
    2.53 +import javax.tools.ToolProvider;
    2.54 +
    2.55 +public class T7068437 {
    2.56 +    public static void main(String[] args) throws Exception {
    2.57 +        new T7068437().run();
    2.58 +    }
    2.59 +
    2.60 +    void run() throws Exception {
    2.61 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    2.62 +        System.err.println("using " + compiler.getClass()
    2.63 +                + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
    2.64 +
    2.65 +        CompilationTask task = compiler.getTask(null, null, null,
    2.66 +                Collections.singleton("-proc:only"),
    2.67 +                Collections.singleton("java.lang.Object"),
    2.68 +                null);
    2.69 +        task.setProcessors(Collections.singleton(new Proc()));
    2.70 +        check("compilation", task.call());
    2.71 +
    2.72 +        task = compiler.getTask(null, null, null,
    2.73 +                Arrays.asList("-proc:only", "-AexpectFile"),
    2.74 +                Collections.singleton("java.lang.Object"),
    2.75 +                null);
    2.76 +        task.setProcessors(Collections.singleton(new Proc()));
    2.77 +        check("compilation", task.call());
    2.78 +    }
    2.79 +
    2.80 +    void check(String msg, boolean ok) {
    2.81 +        System.err.println(msg + ": " + (ok ? "ok" : "failed"));
    2.82 +        if (!ok)
    2.83 +            throw new AssertionError(msg);
    2.84 +    }
    2.85 +
    2.86 +    @SupportedAnnotationTypes("*")
    2.87 +    @SupportedOptions("expectFile")
    2.88 +    private static class Proc extends AbstractProcessor {
    2.89 +        int count;
    2.90 +
    2.91 +        @Override
    2.92 +        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    2.93 +            if (roundEnv.processingOver() || count++ > 0) {
    2.94 +                return false;
    2.95 +            }
    2.96 +
    2.97 +            Filer filer = processingEnv.getFiler();
    2.98 +            Messager messager = processingEnv.getMessager();
    2.99 +            Map<String, String> options = processingEnv.getOptions();
   2.100 +                System.err.println(options);
   2.101 +            boolean expectFile = options.containsKey("expectFile");
   2.102 +
   2.103 +            System.err.println("running Proc: expectFile=" + expectFile);
   2.104 +
   2.105 +            boolean found;
   2.106 +            try {
   2.107 +                messager.printMessage(Kind.NOTE, "found previous content of length " +
   2.108 +                        filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length());
   2.109 +                found = true;
   2.110 +            } catch (FileNotFoundException x) {
   2.111 +                messager.printMessage(Kind.NOTE, "not previously there");
   2.112 +                found = false;
   2.113 +            } catch (IOException x) {
   2.114 +                messager.printMessage(Kind.ERROR, "while reading: " + x);
   2.115 +                found = false;
   2.116 +            }
   2.117 +
   2.118 +            if (expectFile && !found) {
   2.119 +                messager.printMessage(Kind.ERROR, "expected file but file not found");
   2.120 +            }
   2.121 +
   2.122 +            try {
   2.123 +                Writer w = filer.createSourceFile("p.C").openWriter();
   2.124 +                w.write("/* hello! */ package p; class C {}");
   2.125 +                w.close();
   2.126 +                messager.printMessage(Kind.NOTE, "wrote new content");
   2.127 +            } catch (IOException x) {
   2.128 +                messager.printMessage(Kind.ERROR, "while writing: " + x);
   2.129 +            }
   2.130 +
   2.131 +            return true;
   2.132 +        }
   2.133 +
   2.134 +        @Override
   2.135 +        public SourceVersion getSupportedSourceVersion() {
   2.136 +            return SourceVersion.latest();
   2.137 +        }
   2.138 +    }
   2.139 +}

mercurial