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

Support Refaster#anyOf() #76

Merged
merged 13 commits into from
Mar 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public class ImportDetector {
/**
Expand All @@ -38,7 +39,11 @@ public class ImportDetector {
* @return The list of imports to add.
*/
public static List<Symbol> imports(JCTree.JCMethodDecl methodDecl) {
ImportScanner importScanner = new ImportScanner();
return imports(methodDecl, t -> true);
}

public static List<Symbol> imports(JCTree.JCMethodDecl methodDecl, Predicate<JCTree> scopePredicate) {
ImportScanner importScanner = new ImportScanner(scopePredicate);
importScanner.scan(methodDecl.getBody());
importScanner.scan(methodDecl.getReturnType());
for (JCTree.JCVariableDecl param : methodDecl.getParameters()) {
Expand All @@ -57,16 +62,25 @@ public static List<Symbol> imports(JCTree.JCMethodDecl methodDecl) {
* @return The list of imports to add.
*/
public static List<Symbol> imports(JCTree tree) {
ImportScanner importScanner = new ImportScanner();
ImportScanner importScanner = new ImportScanner(t -> true);
importScanner.scan(tree);
return new ArrayList<>(importScanner.imports);
}

private static class ImportScanner extends TreeScanner {
final Set<Symbol> imports = new LinkedHashSet<>();
private final Predicate<JCTree> scopePredicate;

public ImportScanner(Predicate<JCTree> scopePredicate) {
this.scopePredicate = scopePredicate;
}

@Override
public void scan(JCTree tree) {
if (!scopePredicate.test(tree)) {
return;
}

JCTree maybeFieldAccess = tree;
if (maybeFieldAccess instanceof JCFieldAccess &&
((JCFieldAccess) maybeFieldAccess).sym instanceof Symbol.ClassSymbol &&
Expand Down Expand Up @@ -113,6 +127,11 @@ public void scan(JCTree tree) {
&& ((JCIdent) ((JCFieldAccess) tree).selected).sym instanceof Symbol.ClassSymbol
&& !(((JCIdent) ((JCFieldAccess) tree).selected).sym.type instanceof Type.ErrorType)) {
imports.add(((JCIdent) ((JCFieldAccess) tree).selected).sym);
} else if (tree instanceof JCTree.JCFieldAccess &&
((JCTree.JCFieldAccess) tree).sym instanceof Symbol.ClassSymbol) {
if (tree.toString().equals(((JCTree.JCFieldAccess) tree).sym.toString())) {
imports.add(((JCTree.JCFieldAccess) tree).sym);
}
}

super.scan(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.template.internal;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.Pretty;
Expand Down Expand Up @@ -94,12 +95,19 @@ public void visitIdent(JCIdent jcIdent) {
if (param.isPresent()) {
print("#{" + sym.name);
if (seenParameters.add(param.get())) {
String type = param.get().sym.type.toString();
if (param.get().getModifiers().getAnnotations().stream()
.anyMatch(a -> a.attribute.type.tsym.getQualifiedName().toString().equals(PRIMITIVE_ANNOTATION))) {
type = getUnboxedPrimitive(type);
Type type = param.get().sym.type;
String typeString;
if (type instanceof Type.ArrayType) {
print(":anyArray(" + ((Type.ArrayType) type).elemtype.toString() + ")");
} else {
if (param.get().getModifiers().getAnnotations().stream()
.anyMatch(a -> a.attribute.type.tsym.getQualifiedName().toString().equals(PRIMITIVE_ANNOTATION))) {
typeString = getUnboxedPrimitive(type.toString());
} else {
typeString = type.toString();
}
print(":any(" + typeString + ")");
}
print(":any(" + type + ")");
}
print("}");
} else if (sym != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public class UsedMethodDetector {

public static List<Symbol.MethodSymbol> usedMethods(JCTree input) {
return usedMethods(input, t -> true);
}

public static List<Symbol.MethodSymbol> usedMethods(JCTree input, Predicate<JCTree> scopePredicate) {
Set<Symbol.MethodSymbol> imports = new LinkedHashSet<>();

new TreeScanner() {
@Override
public void scan(JCTree tree) {
if (tree instanceof JCTree.JCAnnotation) {
if (tree instanceof JCTree.JCAnnotation || !scopePredicate.test(tree)) {
// completely skip annotations for now
return;
}
Expand Down
Loading