6999891: DefaultFileManager incorrect

Tue, 14 Dec 2010 14:17:20 -0800

author
jjg
date
Tue, 14 Dec 2010 14:17:20 -0800
changeset 802
0141f508b98d
parent 801
a8d3eed8e247
child 803
cff0b8694633

6999891: DefaultFileManager incorrect
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/file/JavacFileManager.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/filer/TestValidRelativeNames.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Mon Dec 13 17:35:57 2010 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Dec 14 14:17:20 2010 -0800
     1.3 @@ -869,8 +869,9 @@
     1.4              return false;
     1.5          if (!path.equals(uri.getPath())) // implicitly checks for embedded . and ..
     1.6              return false;
     1.7 -        char first = path.charAt(0);
     1.8 -        return first != '.' && first != '/';
     1.9 +        if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../"))
    1.10 +            return false;
    1.11 +        return true;
    1.12      }
    1.13  
    1.14      // Convenience method
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/processing/filer/TestValidRelativeNames.java	Tue Dec 14 14:17:20 2010 -0800
     2.3 @@ -0,0 +1,136 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, 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 6999891
    2.30 + * @summary Test valid relative names for Filer.createResource and Filer.getResource
    2.31 + * @library ../../lib
    2.32 + * @build   JavacTestingAbstractProcessor
    2.33 + * @compile TestValidRelativeNames.java
    2.34 + * @compile/process -processor TestValidRelativeNames -Amode=create java.lang.Object
    2.35 + * @compile/process -processor TestValidRelativeNames -Amode=get    java.lang.Object
    2.36 + */
    2.37 +
    2.38 +import java.io.*;
    2.39 +import java.util.*;
    2.40 +import javax.annotation.processing.*;
    2.41 +import javax.lang.model.*;
    2.42 +import javax.lang.model.element.*;
    2.43 +import javax.tools.Diagnostic;
    2.44 +import javax.tools.StandardLocation;
    2.45 +
    2.46 +@SupportedOptions("mode")
    2.47 +public class TestValidRelativeNames extends JavacTestingAbstractProcessor {
    2.48 +    enum Kind { READER_WRITER, INPUT_OUTPUT_STREAM };
    2.49 +
    2.50 +    static final String[] validRelativeNames = {
    2.51 +            "foo", "foo.bar", ".foo", ".foo.bar", "foodir/bar", "foodir/.bar"
    2.52 +    };
    2.53 +
    2.54 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    2.55 +        if (roundEnv.processingOver()) {
    2.56 +            String mode = options.get("mode");
    2.57 +            for (String relativeBase: validRelativeNames) {
    2.58 +                for (Kind kind: Kind.values()) {
    2.59 +                    if (mode.equals("create"))
    2.60 +                        testCreate(relativeBase, kind);
    2.61 +                    else
    2.62 +                        testGet(relativeBase, kind);
    2.63 +                }
    2.64 +            }
    2.65 +        }
    2.66 +
    2.67 +        return true;
    2.68 +    }
    2.69 +
    2.70 +    void testCreate(String relativeBase, Kind kind) {
    2.71 +        String relative = getRelative(relativeBase, kind);
    2.72 +        System.out.println("test create relative path: " + relative + ", kind: " + kind);
    2.73 +        try {
    2.74 +            switch (kind) {
    2.75 +                case READER_WRITER:
    2.76 +                    try (Writer writer = filer.createResource(
    2.77 +                            StandardLocation.CLASS_OUTPUT, "", relative).openWriter()) {
    2.78 +                        writer.write(relative);
    2.79 +                    }
    2.80 +                    break;
    2.81 +
    2.82 +                case INPUT_OUTPUT_STREAM:
    2.83 +                    try (OutputStream out = filer.createResource(
    2.84 +                            StandardLocation.CLASS_OUTPUT, "", relative).openOutputStream()) {
    2.85 +                        out.write(relative.getBytes());
    2.86 +                    }
    2.87 +                    break;
    2.88 +            }
    2.89 +        } catch (Exception e) {
    2.90 +            messager.printMessage(Diagnostic.Kind.ERROR,
    2.91 +                    "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
    2.92 +        }
    2.93 +    }
    2.94 +
    2.95 +    void testGet(String relativeBase, Kind kind) {
    2.96 +        String relative = getRelative(relativeBase, kind);
    2.97 +        System.out.println("test get relative path: " + relative + ", kind: " + kind);
    2.98 +        try {
    2.99 +            switch (kind) {
   2.100 +                case READER_WRITER:
   2.101 +                    try (Reader reader = new BufferedReader(filer.getResource(
   2.102 +                            StandardLocation.CLASS_OUTPUT, "", relative).openReader(true))) {
   2.103 +                        StringBuilder sb = new StringBuilder();
   2.104 +                        char[] buf = new char[1024];
   2.105 +                        int n;
   2.106 +                        while ((n = reader.read(buf, 0, buf.length)) > 0)
   2.107 +                            sb.append(new String(buf, 0, n));
   2.108 +                        if (!sb.toString().equals(relative)) {
   2.109 +                            messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
   2.110 +                        }
   2.111 +                    }
   2.112 +                    break;
   2.113 +
   2.114 +                case INPUT_OUTPUT_STREAM:
   2.115 +                    try (InputStream in = new DataInputStream(filer.getResource(
   2.116 +                            StandardLocation.CLASS_OUTPUT, "", relative).openInputStream())) {
   2.117 +                        StringBuilder sb = new StringBuilder();
   2.118 +                        byte[] buf = new byte[1024];
   2.119 +                        int n;
   2.120 +                        while ((n = in.read(buf, 0, buf.length)) > 0)
   2.121 +                            sb.append(new String(buf, 0, n));
   2.122 +                        if (!sb.toString().equals(relative)) {
   2.123 +                            messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
   2.124 +                        }
   2.125 +                    }
   2.126 +                    break;
   2.127 +            }
   2.128 +        } catch (Exception e) {
   2.129 +            messager.printMessage(Diagnostic.Kind.ERROR,
   2.130 +                    "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
   2.131 +        }
   2.132 +    }
   2.133 +
   2.134 +    String getRelative(String relativeBase, Kind kind) {
   2.135 +        String suffix = (kind == Kind.READER_WRITER ? "RW" : "IOS");
   2.136 +        return relativeBase.replace("foo", "foo" + suffix);
   2.137 +    }
   2.138 +}
   2.139 +

mercurial