changeset 48614:91bd550551e0

8193216: Filer should warn if processors redefine symbols from the classpath or sourcepath Reviewed-by: vromero
author cushon
date Thu, 21 Dec 2017 15:58:45 -0500
parents 38493aecb3d1
children 700068e1dc12 ce63d3742f0e 2bb94a17fe8b 42c802e0b351 b74998ef9762 eedd12872b28 26bd8514b226 2458c6bb49ad 2731c0ee46a9
files src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/A.java test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/B.java test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/TestProcTypeAlreadyExistsWarning.java test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/warn.out
diffstat 5 files changed, 123 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Thu Dec 21 05:51:38 2017 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Thu Dec 21 15:58:45 2017 -0500
@@ -712,20 +712,20 @@
     }
 
     private void checkNameAndExistence(ModuleSymbol mod, String typename, boolean allowUnnamedPackageInfo) throws FilerException {
-        // TODO: Check if type already exists on source or class path?
-        // If so, use warning message key proc.type.already.exists
         checkName(typename, allowUnnamedPackageInfo);
-        ClassSymbol existing;
+        ClassSymbol existing = elementUtils.getTypeElement(typename);
         boolean alreadySeen = aggregateGeneratedSourceNames.contains(Pair.of(mod, typename)) ||
                               aggregateGeneratedClassNames.contains(Pair.of(mod, typename)) ||
                               initialClassNames.contains(typename) ||
-                              ((existing = elementUtils.getTypeElement(typename)) != null &&
-                               initialInputs.contains(existing.sourcefile));
+                              (existing != null && initialInputs.contains(existing.sourcefile));
         if (alreadySeen) {
             if (lint)
                 log.warning(Warnings.ProcTypeRecreate(typename));
             throw new FilerException("Attempt to recreate a file for type " + typename);
         }
+        if (lint && existing != null) {
+            log.warning("proc.type.already.exists", typename);
+        }
         if (!mod.isUnnamed() && !typename.contains(".")) {
             throw new FilerException("Attempt to create a type in unnamed package of a named module: " + typename);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/A.java	Thu Dec 21 15:58:45 2017 -0500
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2017, Google Inc. 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.
+ */
+
+public class A {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/B.java	Thu Dec 21 15:58:45 2017 -0500
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017, Google Inc. 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.
+ */
+
+class B {
+    A a;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/TestProcTypeAlreadyExistsWarning.java	Thu Dec 21 15:58:45 2017 -0500
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2017, Google Inc. 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 8193216
+ * @summary Filer should warn if processors redefine symbols from the classpath or sourcepath
+ * @library /tools/javac/lib
+ * @modules java.compiler
+ *          jdk.compiler
+ * @build JavacTestingAbstractProcessor TestProcTypeAlreadyExistsWarning
+ * @compile A.java
+ * @compile/ref=warn.out -XDrawDiagnostics -Xlint:processing -processor TestProcTypeAlreadyExistsWarning B.java
+ */
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.IOError;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Set;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.element.TypeElement;
+
+@SupportedAnnotationTypes("*")
+public class TestProcTypeAlreadyExistsWarning extends JavacTestingAbstractProcessor {
+
+    int round = 0;
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        round++;
+
+        if (round == 1) {
+            try (OutputStream os =
+                    processingEnv.getFiler().createSourceFile("A").openOutputStream()) {
+                os.write("class A {}".getBytes(UTF_8));
+            } catch (IOException e) {
+                throw new IOError(e);
+            }
+        }
+
+        return false;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/warn.out	Thu Dec 21 15:58:45 2017 -0500
@@ -0,0 +1,2 @@
+- compiler.warn.proc.type.already.exists: A
+1 warning