use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class GenericsUtils method parseClassNodesFromString.
public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) {
GroovyLexer lexer = new GroovyLexer(new StringReader("DummyNode<" + option + ">"));
final GroovyRecognizer rn = GroovyRecognizer.make(lexer);
try {
rn.classOrInterfaceType(true);
final AtomicReference<ClassNode> ref = new AtomicReference<ClassNode>();
AntlrParserPlugin plugin = new AntlrParserPlugin() {
@Override
public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) throws ParserException {
ref.set(makeTypeWithArguments(rn.getAST()));
return null;
}
};
plugin.buildAST(null, null, null);
ClassNode parsedNode = ref.get();
// the returned node is DummyNode<Param1, Param2, Param3, ...)
GenericsType[] parsedNodeGenericsTypes = parsedNode.getGenericsTypes();
if (parsedNodeGenericsTypes == null) {
return null;
}
ClassNode[] signature = new ClassNode[parsedNodeGenericsTypes.length];
for (int i = 0; i < parsedNodeGenericsTypes.length; i++) {
final GenericsType genericsType = parsedNodeGenericsTypes[i];
signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType());
}
return signature;
} catch (RecognitionException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
} catch (TokenStreamException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
} catch (ParserException e) {
sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
}
return null;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class GenericsUtils method extractSuperClassGenerics.
private static void extractSuperClassGenerics(GenericsType[] usage, GenericsType[] declaration, Map<String, ClassNode> spec) {
// if declaration does not provide generics, there is no connection to make
if (usage == null || declaration == null || declaration.length == 0)
return;
if (usage.length != declaration.length)
return;
// both have generics
for (int i = 0; i < usage.length; i++) {
GenericsType ui = usage[i];
GenericsType di = declaration[i];
if (di.isPlaceholder()) {
spec.put(di.getName(), ui.getType());
} else if (di.isWildcard()) {
if (ui.isWildcard()) {
extractSuperClassGenerics(ui.getLowerBound(), di.getLowerBound(), spec);
extractSuperClassGenerics(ui.getUpperBounds(), di.getUpperBounds(), spec);
} else {
ClassNode cu = ui.getType();
extractSuperClassGenerics(cu, di.getLowerBound(), spec);
ClassNode[] upperBounds = di.getUpperBounds();
if (upperBounds != null) {
for (ClassNode cn : upperBounds) {
extractSuperClassGenerics(cu, cn, spec);
}
}
}
} else {
extractSuperClassGenerics(ui.getType(), di.getType(), spec);
}
}
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class TypeSignatureParser method createWildcard.
private static GenericsType createWildcard(ClassNode[] upper, ClassNode lower) {
ClassNode base = ClassHelper.makeWithoutCaching("?");
base.setRedirect(ClassHelper.OBJECT_TYPE);
GenericsType t = new GenericsType(base, upper, lower);
t.setWildcard(true);
return t;
}
use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.
the class SignatureCodecVersion1 method doEncode.
private void doEncode(final ClassNode node, DataOutputStream dos) throws IOException {
dos.writeUTF(node.getClass().getSimpleName());
if (node instanceof UnionTypeClassNode) {
UnionTypeClassNode union = (UnionTypeClassNode) node;
ClassNode[] delegates = union.getDelegates();
dos.writeInt(delegates.length);
for (ClassNode delegate : delegates) {
doEncode(delegate, dos);
}
return;
} else if (node instanceof WideningCategories.LowestUpperBoundClassNode) {
WideningCategories.LowestUpperBoundClassNode lub = (WideningCategories.LowestUpperBoundClassNode) node;
dos.writeUTF(lub.getLubName());
doEncode(lub.getUnresolvedSuperClass(), dos);
ClassNode[] interfaces = lub.getInterfaces();
if (interfaces == null) {
dos.writeInt(-1);
} else {
dos.writeInt(interfaces.length);
for (ClassNode anInterface : interfaces) {
doEncode(anInterface, dos);
}
}
return;
}
if (node.isArray()) {
dos.writeBoolean(true);
doEncode(node.getComponentType(), dos);
} else {
dos.writeBoolean(false);
dos.writeUTF(BytecodeHelper.getTypeDescription(node));
dos.writeBoolean(node.isUsingGenerics());
GenericsType[] genericsTypes = node.getGenericsTypes();
if (genericsTypes == null) {
dos.writeInt(-1);
} else {
dos.writeInt(genericsTypes.length);
for (GenericsType type : genericsTypes) {
dos.writeBoolean(type.isPlaceholder());
dos.writeBoolean(type.isWildcard());
doEncode(type.getType(), dos);
ClassNode lb = type.getLowerBound();
if (lb == null) {
dos.writeBoolean(false);
} else {
dos.writeBoolean(true);
doEncode(lb, dos);
}
ClassNode[] upperBounds = type.getUpperBounds();
if (upperBounds == null) {
dos.writeInt(-1);
} else {
dos.writeInt(upperBounds.length);
for (ClassNode bound : upperBounds) {
doEncode(bound, dos);
}
}
}
}
}
}
use of org.codehaus.groovy.ast.GenericsType in project gradle by gradle.
the class GradleResolveVisitor method visitConstructorOrMethod.
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
VariableScope oldScope = currentScope;
currentScope = node.getVariableScope();
Map<String, GenericsType> oldPNames = genericParameterNames;
genericParameterNames = new HashMap<String, GenericsType>(genericParameterNames);
resolveGenericsHeader(node.getGenericsTypes());
Parameter[] paras = node.getParameters();
for (Parameter p : paras) {
p.setInitialExpression(transform(p.getInitialExpression()));
resolveOrFail(p.getType(), p.getType());
visitAnnotations(p);
}
ClassNode[] exceptions = node.getExceptions();
for (ClassNode t : exceptions) {
resolveOrFail(t, node);
}
resolveOrFail(node.getReturnType(), node);
MethodNode oldCurrentMethod = currentMethod;
currentMethod = node;
super.visitConstructorOrMethod(node, isConstructor);
currentMethod = oldCurrentMethod;
genericParameterNames = oldPNames;
currentScope = oldScope;
}
Aggregations