diff --git a/build.gradle.kts b/build.gradle.kts index a98f5e93..bec3b62e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -89,6 +89,11 @@ dependencies { compileOnly("org.openrewrite:rewrite-java:latest.release") implementation("org.jspecify:jspecify:latest.release") + // Needed for Error Prone annotations like @BeforeTemplate + testImplementation("com.google.errorprone:error_prone_core:latest.release") { + exclude("com.google.auto.service", "auto-service-annotations") + } + // Needed for annotation processing tests testImplementation(files(tools)) testImplementation("org.openrewrite:rewrite-java:latest.integration") diff --git a/src/main/java/org/openrewrite/java/template/internal/ClasspathJarNameDetector.java b/src/main/java/org/openrewrite/java/template/internal/ClasspathJarNameDetector.java index 22d3d84d..439879cf 100644 --- a/src/main/java/org/openrewrite/java/template/internal/ClasspathJarNameDetector.java +++ b/src/main/java/org/openrewrite/java/template/internal/ClasspathJarNameDetector.java @@ -54,7 +54,7 @@ private void addJarNameFor(Symbol owner) { if (matcher.find()) { String jarName = matcher.group(1); // Ignore when `@Matches` on arguments tries to add rewrite-templating, which is implied present - if (jarName.startsWith("rewrite-templating")) { + if (jarName.startsWith("rewrite-templating") || jarName.startsWith("error_prone_core")) { return; } jarNames.add(jarName diff --git a/src/main/java/org/openrewrite/java/template/processor/RecipeWriter.java b/src/main/java/org/openrewrite/java/template/processor/RecipeWriter.java index f52b8bcf..b0d15304 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RecipeWriter.java +++ b/src/main/java/org/openrewrite/java/template/processor/RecipeWriter.java @@ -142,7 +142,7 @@ private void collectRecipes(JCTree.JCClassDecl classDecl, RuleDescriptor descrip for (Set imports : imports.values()) { imports.removeIf(i -> { int endIndex = i.lastIndexOf('.'); - return endIndex < 0 || "java.lang".equals(i.substring(0, endIndex)) || "com.google.errorprone.refaster".equals(i.substring(0, endIndex)); + return endIndex < 0 || "java.lang".equals(i.substring(0, endIndex)) || i.startsWith("com.google.errorprone.refaster"); }); } for (Set imports : staticImports.values()) { diff --git a/src/test/java/com/google/errorprone/refaster/ImportPolicy.java b/src/test/java/com/google/errorprone/refaster/ImportPolicy.java deleted file mode 100644 index 17b0ea05..00000000 --- a/src/test/java/com/google/errorprone/refaster/ImportPolicy.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.errorprone.refaster; - -public enum ImportPolicy { - IMPORT_TOP_LEVEL, - IMPORT_CLASS_DIRECTLY, - STATIC_IMPORT_ALWAYS -} diff --git a/src/test/java/com/google/errorprone/refaster/Refaster.java b/src/test/java/com/google/errorprone/refaster/Refaster.java deleted file mode 100644 index 68bd0ba2..00000000 --- a/src/test/java/com/google/errorprone/refaster/Refaster.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.errorprone.refaster; - -public class Refaster { - @SafeVarargs - public static T anyOf(T... expressions) { - throw new UnsupportedOperationException(); - } -} diff --git a/src/test/java/com/google/errorprone/refaster/annotation/AfterTemplate.java b/src/test/java/com/google/errorprone/refaster/annotation/AfterTemplate.java deleted file mode 100644 index 8dc17a8e..00000000 --- a/src/test/java/com/google/errorprone/refaster/annotation/AfterTemplate.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.errorprone.refaster.annotation; - -public @interface AfterTemplate { -} diff --git a/src/test/java/com/google/errorprone/refaster/annotation/BeforeTemplate.java b/src/test/java/com/google/errorprone/refaster/annotation/BeforeTemplate.java deleted file mode 100644 index cce65249..00000000 --- a/src/test/java/com/google/errorprone/refaster/annotation/BeforeTemplate.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.errorprone.refaster.annotation; - -public @interface BeforeTemplate { -} diff --git a/src/test/java/com/google/errorprone/refaster/annotation/UseImportPolicy.java b/src/test/java/com/google/errorprone/refaster/annotation/UseImportPolicy.java deleted file mode 100644 index fd2fed71..00000000 --- a/src/test/java/com/google/errorprone/refaster/annotation/UseImportPolicy.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.errorprone.refaster.annotation; - -import com.google.errorprone.refaster.ImportPolicy; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.SOURCE) -public @interface UseImportPolicy { - ImportPolicy value(); -} diff --git a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java index 99268e73..30e48e43 100644 --- a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java +++ b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java @@ -173,8 +173,8 @@ static Compilation compile(JavaFileObject javaFileObject, TypeAwareProcessor pro return javac() .withProcessors(processor) .withClasspath(Arrays.asList( - fileForClass(BeforeTemplate.class), - fileForClass(AfterTemplate.class), + fileForClass(com.google.errorprone.refaster.annotation.AfterTemplate.class), + fileForClass(org.openrewrite.java.template.MethodInvocationMatcher.class), fileForClass(com.google.common.collect.ImmutableMap.class), fileForClass(org.assertj.core.api.Assertions.class), fileForClass(org.junit.jupiter.api.Assertions.class),