getOwner MEthod added and check if classnames are the same when calling polymorphic methods with captured wildcards

This commit is contained in:
Andreas Stadelmeier 2024-12-22 02:21:43 +01:00
parent f63a8bcd4e
commit 09ab9a7e02
3 changed files with 48 additions and 10 deletions

View File

@ -61,4 +61,5 @@ public interface MethodInvocationTree extends ExpressionTree {
* @return the arguments * @return the arguments
*/ */
List<? extends ExpressionTree> getArguments(); List<? extends ExpressionTree> getArguments();
String getOwner();
} }

View File

@ -39,18 +39,30 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
javacTask.addTaskListener(this); javacTask.addTaskListener(this);
} }
String currentClassName = "";
Boolean noIncompatibleFields = true;
@Override @Override
public Void visitClass(ClassTree node, Void unused) { public Void visitClass(ClassTree node, Void unused) {
node.getMembers().forEach(typeDecl -> { noIncompatibleFields = true;
if (typeDecl instanceof VariableTree) { try {
logger.accept(preText + "Field Type: " + ((VariableTree) typeDecl).getType().toString()); currentClassName = node.getSimpleName().toString();
logger.accept(preText + "deepestGeneric: " + ((VariableTree) typeDecl).deepestGeneric()); node.getMembers().forEach(typeDecl -> {
if( ((VariableTree) typeDecl).deepestGeneric() >= 3){ if (typeDecl instanceof VariableTree) {
logger.accept(preText + "Field not Wildcard compatible"); 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); return super.visitClass(node, unused);
} }
@ -133,6 +145,15 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
if(typeText.contains("capture#") && methString.startsWith("<")){ //we found a capture conversion if(typeText.contains("capture#") && methString.startsWith("<")){ //we found a capture conversion
//System.out.println(node.getClass().getMethod("getStartPosition").invoke(node)); //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 + "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#")) { }else if(typeText.contains("capture#")) {
logger.accept(preText + "CC: "+ "Arg#"+argumentNumber + ": " + typeText + " in " + currentSource + " " + lineOfPosition(currentClassContent,methodPos) + " of method " + methString); logger.accept(preText + "CC: "+ "Arg#"+argumentNumber + ": " + typeText + " in " + currentSource + " " + lineOfPosition(currentClassContent,methodPos) + " of method " + methString);
}else{ }else{

View File

@ -758,6 +758,10 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
} }
public abstract static class JCExpression extends JCTree implements ExpressionTree { public abstract static class JCExpression extends JCTree implements ExpressionTree {
public String getOwner() {
return "Not implemented";
}
@Override @Override
public JCExpression setType(Type type) { public JCExpression setType(Type type) {
super.setType(type); super.setType(type);
@ -768,7 +772,6 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
super.setPos(pos); super.setPos(pos);
return this; return this;
} }
public boolean isPoly() { return false; } public boolean isPoly() { return false; }
public boolean isStandalone() { return true; } public boolean isStandalone() { return true; }
@ -1875,6 +1878,9 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public List<JCExpression> getArguments() { public List<JCExpression> getArguments() {
return args; return args;
} }
public String getOwner(){
return meth.getOwner();
}
@Override @DefinedBy(Api.COMPILER_TREE) @Override @DefinedBy(Api.COMPILER_TREE)
public <R,D> R accept(TreeVisitor<R,D> v, D d) { public <R,D> R accept(TreeVisitor<R,D> v, D d) {
return v.visitMethodInvocation(this, d); return v.visitMethodInvocation(this, d);
@ -2593,6 +2599,11 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public Tag getTag() { public Tag getTag() {
return SELECT; 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() { public Tag getTag() {
return IDENT; return IDENT;
} }
@Override
public String getOwner() {
return sym.owner.toString();
}
} }
/** /**