# HG changeset patch # User cushon # Date 1513296876 28800 # Thu Dec 14 16:14:36 2017 -0800 # Node ID 42e975bad742c74074d21bea60e25340cc109752 # Parent 0ee20aad71c4f33c426372b4c8bcc1235ce2ec08 8193216: Filer should warn if processors redefine symbols from the classpath or sourcepath Reviewed-by: vromero diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java @@ -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); } diff --git a/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/A.java b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/A.java new file mode 100644 --- /dev/null +++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/A.java @@ -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 {} diff --git a/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/B.java b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/B.java new file mode 100644 --- /dev/null +++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/B.java @@ -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; +} diff --git a/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/TestProcTypeAlreadyExistsWarning.java b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/TestProcTypeAlreadyExistsWarning.java new file mode 100644 --- /dev/null +++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/TestProcTypeAlreadyExistsWarning.java @@ -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 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; + } +} diff --git a/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/warn.out b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/warn.out new file mode 100644 --- /dev/null +++ b/test/langtools/tools/javac/processing/warnings/TypeAlreadyExists/warn.out @@ -0,0 +1,2 @@ +- compiler.warn.proc.type.already.exists: A +1 warning