Skip to content

Commit

Permalink
Merge branch 'main' into java_toolchain_21
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Apr 23, 2024
2 parents 1b180b5 + 2e51b86 commit 520df59
Show file tree
Hide file tree
Showing 18 changed files with 1,076 additions and 457 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
testRuntimeOnly("com.fasterxml.jackson.core:jackson-core")
testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind")
testRuntimeOnly("org.codehaus.groovy:groovy:latest.release")
testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:1.3.5")
testRuntimeOnly(gradleApi())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import static org.openrewrite.java.tree.J.ClassDeclaration.Kind.Type.Interface;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddMissingMethodImplementation extends Recipe {

@Option(displayName = "Fully Qualified Class Name",
description = "A fully qualified class being implemented with missing method.",
example = "com.yourorg.FooBar")
Expand All @@ -49,7 +52,7 @@ public class AddMissingMethodImplementation extends Recipe {

@Override
public String getDisplayName() {
return "Adds missing method implementations.";
return "Adds missing method implementations";
}

@Override
Expand All @@ -63,6 +66,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

public class ClassImplementationVisitor extends JavaIsoVisitor<ExecutionContext> {

private final JavaTemplate methodTemplate = JavaTemplate.builder(methodTemplateString).build();
private final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, true);

Expand All @@ -71,8 +75,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cs, Execution
// need to make sure we handle sub-classes
J.ClassDeclaration classDecl = super.visitClassDeclaration(cs, ctx);

// No need to make changes to abstract classes; only change concrete classes.
if (classDecl.hasModifier(J.Modifier.Type.Abstract)) {
// No need to make changes to abstract classes or interfaces; only change concrete classes.
if (classDecl.hasModifier(J.Modifier.Type.Abstract) || classDecl.getKind() == Interface) {
return classDecl;
}
// Don't make changes to classes that don't match the fully qualified name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.java.migrate;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;

public class RemovedSecurityManagerMethods extends Recipe {
@Override
public String getDisplayName() {
return "Replace deprecated methods in`SecurityManager`";
}

@Override
public String getDescription() {
return "Replace `SecurityManager` methods `checkAwtEventQueueAccess()`, `checkSystemClipboardAccess()`," +
" `checkMemberAccess()` and `checkTopLevelWindow()` deprecated in Java SE 11 by" +
" `checkPermission(new java.security.AllPermission())`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
private final MethodMatcher METHOD_PATTERN_QUE = new MethodMatcher("java.lang.SecurityManager checkAwtEventQueueAccess()", false);
private final MethodMatcher METHOD_PATTERN_CLIP = new MethodMatcher("java.lang.SecurityManager checkSystemClipboardAccess()", false);
private final MethodMatcher METHOD_PATTERN_MEMBER = new MethodMatcher("java.lang.SecurityManager checkMemberAccess(..)", false);
private final MethodMatcher METHOD_PATTERN_WINDOW = new MethodMatcher("java.lang.SecurityManager checkTopLevelWindow(..)", false);

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (METHOD_PATTERN_QUE.matches(method) || METHOD_PATTERN_CLIP.matches(method) || METHOD_PATTERN_MEMBER.matches(method) || METHOD_PATTERN_WINDOW.matches(method)) {
return JavaTemplate.builder("checkPermission(new java.security.AllPermission())")
.imports("java.security.AllPermission")
.build().apply(updateCursor(method),
method.getCoordinates().replaceMethod());
}
return method;
}
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.java.migrate.javax;

import lombok.EqualsAndHashCode;
import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;

import java.util.Comparator;

@EqualsAndHashCode(callSuper = false)
public class AddDefaultConstructorToEntityClass extends Recipe {
@Override
public String getDisplayName() {
return "`@Entity` objects with constructors must also have a default constructor";
}

@Override
public String getDescription() {
return "When a Java Persistence API (JPA) entity class has a constructor with arguments, the class must also " +
"have a default, no-argument constructor. The OpenJPA implementation automatically generates the " +
"no-argument constructor, but the EclipseLink implementation does not.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.or(
new UsesType<>("javax.persistence.Entity", true),
new UsesType<>("javax.persistence.MappedSuperclass", true)),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// Exit if class not annotated with either @Entity or @MappedSuperclass
if (FindAnnotations.find(classDecl, "javax.persistence.Entity").isEmpty()
&& FindAnnotations.find(classDecl, "javax.persistence.MappedSuperclass").isEmpty()) {
return classDecl;
}

// Exit if class already has default no-arg constructor
if (classDecl.getBody().getStatements().stream()
.filter(statement -> statement instanceof J.MethodDeclaration)
.map(J.MethodDeclaration.class::cast)
.filter(J.MethodDeclaration::isConstructor)
.anyMatch(constructor -> constructor.getParameters().get(0) instanceof J.Empty)) {
return classDecl;
}

// Add default constructor with empty body
return classDecl.withBody(JavaTemplate.builder("public #{}(){}")
.contextSensitive()
.build()
.apply(new Cursor(getCursor(), classDecl.getBody()),
classDecl.getBody().getCoordinates().firstStatement(),
classDecl.getSimpleName()
)
);
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright 2024 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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.
#
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies
displayName: Add explicit Common Annotations dependencies
description: >
Add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater.
tags:
- javax
- java11
- jsr250
- jakarta
recipeList:
# Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space.
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: javax.annotation
oldArtifactId: javax.annotation-api
newGroupId: jakarta.annotation
newArtifactId: jakarta.annotation-api
newVersion: 1.3.x
- org.openrewrite.java.dependencies.AddDependency:
groupId: jakarta.annotation
artifactId: jakarta.annotation-api
version: 1.3.x
onlyIfUsing: javax.annotation..*
acceptTransitive: true
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.javax.AddInjectDependencies
displayName: Add explicit Inject dependencies
description: >
This recipe will add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java
version 11 or greater.
Add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater.
tags:
- javax
- java11
Expand All @@ -33,7 +32,6 @@ recipeList:
version: 1.0.3
onlyIfUsing: javax.inject.*
acceptTransitive: true

- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: jakarta.inject
artifactId: jakarta.inject-api
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/META-INF/rewrite/jakarta-ee-10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ recipeList:
- org.openrewrite.java.migrate.jakarta.BeanValidationMessages
- org.openrewrite.java.migrate.jakarta.JavaxBeansXmlToJakartaBeansXml
- org.openrewrite.java.migrate.jakarta.JavaxToJakartaCdiExtensions
- org.openrewrite.java.migrate.jakarta.UpdateJakartaPlatform10
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.ServletCookieBehaviorChangeRFC6265
Expand Down Expand Up @@ -219,4 +220,13 @@ recipeList:
- org.openrewrite.RenameFile:
fileMatcher: '**/javax.enterprise.inject.spi.Extension'
fileName: jakarta.enterprise.inject.spi.Extension

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.UpdateJakartaPlatform10
displayName: Update Jakarta EE Platform Dependencies to 10.0.0
description: Update Jakarta EE Platform Dependencies to 10.0.0
recipeList:
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: jakarta.platform
artifactId: "*"
newVersion: 10.0.0
Loading

0 comments on commit 520df59

Please sign in to comment.