Skip to content

Commit

Permalink
GROOVY-9822
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 19, 2020
1 parent ee2e716 commit 789df42
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,57 @@ public void testTypeChecked9803() {

runConformTest(sources, "123");
}

@Test
public void testTypeChecked9822() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"GraphTraversalSource test(Graph graph) {\n" +
" def strategy = ReadOnlyStrategy.instance()\n" +
" graph.traversal().withStrategies(strategy)\n" +
"}\n",

"Types.java", // from org.apache.tinkerpop:gremlin-core:3.4.8
"@SuppressWarnings(\"rawtypes\")\n" +
"interface TraversalStrategy<S extends TraversalStrategy> extends Comparable<Class<? extends TraversalStrategy>> {\n" +
" interface VerificationStrategy extends TraversalStrategy<VerificationStrategy> {\n" +
" }\n" +
"}\n" +
"@SuppressWarnings(\"rawtypes\")\n" +
"abstract class AbstractTraversalStrategy<S extends TraversalStrategy> implements TraversalStrategy<S> {\n" +
"}\n" +
"abstract\n" + // don't want to implement Comparable
"class ReadOnlyStrategy extends AbstractTraversalStrategy<TraversalStrategy.VerificationStrategy>\n" +
" implements TraversalStrategy.VerificationStrategy {\n" +
" static ReadOnlyStrategy instance() { return null; }\n" +
"}\n" +
"interface TraversalSource extends Cloneable, AutoCloseable {\n" +
" @SuppressWarnings(\"rawtypes\")\n" +
" default TraversalSource withStrategies(TraversalStrategy... strategies) {\n" +
" return null;\n" +
" }\n" +
"}\n" +
"abstract\n" + // don't want to implement AutoCloseable
"class GraphTraversalSource implements TraversalSource {\n" +
" @Override\n" +
" @SuppressWarnings(\"rawtypes\")\n" +
" public GraphTraversalSource withStrategies(TraversalStrategy... strategies) {\n" +
" return (GraphTraversalSource) TraversalSource.super.withStrategies(strategies);\n" +
" }\n" +
"}\n" +
"class Graph {\n" +
" public <C extends TraversalSource> C traversal(Class<C> c) {\n" +
" return null;\n" +
" }\n" +
" public GraphTraversalSource traversal() {\n" +
" return null;\n" +
" }\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1993,9 +1993,7 @@ private static boolean hasNonTrivialBounds(GenericsType gt) {
}

// GRECLIPSE private->package
static ClassNode[] applyGenericsContext(
Map<GenericsTypeName, GenericsType> spec, ClassNode[] bounds
) {
static ClassNode[] applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final ClassNode[] bounds) {
if (bounds == null) return null;
ClassNode[] newBounds = new ClassNode[bounds.length];
for (int i = 0; i < bounds.length; i++) {
Expand All @@ -2004,26 +2002,38 @@ static ClassNode[] applyGenericsContext(
return newBounds;
}

static ClassNode applyGenericsContext(
Map<GenericsTypeName, GenericsType> spec, ClassNode bound
) {
static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final ClassNode bound) {
/* GRECLIPSE edit -- GROOVY-9822
if (bound == null) return null;
*/
if (bound == null || !isUsingGenericsOrIsArrayUsingGenerics(bound)) {
return bound;
}
// GRECLIPSE end
if (bound.isArray()) {
return applyGenericsContext(spec, bound.getComponentType()).makeArray();
}
/* GRECLIPSE edit -- GROOVY-9822
if (!bound.isUsingGenerics()) return bound;
ClassNode newBound = bound.getPlainNodeReference();
newBound.setGenericsTypes(applyGenericsContext(spec, bound.getGenericsTypes()));
*/
ClassNode newBound = bound.getPlainNodeReference();
GenericsType[] gt = bound.getGenericsTypes();
if (spec != null && !spec.isEmpty()) {
gt = applyGenericsContext(spec, gt);
}
newBound.setGenericsTypes(gt);
// GRECLIPSE end
if (bound.isGenericsPlaceHolder()) {
GenericsType[] gt = newBound.getGenericsTypes();
boolean hasBounds = hasNonTrivialBounds(gt[0]);
if (hasBounds || !gt[0].isPlaceholder()) return getCombinedBoundType(gt[0]);
String placeHolderName = newBound.getGenericsTypes()[0].getName();
String placeHolderName = gt[0].getName();
if (!placeHolderName.equals(newBound.getUnresolvedName())) {
// we should produce a clean placeholder ClassNode here
ClassNode clean = make(placeHolderName);
clean.setGenericsTypes(newBound.getGenericsTypes());
clean.setRedirect(newBound);
clean.setGenericsTypes(gt);
newBound = clean;
}
newBound.setGenericsPlaceHolder(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1882,23 +1882,37 @@ static ClassNode[] applyGenericsContext(final Map<GenericsTypeName, GenericsType
}

static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final ClassNode bound) {
/* GRECLIPSE edit -- GROOVY-9822
if (bound == null) return null;
*/
if (bound == null || !isUsingGenericsOrIsArrayUsingGenerics(bound)) {
return bound;
}
// GRECLIPSE end
if (bound.isArray()) {
return applyGenericsContext(spec, bound.getComponentType()).makeArray();
}
/* GRECLIPSE edit -- GROOVY-9822
if (!bound.isUsingGenerics()) return bound;
ClassNode newBound = bound.getPlainNodeReference();
newBound.setGenericsTypes(applyGenericsContext(spec, bound.getGenericsTypes()));
*/
ClassNode newBound = bound.getPlainNodeReference();
GenericsType[] gt = bound.getGenericsTypes();
if (asBoolean(spec)) {
gt = applyGenericsContext(spec, gt);
}
newBound.setGenericsTypes(gt);
// GRECLIPSE end
if (bound.isGenericsPlaceHolder()) {
GenericsType[] gt = newBound.getGenericsTypes();
boolean hasBounds = hasNonTrivialBounds(gt[0]);
if (hasBounds || !gt[0].isPlaceholder()) return getCombinedBoundType(gt[0]);
String placeHolderName = newBound.getGenericsTypes()[0].getName();
String placeHolderName = gt[0].getName();
if (!placeHolderName.equals(newBound.getUnresolvedName())) {
// we should produce a clean placeholder ClassNode here
ClassNode clean = make(placeHolderName);
clean.setGenericsTypes(newBound.getGenericsTypes());
clean.setRedirect(newBound);
clean.setGenericsTypes(gt);
newBound = clean;
}
newBound.setGenericsPlaceHolder(true);
Expand Down

0 comments on commit 789df42

Please sign in to comment.