6972556: warning for using a file name instead of a binary name for Filer.createSourceFile

Thu, 29 Jul 2010 18:06:34 -0700

author
jjg
date
Thu, 29 Jul 2010 18:06:34 -0700
changeset 618
4a7979c3ce15
parent 617
62f3f07002ea
child 619
8a5c98a695ae

6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/processing/JavacFiler.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples/ProcSuspiciousClassName/ProcSuspiciousClassName.java file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples/ProcSuspiciousClassName/processors/AnnoProc.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Thu Jul 29 15:57:43 2010 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Thu Jul 29 18:06:34 2010 -0700
     1.3 @@ -379,6 +379,15 @@
     1.4      }
     1.5  
     1.6      private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException {
     1.7 +        if (lint) {
     1.8 +            int periodIndex = name.lastIndexOf(".");
     1.9 +            if (periodIndex != -1) {
    1.10 +                String base = name.substring(periodIndex);
    1.11 +                String extn = (isSourceFile ? ".java" : ".class");
    1.12 +                if (base.equals(extn))
    1.13 +                    log.warning("proc.suspicious.class.name", name, extn);
    1.14 +            }
    1.15 +        }
    1.16          checkNameAndExistence(name, isSourceFile);
    1.17          Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
    1.18          JavaFileObject.Kind kind = (isSourceFile ?
     2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jul 29 15:57:43 2010 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jul 29 18:06:34 2010 -0700
     2.3 @@ -778,6 +778,9 @@
     2.4  compiler.warn.proc.illegal.file.name=\
     2.5      Cannot create file for illegal name ''{0}''.
     2.6  
     2.7 +compiler.warn.proc.suspicious.class.name=\
     2.8 +    Creating file for a type whose name ends in {1}: ''{0}''
     2.9 +
    2.10  compiler.warn.proc.file.create.last.round=\
    2.11      File for type ''{0}'' created in the last round will not be subject to annotation processing.
    2.12  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/diags/examples/ProcSuspiciousClassName/ProcSuspiciousClassName.java	Thu Jul 29 18:06:34 2010 -0700
     3.3 @@ -0,0 +1,27 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +// key: compiler.warn.proc.suspicious.class.name
    3.28 +// options: -Xlint:processing -processor AnnoProc
    3.29 +
    3.30 +class ProcSuspiciousClassName { }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/diags/examples/ProcSuspiciousClassName/processors/AnnoProc.java	Thu Jul 29 18:06:34 2010 -0700
     4.3 @@ -0,0 +1,58 @@
     4.4 +/*
     4.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +import java.io.*;
    4.28 +import java.util.*;
    4.29 +import javax.annotation.processing.*;
    4.30 +import javax.lang.model.*;
    4.31 +import javax.lang.model.element.*;
    4.32 +import javax.tools.*;
    4.33 +
    4.34 +@SupportedAnnotationTypes("*")
    4.35 +public class AnnoProc extends AbstractProcessor {
    4.36 +    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
    4.37 +        if (++round == 1) {
    4.38 +            Filer filer = processingEnv.getFiler();
    4.39 +            Messager messager = processingEnv.getMessager();
    4.40 +            try {
    4.41 +                FileObject fo = filer.createSourceFile("HelloWorld.java");
    4.42 +                try (Writer out = fo.openWriter()) {
    4.43 +                    out.write("class HelloWorld {\n");
    4.44 +                    out.write("  public static void main(String[] args) {\n");
    4.45 +                    out.write("    System.out.println(\"Hello World!\");\n");
    4.46 +                    out.write("  }\n");
    4.47 +                    out.write("}\n");
    4.48 +                }
    4.49 +            } catch (IOException e) {
    4.50 +                messager.printMessage(Diagnostic.Kind.ERROR, e.toString());
    4.51 +            }
    4.52 +        }
    4.53 +        return false;
    4.54 +    }
    4.55 +
    4.56 +    public SourceVersion getSupportedSourceVersion() {
    4.57 +        return SourceVersion.latest();
    4.58 +    }
    4.59 +
    4.60 +    int round = 0;
    4.61 +}

mercurial