diff --git a/src/jdk.compiler/share/classes/com/sun/source/tree/VariableTree.java b/src/jdk.compiler/share/classes/com/sun/source/tree/VariableTree.java index a1191b2f8e6..d550dad89b4 100644 --- a/src/jdk.compiler/share/classes/com/sun/source/tree/VariableTree.java +++ b/src/jdk.compiler/share/classes/com/sun/source/tree/VariableTree.java @@ -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> + * @return + */ + int deepestGeneric(); + /** * Returns the initializer for the variable, or {@code null} if none. * @return the initializer 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 5ecec214766..ce864e03da9 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 @@ -12,6 +12,7 @@ public class WildcardFinderPlugin extends TreeScanner implements Plu private final Consumer logger; public WildcardFinderPlugin(Consumer logger){ + this.logger = logger; } @Override @@ -38,6 +39,21 @@ public class WildcardFinderPlugin extends TreeScanner implements Plu javacTask.addTaskListener(this); } + @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"); + } + //logger.accept("deepestGeneric: " + ((VariableTree)((VariableTree) typeDecl).getType()).deepestGeneric()); + } + }); + return super.visitClass(node, unused); + } + String currentSource = ""; String currentClassContent = ""; @Override diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java index 7bcef30a3f9..8934af73528 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java @@ -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; 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 a2837013506..0e60316df37 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 @@ -1076,6 +1076,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;