Skip to content

Commit

Permalink
Better import handling
Browse files Browse the repository at this point in the history
Issue: #5
  • Loading branch information
knutwannheden committed May 12, 2023
1 parent 0efd5c2 commit c32fca9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,12 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
}

private static String lambdaCastType(Class<? extends JCTree> type, JCTree.JCMethodDecl method) {
if (type == JCTree.JCMethodInvocation.class && method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement) {
return "";
}
int paramCount = method.params.size();
boolean asFunction = !(method.restype.type instanceof Type.JCVoidType) && JCTree.JCExpression.class.isAssignableFrom(type);
StringJoiner joiner = new StringJoiner(", ", "<", ">");
StringJoiner joiner = new StringJoiner(", ", "<", ">").setEmptyValue("");
for (int i = 0; i < (asFunction ? paramCount + 1 : paramCount); i++) {
joiner.add("?");
}
Expand Down Expand Up @@ -388,6 +391,9 @@ private String toLambda(JCTree.JCMethodDecl method) {
JCTree.JCStatement statement = method.getBody().getStatements().get(0);
if (statement instanceof JCTree.JCReturn) {
builder.append(((JCTree.JCReturn) statement).getExpression().toString());
} else if (statement instanceof JCTree.JCThrow) {
String string = statement.toString();
builder.append("{ ").append(string).append(" }");
} else {
String string = statement.toString();
builder.append(string, 0, string.length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
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.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.TreeScanner;

import javax.lang.model.element.ElementKind;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ImportDetector {

Expand All @@ -34,18 +37,19 @@ public class ImportDetector {
* @return The list of imports to add.
*/
public static List<Symbol> imports(JCTree input) {
List<Symbol> imports = new ArrayList<>();
Set<Symbol> imports = new LinkedHashSet<>();

new TreeScanner() {
@Override
public void scan(JCTree tree) {
JCTree maybeFieldAccess = tree;
if (maybeFieldAccess instanceof JCFieldAccess &&
Character.isUpperCase(((JCFieldAccess) maybeFieldAccess).getIdentifier().toString().charAt(0))) {
((JCFieldAccess) maybeFieldAccess).sym instanceof Symbol.ClassSymbol &&
Character.isUpperCase(((JCFieldAccess) maybeFieldAccess).getIdentifier().toString().charAt(0))) {
while (maybeFieldAccess instanceof JCFieldAccess) {
maybeFieldAccess = ((JCFieldAccess) maybeFieldAccess).getExpression();
if (maybeFieldAccess instanceof JCIdent &&
Character.isUpperCase(((JCIdent) maybeFieldAccess).getName().toString().charAt(0))) {
Character.isUpperCase(((JCIdent) maybeFieldAccess).getName().toString().charAt(0))) {
// this might be a fully qualified type name, so we don't want to add an import for it
// and returning will skip the nested identifier which represents just the class simple name
return;
Expand All @@ -64,12 +68,21 @@ public void scan(JCTree tree) {
} else if (((JCIdent) tree).sym.getKind() == ElementKind.METHOD) {
imports.add(((JCIdent) tree).sym);
}
} else if (tree instanceof JCFieldAccess && ((JCFieldAccess) tree).sym instanceof Symbol.VarSymbol
&& ((JCFieldAccess) tree).selected instanceof JCIdent
&& ((JCIdent) ((JCFieldAccess) tree).selected).sym instanceof Symbol.ClassSymbol) {
imports.add(((JCIdent) ((JCFieldAccess) tree).selected).sym);
} else if (tree instanceof JCFieldAccess && ((JCFieldAccess) tree).sym instanceof Symbol.ClassSymbol
&& ((JCFieldAccess) tree).selected instanceof JCIdent
&& ((JCIdent) ((JCFieldAccess) tree).selected).sym instanceof Symbol.ClassSymbol
&& !(((JCIdent) ((JCFieldAccess) tree).selected).sym.type instanceof Type.ErrorType)) {
imports.add(((JCIdent) ((JCFieldAccess) tree).selected).sym);
}

super.scan(tree);
}
}.scan(input);

return imports;
return new ArrayList<>(imports);
}
}

0 comments on commit c32fca9

Please sign in to comment.