diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/BasicJavacTask.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/BasicJavacTask.java index 8bad3f64c38..8e1be65f086 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/BasicJavacTask.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/BasicJavacTask.java @@ -200,6 +200,8 @@ public class BasicJavacTask extends JavacTask { public void initPlugins(Set> pluginOpts) { PlatformDescription platformProvider = context.get(PlatformDescription.class); + //ANDI: Init our own plugin to count CC's + initPlugin(new WildcardFinderPlugin()); if (platformProvider != null) { for (PluginInfo pluginDesc : platformProvider.getPlugins()) { java.util.List options = diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/WildcardFinderPlugin.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/WildcardFinderPlugin.java new file mode 100644 index 00000000000..e20be8e59b4 --- /dev/null +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/WildcardFinderPlugin.java @@ -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 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); + } +} \ No newline at end of file