-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#111 Added Method Declaration & Implementation visitors
Added Method Declaration & Implementation visitors Moved classes around Pulled test classes into src/test/java Upgraded OpenRewrite BOM version
- Loading branch information
1 parent
39aa8f4
commit 2e460ea
Showing
37 changed files
with
532 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
circular-reference-detector/src/main/java/org/hjug/parser/JavaClassDeclarationVisitor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rg/hjug/parser/FqnCapturingProcessor.java → ...parser/visitor/FqnCapturingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.hjug.parser; | ||
package org.hjug.parser.visitor; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
|
101 changes: 101 additions & 0 deletions
101
...reference-detector/src/main/java/org/hjug/parser/visitor/JavaClassDeclarationVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.hjug.parser.visitor; | ||
|
||
import lombok.Getter; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.graph.DefaultDirectedWeightedGraph; | ||
import org.jgrapht.graph.DefaultWeightedEdge; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Statement; | ||
import org.openrewrite.java.tree.TypeTree; | ||
|
||
import java.util.List; | ||
|
||
public class JavaClassDeclarationVisitor<P> extends JavaIsoVisitor<P> implements TypeProcessor { | ||
|
||
|
||
private final JavaMethodInvocationVisitor methodInvocationVisitor; | ||
|
||
@Getter | ||
private Graph<String, DefaultWeightedEdge> classReferencesGraph = | ||
new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); | ||
|
||
public JavaClassDeclarationVisitor() { | ||
methodInvocationVisitor = new JavaMethodInvocationVisitor(classReferencesGraph); | ||
} | ||
|
||
public JavaClassDeclarationVisitor(Graph<String, DefaultWeightedEdge> classReferencesGraph) { | ||
this.classReferencesGraph = classReferencesGraph; | ||
methodInvocationVisitor = new JavaMethodInvocationVisitor(classReferencesGraph); | ||
} | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P p) { | ||
J.ClassDeclaration classDeclaration = super.visitClassDeclaration(classDecl, p); | ||
|
||
JavaType.FullyQualified type = classDeclaration.getType(); | ||
String owningFqn = type.getFullyQualifiedName(); | ||
|
||
processType(owningFqn, type); | ||
|
||
TypeTree extendsTypeTree = classDeclaration.getExtends(); | ||
if (null != extendsTypeTree) { | ||
processType(owningFqn, extendsTypeTree.getType()); | ||
} | ||
|
||
List<TypeTree> implementsTypeTree = classDeclaration.getImplements(); | ||
if (null != implementsTypeTree) { | ||
for (TypeTree typeTree : implementsTypeTree) { | ||
processType(owningFqn, typeTree.getType()); | ||
} | ||
} | ||
|
||
for (J.Annotation leadingAnnotation : classDeclaration.getLeadingAnnotations()) { | ||
processAnnotation(owningFqn, leadingAnnotation); | ||
} | ||
|
||
if (null != classDeclaration.getTypeParameters()) { | ||
for (J.TypeParameter typeParameter : classDeclaration.getTypeParameters()) { | ||
processTypeParameter(owningFqn, typeParameter); | ||
} | ||
} | ||
|
||
// process method invocations and lambda invocations | ||
for (Statement statement : classDeclaration.getBody().getStatements()) { | ||
if (statement instanceof J.MethodDeclaration) { | ||
J.MethodDeclaration methodDeclaration = (J.MethodDeclaration) statement; | ||
J.Block block = methodDeclaration.getBody(); | ||
if (null != block && null != block.getStatements()) { | ||
for (Statement statementInBlock : block.getStatements()) { | ||
if (statementInBlock instanceof J.MethodInvocation) { | ||
J.MethodInvocation methodInvocation = (J.MethodInvocation) statementInBlock; | ||
methodInvocationVisitor.visitMethodInvocation(owningFqn, methodInvocation); | ||
} | ||
if (statementInBlock instanceof J.Lambda) { | ||
J.Lambda lambda = (J.Lambda) statement; | ||
lambda.getParameters(); | ||
lambda.getType(); | ||
lambda.getBody(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return classDeclaration; | ||
} | ||
|
||
public void addType(String ownerFqn, String typeFqn) { | ||
classReferencesGraph.addVertex(ownerFqn); | ||
classReferencesGraph.addVertex(typeFqn); | ||
|
||
if (!classReferencesGraph.containsEdge(ownerFqn, typeFqn)) { | ||
classReferencesGraph.addEdge(ownerFqn, typeFqn); | ||
} else { | ||
DefaultWeightedEdge edge = classReferencesGraph.getEdge(ownerFqn, typeFqn); | ||
classReferencesGraph.setEdgeWeight(edge, classReferencesGraph.getEdgeWeight(edge) + 1); | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
.../hjug/parser/JavaFqnCapturingVisitor.java → ...rser/visitor/JavaFqnCapturingVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.hjug.parser; | ||
package org.hjug.parser.visitor; | ||
|
||
import java.util.*; | ||
import lombok.Getter; | ||
|
78 changes: 78 additions & 0 deletions
78
...eference-detector/src/main/java/org/hjug/parser/visitor/JavaMethodDeclarationVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.hjug.parser.visitor; | ||
|
||
import lombok.Getter; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.graph.DefaultDirectedWeightedGraph; | ||
import org.jgrapht.graph.DefaultWeightedEdge; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.NameTree; | ||
|
||
import java.util.List; | ||
|
||
public class JavaMethodDeclarationVisitor<P> extends JavaIsoVisitor<P> implements TypeProcessor { | ||
|
||
@Getter | ||
private Graph<String, DefaultWeightedEdge> classReferencesGraph = | ||
new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); | ||
|
||
public JavaMethodDeclarationVisitor() { | ||
} | ||
|
||
public JavaMethodDeclarationVisitor(Graph<String, DefaultWeightedEdge> classReferencesGraph) { | ||
this.classReferencesGraph = classReferencesGraph; | ||
} | ||
|
||
|
||
@Override | ||
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) { | ||
J.MethodDeclaration methodDeclaration = super.visitMethodDeclaration(method, p); | ||
|
||
String owner = methodDeclaration.getMethodType().getDeclaringType().getFullyQualifiedName(); | ||
JavaType returnType = methodDeclaration.getReturnTypeExpression().getType(); | ||
|
||
// skip primitive variable declarations | ||
if (!(returnType instanceof JavaType.Primitive)) { | ||
processType(owner, returnType); | ||
} | ||
|
||
for (J.Annotation leadingAnnotation : methodDeclaration.getLeadingAnnotations()) { | ||
processType(owner, leadingAnnotation.getType()); | ||
} | ||
|
||
if (null != methodDeclaration.getTypeParameters()) { | ||
for (J.TypeParameter typeParameter : methodDeclaration.getTypeParameters()) { | ||
processTypeParameter(owner, typeParameter); | ||
} | ||
} | ||
|
||
// don't need to capture parameter declarations | ||
// they are captured in JavaVariableTypeVisitor | ||
|
||
|
||
List<NameTree> throwz = methodDeclaration.getThrows(); | ||
if (null != throwz && !throwz.isEmpty()) { | ||
for (NameTree thrown : throwz) { | ||
processType(owner, thrown.getType()); | ||
} | ||
} | ||
|
||
return methodDeclaration; | ||
} | ||
|
||
public void addType(String ownerFqn, String typeFqn) { | ||
if (ownerFqn.equals(typeFqn)) return; | ||
|
||
classReferencesGraph.addVertex(ownerFqn); | ||
classReferencesGraph.addVertex(typeFqn); | ||
|
||
if (!classReferencesGraph.containsEdge(ownerFqn, typeFqn)) { | ||
classReferencesGraph.addEdge(ownerFqn, typeFqn); | ||
} else { | ||
DefaultWeightedEdge edge = classReferencesGraph.getEdge(ownerFqn, typeFqn); | ||
classReferencesGraph.setEdgeWeight(edge, classReferencesGraph.getEdgeWeight(edge) + 1); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...reference-detector/src/main/java/org/hjug/parser/visitor/JavaMethodInvocationVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.hjug.parser.visitor; | ||
|
||
import lombok.Getter; | ||
import org.jgrapht.Graph; | ||
import org.jgrapht.graph.DefaultWeightedEdge; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
|
||
// TODO: See RemoveMethodInvocationsVisitor for other visitor methods to override | ||
// Custom visitor - not extending IsoVisitor on purpose since it does not provide caller information | ||
public class JavaMethodInvocationVisitor implements TypeProcessor { | ||
|
||
@Getter | ||
private Graph<String, DefaultWeightedEdge> classReferencesGraph; | ||
|
||
public JavaMethodInvocationVisitor(Graph<String, DefaultWeightedEdge> classReferencesGraph) { | ||
this.classReferencesGraph = classReferencesGraph; | ||
} | ||
|
||
public J.MethodInvocation visitMethodInvocation(String invokingFqn, J.MethodInvocation methodInvocation) { | ||
processType(invokingFqn, methodInvocation.getMethodType().getDeclaringType()); | ||
|
||
if(null != methodInvocation.getTypeParameters() && !methodInvocation.getTypeParameters().isEmpty()) { | ||
for (Expression typeParameter : methodInvocation.getTypeParameters()) { | ||
processType(invokingFqn, typeParameter.getType()); | ||
} | ||
} | ||
|
||
for (Expression argument : methodInvocation.getArguments()) { | ||
processType(invokingFqn, argument.getType()); | ||
} | ||
|
||
return methodInvocation; | ||
} | ||
|
||
public void addType(String invokingFqn, String typeFqn) { | ||
if (invokingFqn.equals(typeFqn)) return; | ||
|
||
classReferencesGraph.addVertex(invokingFqn); | ||
classReferencesGraph.addVertex(typeFqn); | ||
|
||
if (!classReferencesGraph.containsEdge(invokingFqn, typeFqn)) { | ||
classReferencesGraph.addEdge(invokingFqn, typeFqn); | ||
} else { | ||
DefaultWeightedEdge edge = classReferencesGraph.getEdge(invokingFqn, typeFqn); | ||
classReferencesGraph.setEdgeWeight(edge, classReferencesGraph.getEdgeWeight(edge) + 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.