Skip to content

Instantly share code, notes, and snippets.

@AdeshAtole
Created July 10, 2021 13:04
Show Gist options
  • Save AdeshAtole/0b3b728c770199d1d782a5c9df4a8e04 to your computer and use it in GitHub Desktop.
Save AdeshAtole/0b3b728c770199d1d782a5c9df4a8e04 to your computer and use it in GitHub Desktop.
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.utils.SourceRoot;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.stream.Collectors;
public class InstaScanner {
private static String HMS_SRC_RESOLVER_BASE = "/home/adesh/repos/insta-hms/instahms/src/main/java";
public static final String HMS_SRC_BASE =
"/home/adesh/repos/insta-hms/instahms/src/main/java/com/insta/hms";
public static void main(String[] args) {
try {
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(),
new JavaParserTypeSolver(HMS_SRC_RESOLVER_BASE));
ParserConfiguration configuration = new ParserConfiguration();
configuration.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8).setStoreTokens(true)
.setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
SourceRoot root = new SourceRoot(new File(HMS_SRC_BASE).toPath());
root.setParserConfiguration(configuration);
List<ParseResult<CompilationUnit>> l = root.tryToParse();
List<ParseResult<CompilationUnit>> services = l.stream().filter(parseResult -> {
String typeName = parseResult.getResult().get().getPrimaryTypeName().get();
return typeName.toLowerCase().endsWith("service");
}).collect(Collectors.toList());
File f = new File("scanner.out");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
for (ParseResult<CompilationUnit> result : services) {
CompilationUnit clazz = result.getResult().get();
// clazz.findAll(ExpressionStmt.class).forEach(statement -> System.out.println(statement.
// +"->" + statement.getMetaModel()));
// System.out.println(result.getResult().get().getPrimaryTypeName().get() );
// System.out.println(result.getResult().get().getPackageDeclaration().get().getName());
clazz.walk(MethodCallExpr.class, methodCall -> {
methodCall.getScope().ifPresent(scope -> clazz.findFirst(VariableDeclarator.class,
(memberDeclaration -> memberDeclaration.getName().getId()
.equals(scope.toString()))).ifPresent(foundDeclaration -> {
if ((foundDeclaration.getType().toString().endsWith("Service") || foundDeclaration
.getType().toString().endsWith("Repository")) && foundDeclaration
.getType() instanceof ClassOrInterfaceType) {
try {
String calledObjQualifiedName =
((ClassOrInterfaceType) foundDeclaration.getType()).resolve()
.getQualifiedName();
// System.out
// .println(clazz.getPackageDeclaration().get().getName()+","+clazz.getPrimaryTypeName().get() + ","
// + calledObjQualifiedName
// .substring(0, calledObjQualifiedName.lastIndexOf(".")) + ","
// + calledObjQualifiedName + "," + methodCall);
bw.write((clazz.getPackageDeclaration().get().getName()+","+clazz.getPrimaryTypeName().get() + ","
+ calledObjQualifiedName
.substring(0, calledObjQualifiedName.lastIndexOf(".")) + ","
+ calledObjQualifiedName + ",'" + methodCall + "'\n"));
} catch (UnsolvedSymbolException e ) {
System.err.println(e.getMessage());
}catch (IOException e){
e.printStackTrace();
}
}
}));
});
}
try {
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment