Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds MXbean recipe #335

Merged
merged 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/main/java/org/openrewrite/java/migrate/MXBeanNonPublic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2023 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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.Modifier;
import org.openrewrite.java.tree.Space;
import org.openrewrite.marker.Markers;
import org.openrewrite.marker.SearchResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.util.Collections.emptyList;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.tree.J.ClassDeclaration.Kind.Type.Interface;
import static org.openrewrite.staticanalysis.ModifierOrder.sortModifiers;

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

@Override
public String getDisplayName() {
return "MBean and MXBean interfaces must be public";
}

@Override
public String getDescription() {
return "Sets visibility of MBean and MXBean interfaces to public.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.and(
new JavaVisitor<ExecutionContext>() {
@Override
public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
if (!classDecl.hasModifier(Modifier.Type.Public) && classDecl.getKind() == Interface) {
return SearchResult.found(classDecl, "Not yet public interface");
}
return super.visitClassDeclaration(classDecl, executionContext);
}
},
Preconditions.or(
new UsesType("javax.management.MXBean", true),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
String className = classDecl.getName().getSimpleName();
if (className.endsWith("MXBean") || className.endsWith("MBean")) {
return SearchResult.found(classDecl, "Matching class name");
}
return super.visitClassDeclaration(classDecl, executionContext);
}
})
), new ClassImplementationVisitor());
}

private static class ClassImplementationVisitor extends JavaIsoVisitor<ExecutionContext> {
private static final AnnotationMatcher MX_BEAN = new AnnotationMatcher("@javax.management.MXBean");
private static final AnnotationMatcher MX_BEAN_VALUE_TRUE = new AnnotationMatcher("@javax.management.MXBean(value=true)");

private boolean shouldUpdate(J.ClassDeclaration classDecl) {
// Annotation with no argument, or explicit true argument
List<J.Annotation> leadingAnnotations = classDecl.getLeadingAnnotations();
Optional<J.Annotation> firstAnnotation = leadingAnnotations.stream().filter(MX_BEAN::matches).findFirst();
if (firstAnnotation.isPresent()) {
List<Expression> arguments = firstAnnotation.get().getArguments();
return arguments == null || arguments.isEmpty() || MX_BEAN_VALUE_TRUE.matches(firstAnnotation.get());
}
// Suffix naming convention
String className = classDecl.getName().getSimpleName();
return className.endsWith("MXBean") || className.endsWith("MBean");
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext executionContext) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, executionContext);
if (!shouldUpdate(cd)) {
return cd;
}

List<Modifier> modifiers = new ArrayList<>(cd.getModifiers());
modifiers.removeIf(modifier -> modifier.getType() == Modifier.Type.Private
|| modifier.getType() == Modifier.Type.Protected
|| modifier.getType() == Modifier.Type.Abstract);
modifiers.add(new J.Modifier(randomId(), Space.EMPTY, Markers.EMPTY, Modifier.Type.Public, emptyList()));
return maybeAutoFormat(cd, cd.withModifiers(sortModifiers(modifiers)), executionContext);
}
}
}
26 changes: 26 additions & 0 deletions src/main/resources/META-INF/rewrite/java-version-8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright 2023 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.UpgradeToJava8
displayName: Migrate to Java 8
description: >
This recipe will apply changes commonly needed when upgrading to Java 8. This recipe will also replace deprecated API
with equivalents when there is a clear migration strategy.
tags:
- java8
recipeList:
- org.openrewrite.java.migrate.MXBeanNonPublic
Loading