From 09ab9a7e02b07ec7edb81ff84440b88c0c1a0154 Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Sun, 22 Dec 2024 02:21:43 +0100 Subject: [PATCH] getOwner MEthod added and check if classnames are the same when calling polymorphic methods with captured wildcards --- .../sun/source/tree/MethodInvocationTree.java | 1 + .../tools/javac/api/WildcardFinderPlugin.java | 39 ++++++++++++++----- .../com/sun/tools/javac/tree/JCTree.java | 18 ++++++++- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/source/tree/MethodInvocationTree.java b/src/jdk.compiler/share/classes/com/sun/source/tree/MethodInvocationTree.java index 864a85837db..03752787d84 100644 --- a/src/jdk.compiler/share/classes/com/sun/source/tree/MethodInvocationTree.java +++ b/src/jdk.compiler/share/classes/com/sun/source/tree/MethodInvocationTree.java @@ -61,4 +61,5 @@ public interface MethodInvocationTree extends ExpressionTree { * @return the arguments */ List getArguments(); + String getOwner(); } 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 index ce864e03da9..b75067ca6eb 100644 --- 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 @@ -39,18 +39,30 @@ public class WildcardFinderPlugin extends TreeScanner implements Plu javacTask.addTaskListener(this); } + String currentClassName = ""; + Boolean noIncompatibleFields = true; @Override public Void visitClass(ClassTree node, Void unused) { - node.getMembers().forEach(typeDecl -> { - if (typeDecl instanceof VariableTree) { - logger.accept(preText + "Field Type: " + ((VariableTree) typeDecl).getType().toString()); - logger.accept(preText + "deepestGeneric: " + ((VariableTree) typeDecl).deepestGeneric()); - if( ((VariableTree) typeDecl).deepestGeneric() >= 3){ - logger.accept(preText + "Field not Wildcard compatible"); + noIncompatibleFields = true; + try { + currentClassName = node.getSimpleName().toString(); + node.getMembers().forEach(typeDecl -> { + if (typeDecl instanceof VariableTree) { + logger.accept(preText + "Field Type: " + ((VariableTree) typeDecl).getType().toString()); + logger.accept(preText + "deepestGeneric: " + ((VariableTree) typeDecl).deepestGeneric()); + if (((VariableTree) typeDecl).deepestGeneric() >= 3) { + logger.accept(preText + "Field not Wildcard compatible"); + noIncompatibleFields = false; + } + //logger.accept("deepestGeneric: " + ((VariableTree)((VariableTree) typeDecl).getType()).deepestGeneric()); } - //logger.accept("deepestGeneric: " + ((VariableTree)((VariableTree) typeDecl).getType()).deepestGeneric()); - } - }); + }); + } catch (Exception e) {} + if(noIncompatibleFields){ + logger.accept(preText + "Wildcard compatible class"); + }else{ + logger.accept(preText + "Wildcard incompatible class"); + } return super.visitClass(node, unused); } @@ -133,6 +145,15 @@ public class WildcardFinderPlugin extends TreeScanner implements Plu if(typeText.contains("capture#") && methString.startsWith("<")){ //we found a capture conversion //System.out.println(node.getClass().getMethod("getStartPosition").invoke(node)); logger.accept(preText + "CC!: "+ "Arg#"+argumentNumber + ": " + typeText + " in " + currentSource + " " + lineOfPosition(currentClassContent,methodPos) + " of method " + methString); + logger.accept(preText + " in class:" + node.getOwner()); + String ownerName = node.getOwner(); + int i = node.getOwner().lastIndexOf('.'); + if (i > 0) { + ownerName = node.getOwner().substring(i+1); + } + if(ownerName.equals(currentClassName)){ + logger.accept(preText + " in same class"); + } }else if(typeText.contains("capture#")) { logger.accept(preText + "CC: "+ "Arg#"+argumentNumber + ": " + typeText + " in " + currentSource + " " + lineOfPosition(currentClassContent,methodPos) + " of method " + methString); }else{ diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java index 0e60316df37..93beedf2c29 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java @@ -758,6 +758,10 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { } public abstract static class JCExpression extends JCTree implements ExpressionTree { + + public String getOwner() { + return "Not implemented"; + } @Override public JCExpression setType(Type type) { super.setType(type); @@ -768,7 +772,6 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { super.setPos(pos); return this; } - public boolean isPoly() { return false; } public boolean isStandalone() { return true; } @@ -1875,6 +1878,9 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { public List getArguments() { return args; } + public String getOwner(){ + return meth.getOwner(); + } @Override @DefinedBy(Api.COMPILER_TREE) public R accept(TreeVisitor v, D d) { return v.visitMethodInvocation(this, d); @@ -2593,6 +2599,11 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { public Tag getTag() { return SELECT; } + + @Override + public String getOwner() { + return sym.owner.toString(); + } } /** @@ -2725,6 +2736,11 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { public Tag getTag() { return IDENT; } + + @Override + public String getOwner() { + return sym.owner.toString(); + } } /**