Skip to content

Commit

Permalink
Add support for simple wildcard types in DSLD contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 21, 2018
1 parent b781cbe commit 2e35482
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,39 @@ final class DSLInferencingTests extends DSLInferencingTestSuite {
assertType(contents, 0, contents.length(), 'java.util.Map<java.lang.String[],java.lang.Integer[]>')
}

@Test
void testWildcardType1() {
createDsls '''\
contribute(currentType()) {
property name:'prop', type:'Class<? extends java.lang.annotation.Annotation>'
}
'''.stripIndent()
String contents = 'prop'
assertType(contents, 0, contents.length(), 'java.lang.Class<? extends java.lang.annotation.Annotation>')
}

@Test
void testWildcardType2() {
createDsls '''\
contribute(currentType()) {
property name:'prop', type:'Map<String, ? extends java.lang.annotation.Annotation>'
}
'''.stripIndent()
String contents = 'prop'
assertType(contents, 0, contents.length(), 'java.util.Map<java.lang.String,? extends java.lang.annotation.Annotation>')
}

@Test
void testWildcardType3() {
createDsls '''\
contribute(currentType()) {
property name:'prop', type:'Map<String, List<? extends java.lang.annotation.Annotation>>'
}
'''.stripIndent()
String contents = 'prop'
assertType(contents, 0, contents.length(), 'java.util.Map<java.lang.String,java.util.List<? extends java.lang.annotation.Annotation>>')
}

@Test
void testNestedCalls() {
createDsls '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,26 @@ public ClassNode resolve(String name) {

// now recur down through the type parameters
if (typeParamStart > 0 && type.isUsingGenerics()) {
String[] typeParameterNames = componentName.substring(typeParamStart + 1, componentName.length() - 1).split(",");
String[] typeParameterNames = componentName.substring(typeParamStart + 1, componentName.length() - 1).split("\\s*,\\s*");
ClassNode[] typeParameterTypes = new ClassNode[typeParameterNames.length];
for (int i = 0; i < typeParameterNames.length; i += 1) {
typeParameterTypes[i] = resolve(typeParameterNames[i]); // TODO: Handle "? extends T" and "? super T"
typeParameterTypes[i] = resolve(typeParameterNames[i].replaceFirst("^\\?\\s+(extends|super)\\s+", ""));
}
type = VariableScope.clone(type);
GenericsType[] genericsTypes = type.getGenericsTypes();
// need to be careful here...there may be too many or too few type parameters
for (int i = 0; i < genericsTypes.length && i < typeParameterTypes.length; i += 1) {
if (typeParameterNames[i].startsWith("?")) {
genericsTypes[i] = GenericsUtils.buildWildcardType(typeParameterTypes[i]);
} else {
genericsTypes[i].setPlaceholder(false); // must precede setType
genericsTypes[i].setName(typeParameterTypes[i].getName());
genericsTypes[i].setType(typeParameterTypes[i]);
genericsTypes[i].setUpperBounds(null);
genericsTypes[i].setLowerBound(null);
genericsTypes[i].setWildcard(false);
}
genericsTypes[i].setResolved(true);
genericsTypes[i].setWildcard(false);
genericsTypes[i].setLowerBound(null);
genericsTypes[i].setUpperBounds(null);
genericsTypes[i].setPlaceholder(false);
genericsTypes[i].setType(typeParameterTypes[i]);
genericsTypes[i].setName(typeParameterTypes[i].getName());
}
nameTypeCache.put(componentName, type);
}
Expand Down

1 comment on commit 2e35482

@eric-milles
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.