Compare commits

...

4 Commits

6 changed files with 100 additions and 8 deletions

View File

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

View File

@ -71,6 +71,15 @@ public interface VariableTree extends StatementTree {
*/
Tree getType();
/**
* 0 - no generic
* 1 - is a generic
* 2 - contains a generic
* 3 - contains a generic like List<List<X>>
* @return
*/
int deepestGeneric();
/**
* Returns the initializer for the variable, or {@code null} if none.
* @return the initializer

View File

@ -201,7 +201,7 @@ public class BasicJavacTask extends JavacTask {
PlatformDescription platformProvider = context.get(PlatformDescription.class);
//ANDI: Init our own plugin to count CC's
initPlugin(new WildcardFinderPlugin((s -> Log.instance(context).printRawLines(s))));
initPlugin(new WildcardFinderPlugin((s -> Log.instance(context).printRawLines(Log.WriterKind.NOTICE,s))));
if (platformProvider != null) {
for (PluginInfo<Plugin> pluginDesc : platformProvider.getPlugins()) {
java.util.List<String> options =

View File

@ -1,10 +1,9 @@
package com.sun.tools.javac.api;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.*;
import com.sun.source.util.*;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.util.function.Consumer;
@ -13,6 +12,7 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
private final Consumer<String> logger;
public WildcardFinderPlugin(Consumer<String> logger){
this.logger = logger;
}
@Override
@ -39,16 +39,45 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
javacTask.addTaskListener(this);
}
String currentClassName = "";
Boolean noIncompatibleFields = true;
@Override
public Void visitClass(ClassTree node, Void unused) {
noIncompatibleFields = true;
try {
currentClassName = currentPackage + "." + 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());
}
});
} catch (Exception e) {}
if(noIncompatibleFields){
logger.accept(preText + "Wildcard compatible class");
}else{
logger.accept(preText + "Wildcard incompatible class");
}
return super.visitClass(node, unused);
}
String currentPackage = "";
String currentSource = "";
String currentClassContent = "";
@Override
public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
//System.out.println(node.getSourceFile().getName());
currentPackage = node.getPackageName()!=null?node.getPackageName().toString():"";
currentSource = node.getSourceFile().getName();
try {
currentClassContent = String.valueOf(node.getSourceFile().getCharContent(true));
} catch (IOException e) {
logger.accept(preText + "error5: " + e);
}
return super.visitCompilationUnit(node, unused);
}
@ -71,7 +100,8 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
var pos_field = node.getClass().getField("pos");
type_field.setAccessible(true);
pos_field.setAccessible(true);
String typeText = type_field.get(node).toString();
//Some fields (or "members") do not have a type... TODO: fix this
String typeText = type_field.get(node)!=null?type_field.get(node).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));
@ -88,7 +118,8 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
}
} catch (Throwable e){
//System.out.println("Error in method invocation: " + e.getMessage()+
// " in "+currentSource);i
// " in "+currentSource);
logger.accept(preText + "error4: " + e);
}
return super.visitMemberSelect(node, unused);
}
@ -118,6 +149,11 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> 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();
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{
@ -127,11 +163,14 @@ public class WildcardFinderPlugin extends TreeScanner<Void, Void> implements Plu
}
} catch (NoSuchFieldException e) {
//System.out.println("Argument has no 'type' field");
logger.accept(preText + "error1: " + e);
} catch (IllegalAccessException e) {
//System.out.println("Illegal access of 'type' field");
logger.accept(preText + "error2: " + e);
} catch (Throwable e){
//System.out.println("Error in method invocation: " + e.getMessage()+
// " in "+currentSource);
logger.accept(preText + "error3: " + e);
}
return super.visitMethodInvocation(node, unused);
}

View File

@ -82,6 +82,9 @@ import static com.sun.tools.javac.code.TypeTag.*;
*/
public abstract class Type extends AnnoConstruct implements TypeMirror, PoolConstant {
public int deepestGeneric(){
return 0;
}
/**
* Type metadata, Should be {@code null} for the default value.
*
@ -898,6 +901,11 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
};
}
@Override
public int deepestGeneric() {
return 0;
}
@Override
public TypeTag getTag() {
return WILDCARD;
@ -1031,6 +1039,12 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
this.interfaces_field = null;
}
@Override
public int deepestGeneric() {
int deepest = typarams_field.stream().mapToInt(t -> t.deepestGeneric()).max().orElse(0);
return deepest>0?1 + deepest:0;
}
public int poolTag() {
return ClassFile.CONSTANT_Class;
}
@ -1354,6 +1368,11 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
this(that.elemtype, that.tsym, that.getMetadata());
}
@Override
public int deepestGeneric() {
return elemtype.deepestGeneric();
}
public int poolTag() {
return ClassFile.CONSTANT_Class;
}
@ -1699,6 +1718,11 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
};
}
@Override
public int deepestGeneric() {
return 1;
}
@Override
public TypeTag getTag() {
return TYPEVAR;

View File

@ -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; }
@ -1076,6 +1079,9 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public JCExpression getNameExpression() { return nameexpr; }
@DefinedBy(Api.COMPILER_TREE)
public JCTree getType() { return vartype; }
public int deepestGeneric(){
return type.deepestGeneric();
}
@DefinedBy(Api.COMPILER_TREE)
public JCExpression getInitializer() {
return init;
@ -1872,6 +1878,9 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public List<JCExpression> getArguments() {
return args;
}
public String getOwner(){
return meth.getOwner();
}
@Override @DefinedBy(Api.COMPILER_TREE)
public <R,D> R accept(TreeVisitor<R,D> v, D d) {
return v.visitMethodInvocation(this, d);
@ -2590,6 +2599,11 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public Tag getTag() {
return SELECT;
}
@Override
public String getOwner() {
return sym.owner.toString();
}
}
/**
@ -2722,6 +2736,11 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public Tag getTag() {
return IDENT;
}
@Override
public String getOwner() {
return sym.owner.toString();
}
}
/**