Implement WIldcardfinder Plugin directly into the JDK

This commit is contained in:
Andreas Stadelmeier 2024-12-06 22:42:46 +01:00
parent 8b529abb27
commit 70248c8a34
2 changed files with 88 additions and 0 deletions

View File

@ -200,6 +200,8 @@ public class BasicJavacTask extends JavacTask {
public void initPlugins(Set<List<String>> pluginOpts) {
PlatformDescription platformProvider = context.get(PlatformDescription.class);
//ANDI: Init our own plugin to count CC's
initPlugin(new WildcardFinderPlugin());
if (platformProvider != null) {
for (PluginInfo<Plugin> pluginDesc : platformProvider.getPlugins()) {
java.util.List<String> options =

View File

@ -0,0 +1,86 @@
package com.sun.tools.javac.api;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.util.*;
import java.io.IOException;
public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plugin, TaskListener {
@Override
public void started(TaskEvent e) {
TaskListener.super.started(e);
}
@Override
public void finished(TaskEvent e) {
if(e.getKind() == TaskEvent.Kind.ANALYZE) {
//System.out.println(preText + "Searching for Capture-Conversions...");
e.getCompilationUnit().accept(this, null);
}
TaskListener.super.finished(e);
}
@Override
public String getName() {
return "WildcardFinder";
}
@Override
public void init(JavacTask javacTask, String... args) {
javacTask.addTaskListener(this);
}
String currentSource = "";
String currentClassContent = "";
@Override
public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
//System.out.println(node.getSourceFile().getName());
currentSource = node.getSourceFile().getName();
try {
currentClassContent = String.valueOf(node.getSourceFile().getCharContent(true));
} catch (IOException e) {
}
return super.visitCompilationUnit(node, unused);
}
static int lineOfPosition(String s, int position)
{
int count = 1;
for (int i = 0; i <= position && i < s.length(); i++)
if (s.charAt(i) == '\n')
count++;
// Return the required count
return count;
}
public static final String preText = "[ANDI] ";
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
var args = node.getArguments();
try {
for(var arg : args){
var type_field = arg.getClass().getField("type");
var pos_field = arg.getClass().getField("pos");
type_field.setAccessible(true);
pos_field.setAccessible(true);
String typeText = type_field.get(arg).toString();
int methodPos = pos_field.getInt(node);
if(typeText.contains("capture#")){ //we found a capture conversion
//System.out.println(node.getClass().getMethod("getStartPosition").invoke(node));
System.out.println(preText + "CC: " + typeText + " in " + currentSource + " " + lineOfPosition(currentClassContent,methodPos));
}else{
System.out.println(preText + "normal Method call");
}
}
} catch (NoSuchFieldException e) {
//System.out.println("Argument has no 'type' field");
} catch (IllegalAccessException e) {
//System.out.println("Illegal access of 'type' field");
} catch (Throwable e){
//System.out.println("Error in method invocation: " + e.getMessage()+
// " in "+currentSource);
}
return super.visitMethodInvocation(node, unused);
}
}