Mercurial > hg > openjdk > jdk6 > langtools
changeset 122:f8c147836597
6999891: DefaultFileManager incorrect
Reviewed-by: jjg
author | jjh |
---|---|
date | Sun, 27 Mar 2011 09:31:59 -0700 |
parents | 8eb53e0c5dc9 |
children | e4adb4f86165 |
files | src/share/classes/com/sun/tools/javac/file/JavacFileManager.java test/tools/javac/lib/JavacTestingAbstractProcessor.java test/tools/javac/processing/filer/TestValidRelativeNames.java |
diffstat | 3 files changed, 243 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java Sun Mar 27 09:31:59 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java Sun Mar 27 09:31:59 2011 -0700 @@ -865,8 +865,9 @@ return false; if (!path.equals(uri.getPath())) // implicitly checks for embedded . and .. return false; - char first = path.charAt(0); - return first != '.' && first != '/'; + if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../")) + return false; + return true; } // Convenience method
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/lib/JavacTestingAbstractProcessor.java Sun Mar 27 09:31:59 2011 -0700 @@ -0,0 +1,100 @@ +/* + * 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. + */ + +import java.util.*; +import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; +import static javax.lang.model.SourceVersion.*; +import javax.lang.model.element.*; +import javax.lang.model.util.*; + +/** + * An abstract annotation processor tailored to javac regression testing. + */ +public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { + private static final Set<String> allAnnotations; + + static { + Set<String> tmp = new HashSet<String>(); + tmp.add("*"); + allAnnotations = Collections.unmodifiableSet(tmp); + } + + protected Elements eltUtils; + protected Elements elements; + protected Types typeUtils; + protected Types types; + protected Filer filer; + protected Messager messager; + protected Map<String, String> options; + + /** + * Constructor for subclasses to call. + */ + protected JavacTestingAbstractProcessor() { + super(); + } + + /** + * Return the latest source version. Unless this method is + * overridden, an {@code IllegalStateException} will be thrown if a + * subclass has a {@code SupportedSourceVersion} annotation. + */ + @Override + public SourceVersion getSupportedSourceVersion() { + SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); + if (ssv != null) + throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); + + return SourceVersion.latest(); + } + + /** + * If the processor class is annotated with {@link + * SupportedAnnotationTypes}, return an unmodifiable set with the + * same set of strings as the annotation. If the class is not so + * annotated, a one-element set containing {@code "*"} is returned + * to indicate all annotations are processed. + * + * @return the names of the annotation types supported by this + * processor, or an empty set if none + */ + @Override + public Set<String> getSupportedAnnotationTypes() { + SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); + if (sat != null) + return super.getSupportedAnnotationTypes(); + else + return allAnnotations; + } + + @Override + public void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + elements = eltUtils = processingEnv.getElementUtils(); + types = typeUtils = processingEnv.getTypeUtils(); + filer = processingEnv.getFiler(); + messager = processingEnv.getMessager(); + options = processingEnv.getOptions(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/processing/filer/TestValidRelativeNames.java Sun Mar 27 09:31:59 2011 -0700 @@ -0,0 +1,140 @@ +/* + * 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. + */ + +/* + * @test + * @bug 6999891 + * @summary Test valid relative names for Filer.createResource and Filer.getResource + * @library ../../lib + * @build JavacTestingAbstractProcessor + * @compile TestValidRelativeNames.java + * @compile/process -processor TestValidRelativeNames -Amode=create java.lang.Object + * @compile/process -processor TestValidRelativeNames -Amode=get java.lang.Object + */ + +import java.io.*; +import java.util.*; +import javax.annotation.processing.*; +import javax.lang.model.*; +import javax.lang.model.element.*; +import javax.tools.Diagnostic; +import javax.tools.StandardLocation; + +@SupportedOptions("mode") +public class TestValidRelativeNames extends JavacTestingAbstractProcessor { + enum Kind { READER_WRITER, INPUT_OUTPUT_STREAM }; + + static final String[] validRelativeNames = { + "foo", "foo.bar", ".foo", ".foo.bar", "foodir/bar", "foodir/.bar" + }; + + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + if (roundEnv.processingOver()) { + String mode = options.get("mode"); + for (String relativeBase: validRelativeNames) { + for (Kind kind: Kind.values()) { + if (mode.equals("create")) + testCreate(relativeBase, kind); + else + testGet(relativeBase, kind); + } + } + } + + return true; + } + + void testCreate(String relativeBase, Kind kind) { + String relative = getRelative(relativeBase, kind); + System.out.println("test create relative path: " + relative + ", kind: " + kind); + try { + switch (kind) { + case READER_WRITER: + Writer writer = filer.createResource( + StandardLocation.CLASS_OUTPUT, "", relative).openWriter(); + writer.write(relative); + writer.close(); + break; + + case INPUT_OUTPUT_STREAM: + OutputStream out = filer.createResource( + StandardLocation.CLASS_OUTPUT, "", relative).openOutputStream(); + out.write(relative.getBytes()); + out.close(); + break; + } + } catch (Exception e) { + messager.printMessage(Diagnostic.Kind.ERROR, + "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); + } + } + + void testGet(String relativeBase, Kind kind) { + String relative = getRelative(relativeBase, kind); + System.out.println("test get relative path: " + relative + ", kind: " + kind); + try { + switch (kind) { + case READER_WRITER: + { + Reader reader = new BufferedReader(filer.getResource( + StandardLocation.CLASS_OUTPUT, "", relative).openReader(true)); + StringBuilder sb = new StringBuilder(); + char[] buf = new char[1024]; + int n; + while ((n = reader.read(buf, 0, buf.length)) > 0) + sb.append(new String(buf, 0, n)); + if (!sb.toString().equals(relative)) { + messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb); + } + reader.close(); + } + break; + + case INPUT_OUTPUT_STREAM: + { + InputStream in = new DataInputStream(filer.getResource( + StandardLocation.CLASS_OUTPUT, "", relative).openInputStream()); + StringBuilder sb = new StringBuilder(); + byte[] buf = new byte[1024]; + int n; + while ((n = in.read(buf, 0, buf.length)) > 0) + sb.append(new String(buf, 0, n)); + if (!sb.toString().equals(relative)) { + messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb); + } + in.close(); + } + break; + } + } catch (Exception e) { + messager.printMessage(Diagnostic.Kind.ERROR, + "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); + } + } + + String getRelative(String relativeBase, Kind kind) { + String suffix = (kind == Kind.READER_WRITER ? "RW" : "IOS"); + return relativeBase.replace("foo", "foo" + suffix); + } +} +