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

Add scope to injected classes #248

Merged
merged 7 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.Data;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaParser;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.tree.*;

import java.util.*;

public class AddScopeToInjectedClass extends ScanningRecipe<AddScopeToInjectedClass.Accumulator> {
private static final Collection<String> typesPromptingScopeAddition = Arrays.asList("javax.inject.Inject");
m-brophy marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String getDisplayName() {
return "Add scope annotation to injected classes";
}

@Override
public String getDescription() {
return "Finds member variables annotated with `@Inject' and applies `@Dependent` scope annotation to the variable's Type";
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

private static boolean variableTypeRequiresScope(@Nullable JavaType.Variable memberVariable) {
if (memberVariable == null) {
return false;
}

for (JavaType.FullyQualified annotation : memberVariable.getAnnotations()) {
if (typesPromptingScopeAddition.contains(annotation.getFullyQualifiedName())) {
return true;
}
}
return false;
}

@Override
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable
Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
// look for classes requiring scope definition
if (tree instanceof J.CompilationUnit) {
J.CompilationUnit cu = (J.CompilationUnit) tree;
for (J.ClassDeclaration classDeclaration : cu.getClasses()) {
JavaType.FullyQualified type = (JavaType.FullyQualified) classDeclaration.getType();
for (JavaType.Variable variable : type.getMembers())
if (variableTypeRequiresScope(variable)) {
acc.getScopeTypes().add((JavaType.FullyQualified) variable.getType());
}
}
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable
Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof J.CompilationUnit) {
J.CompilationUnit cu = (J.CompilationUnit) tree;
for (J.ClassDeclaration aClass : cu.getClasses()) {
JavaType.FullyQualified type = (JavaType.FullyQualified) aClass.getType();
if (acc.getScopeTypes().contains(type)) {
return new AddScopeAnnotationVisitor().visit(cu, acc.getScopeTypes());
}
}
}
return tree;
}
};
}

private static class AddScopeAnnotationVisitor extends JavaIsoVisitor<Set<JavaType.FullyQualified>> {
final String scopeAnnotationFqn = "javax.enterprise.context.Dependent";
final AnnotationMatcher SCOPE_ANNOTATION_MATCHER = new AnnotationMatcher("@" + scopeAnnotationFqn);
final JavaTemplate templ = JavaTemplate.builder("@Dependent")
.imports(scopeAnnotationFqn)
.javaParser(JavaParser.fromJavaVersion().dependsOn("package javax.enterprise.context; public @interface Dependent {}"))
.build();

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set<JavaType.FullyQualified> scopeTypes) {
if (!scopeTypes.contains(TypeUtils.asFullyQualified(classDecl.getType()))) {
return classDecl;
}
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, scopeTypes);
if (cd.getLeadingAnnotations().stream().noneMatch(SCOPE_ANNOTATION_MATCHER::matches)) {
cd = templ.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
maybeAddImport(scopeAnnotationFqn);
}
return cd;
}
}

@Data
static class Accumulator {
Set<JavaType.FullyQualified> scopeTypes = new HashSet<>();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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 org.openrewrite.DocumentExample;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.java.JavaParser;
import org.junit.jupiter.api.Test;

import static org.openrewrite.java.Assertions.java;

class AddScopeToInjectedClassTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {

spec.recipe(new AddScopeToInjectedClass());
spec.parser(JavaParser.fromJavaVersion().dependsOn("""
package javax.enterprise.context;
@Target({ElementType.Type})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dependent {
}
""",
"""
package javax.inject;
@Target({ElementType.Type})
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
"""));
}

@DocumentExample
@Test
void scopeRequired() {
rewriteRun(spec ->
java("""
package com.sample.service;

public class Bar {}

""",
"""
package com.sample.service;

import javax.enterprise.context.Dependent;

@Dependent
public class Bar {}

"""),

java("""
package com.sample;

import javax.inject.Inject;
import com.sample.service.Bar;

public class Foo{

@javax.inject.Inject
Bar service;
}
"""));
}


@Test
void noMemberVariableAnnotation() {
rewriteRun(spec ->
java("""
package com.sample.service;

public class Bar {}

"""),

java("""
package com.sample;

import com.sample.service.Bar;

public class Foo{

Bar service;
}
"""));
}

@Test
void nonInjectAnnotation() {
rewriteRun(spec ->
java("""
package com.sample.service;

public class Bar {}

"""),

java("""
package com.sample;

import com.sample.service.Bar;
import javax.inject.NotInject;

public class Foo{
@NotInject
Bar service;
}
"""),
java("""
package javax.inject;
@Target({ElementType.Type})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotInject {
}
"""));
}


@Test
void scopeAnnotationAlreadyExists() {
rewriteRun(spec ->
java("""
package com.sample.service;

import javax.enterprise.context.Dependent;

@Dependent
public class Bar {}

"""),

java("""
package com.sample;

import javax.inject.Inject;
import com.sample.service.Bar;

public class Foo{

@javax.inject.Inject
Bar service;
}
"""));
}

}