Skip to content

Commit

Permalink
#111 Added Method Declaration & Implementation visitors
Browse files Browse the repository at this point in the history
Added Method Declaration & Implementation visitors
Moved classes around
Pulled test classes into src/test/java
Upgraded OpenRewrite BOM version
  • Loading branch information
jimbethancourt committed Jan 27, 2025
1 parent 39aa8f4 commit 2e460ea
Show file tree
Hide file tree
Showing 37 changed files with 532 additions and 223 deletions.
3 changes: 2 additions & 1 deletion circular-reference-detector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recipe-bom</artifactId>
<version>2.23.0</version>
<!-- <version>2.23.0</version>-->
<version>3.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.hjug.parser.visitor.JavaVariableTypeVisitor;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
Expand Down
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;
Expand Down
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);
}
}

}
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;
Expand Down
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);
}
}

}
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);
}
}
}
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.List;

Expand Down Expand Up @@ -69,15 +69,15 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
// but I'm not sure how to get a cursor
// All types, including primitives can be annotated
for (J.Annotation annotation : variableDeclarations.getAllAnnotations()) {
processAnnotation(annotation, ownerFqn);
processAnnotation(ownerFqn, annotation);
}

// skip primitive variable declarations
if (javaType instanceof JavaType.Primitive) {
return variableDeclarations;
}

processType(javaType, ownerFqn);
processType(ownerFqn, javaType);

return variableDeclarations;
}
Expand Down
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.*;

Expand Down Expand Up @@ -58,14 +58,4 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, P p) {

return compilationUnit;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) {
J.MethodDeclaration methodDeclaration = super.visitMethodDeclaration(method, p);

methodDeclaration.getLeadingAnnotations();
methodDeclaration.getReturnTypeExpression().getType();

return methodDeclaration;
}
}
Loading

0 comments on commit 2e460ea

Please sign in to comment.