Java Code Examples for org.codehaus.groovy.ast.ClassNode#getUnresolvedSuperClass()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getUnresolvedSuperClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 6 votes vote down vote up
public void visitClass(ClassNode node) {
	URI uri = sourceUnit.getSource().getURI();
	classNodesByURI.get(uri).add(node);
	pushASTNode(node);
	try {
		ClassNode unresolvedSuperClass = node.getUnresolvedSuperClass();
		if (unresolvedSuperClass != null && unresolvedSuperClass.getLineNumber() != -1) {
			pushASTNode(unresolvedSuperClass);
			popASTNode();
		}
		for (ClassNode unresolvedInterface : node.getUnresolvedInterfaces()) {
			if (unresolvedInterface.getLineNumber() == -1) {
				continue;
			}
			pushASTNode(unresolvedInterface);
			popASTNode();
		}
		super.visitClass(node);
	} finally {
		popASTNode();
	}
}
 
Example 2
Source File: VariableScopeVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addClassNodeOccurrences(ClassNode visitedNode, ClassNode findingNode) {
    final String findingName = ElementUtils.getTypeName(findingNode);
    final ClassNode superClass = visitedNode.getUnresolvedSuperClass(false);
    final ClassNode[] interfaces = visitedNode.getInterfaces();

    // Check if the caret is on the ClassNode itself
    if (findingName.equals(visitedNode.getName())) {
        occurrences.add(new FakeASTNode(visitedNode, visitedNode.getNameWithoutPackage()));
    }

    // Check if the caret is on the parent type
    if (superClass.getLineNumber() > 0 && superClass.getColumnNumber() > 0) {
        if (findingName.equals(superClass.getName())) {
            occurrences.add(new FakeASTNode(superClass, superClass.getNameWithoutPackage()));
        }
    }

    // Check all implemented interfaces
    for (ClassNode interfaceNode : interfaces) {
        if (interfaceNode.getLineNumber() > 0 && interfaceNode.getColumnNumber() > 0) {
            if (findingName.equals(interfaceNode.getName())) {
                occurrences.add(new FakeASTNode(interfaceNode, interfaceNode.getNameWithoutPackage()));
            }
        }
    }

    // Check all class level annotations
    for (AnnotationNode annotation : visitedNode.getAnnotations(findingNode)) {
        addAnnotationOccurrences(annotation, findingNode);
    }
}
 
Example 3
Source File: GenericsVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean checkWildcard(ClassNode cn) {
    ClassNode sn = cn.getUnresolvedSuperClass(false);
    if (sn == null) return false;
    GenericsType[] generics = sn.getGenericsTypes();
    if (generics == null) return false;
    boolean error = false;
    for (GenericsType generic : generics) {
        if (generic.isWildcard()) {
            addError("A supertype may not specify a wildcard type", sn);
            error = true;
        }
    }
    return error;
}
 
Example 4
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void replaceExtendsByImplements(final ClassNode cNode) {
    ClassNode superClass = cNode.getUnresolvedSuperClass();
    if (Traits.isTrait(superClass)) {
        // move from super class to interface;
        cNode.setSuperClass(ClassHelper.OBJECT_TYPE);
        cNode.setUnresolvedSuperClass(ClassHelper.OBJECT_TYPE);
        cNode.addInterface(superClass);
        resolveScope(cNode);
    }
}
 
Example 5
Source File: WideningCategories.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode findGenericsTypeHolderForClass(ClassNode source, ClassNode type) {
    if (isPrimitiveType(source)) source = getWrapper(source);
    if (source.equals(type)) return source;
    if (type.isInterface()) {
        for (ClassNode interfaceNode : source.getAllInterfaces()) {
            if (interfaceNode.equals(type)) {
                ClassNode parameterizedInterface = GenericsUtils.parameterizeType(source, interfaceNode);
                return parameterizedInterface;
            }
        }
    }
    ClassNode superClass = source.getUnresolvedSuperClass();
    // copy generic type information if available
    if (superClass!=null && superClass.isUsingGenerics()) {
        Map<GenericsTypeName, GenericsType> genericsTypeMap = GenericsUtils.extractPlaceholders(source);
        GenericsType[] genericsTypes = superClass.getGenericsTypes();
        if (genericsTypes!=null) {
            GenericsType[] copyTypes = new GenericsType[genericsTypes.length];
            for (int i = 0; i < genericsTypes.length; i++) {
                GenericsType genericsType = genericsTypes[i];
                GenericsTypeName gtn = new GenericsTypeName(genericsType.getName());
                if (genericsType.isPlaceholder() && genericsTypeMap.containsKey(gtn)) {
                    copyTypes[i] = genericsTypeMap.get(gtn);
                } else {
                    copyTypes[i] = genericsType;
                }
            }
            superClass = superClass.getPlainNodeReference();
            superClass.setGenericsTypes(copyTypes);
        }
    }
    if (superClass!=null) return findGenericsTypeHolderForClass(superClass, type);
    return null;
}
 
Example 6
Source File: GenericsUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<ClassNode> getAllUnresolvedSuperClasses(ClassNode actualReceiver) {
    List<ClassNode> superClassNodeList = new LinkedList<>();

    for (ClassNode cn = actualReceiver.getUnresolvedSuperClass(); null != cn && ClassHelper.OBJECT_TYPE != cn; cn = cn.getUnresolvedSuperClass()) {
        superClassNodeList.add(cn);
    }

    return superClassNodeList;
}
 
Example 7
Source File: ExtendedVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean isOverrideMethod(MethodNode method) {
    ClassNode cNode = method.getDeclaringClass();
    ClassNode next = cNode;
    outer:
    while (next != null) {
        Map<String, ClassNode> genericsSpec = createGenericsSpec(next);
        MethodNode mn = correctToGenericsSpec(genericsSpec, method);
        if (next != cNode) {
            ClassNode correctedNext = correctToGenericsSpecRecurse(genericsSpec, next);
            MethodNode found = getDeclaredMethodCorrected(genericsSpec, mn, correctedNext);
            if (found != null) break;
        }
        List<ClassNode> ifaces = new ArrayList<>(Arrays.asList(next.getInterfaces()));
        while (!ifaces.isEmpty()) {
            ClassNode origInterface = ifaces.remove(0);
            if (!origInterface.equals(ClassHelper.OBJECT_TYPE)) {
                genericsSpec = createGenericsSpec(origInterface, genericsSpec);
                ClassNode iNode = correctToGenericsSpecRecurse(genericsSpec, origInterface);
                MethodNode found2 = getDeclaredMethodCorrected(genericsSpec, mn, iNode);
                if (found2 != null) break outer;
                Collections.addAll(ifaces, iNode.getInterfaces());
            }
        }
        ClassNode superClass = next.getUnresolvedSuperClass();
        if (superClass != null) {
            next = correctToGenericsSpecRecurse(genericsSpec, superClass);
        } else {
            next = null;
        }
    }
    return next != null;
}
 
Example 8
Source File: BytecodeHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean usesGenericsInClassSignature(ClassNode node) {
    if (!node.isUsingGenerics()) return false;
    if (hasGenerics(node)) return true;
    ClassNode sclass = node.getUnresolvedSuperClass(false);
    if (sclass.isUsingGenerics()) return true;
    ClassNode[] interfaces = node.getInterfaces();
    if (interfaces != null) {
        for (ClassNode anInterface : interfaces) {
            if (anInterface.isUsingGenerics()) return true;
        }
    }
    return false;
}
 
Example 9
Source File: BytecodeHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static String getGenericsSignature(ClassNode node) {
    if (!usesGenericsInClassSignature(node)) return null;
    GenericsType[] genericsTypes = node.getGenericsTypes();
    StringBuilder ret = new StringBuilder(100);
    getGenericsTypeSpec(ret, genericsTypes);
    GenericsType extendsPart = new GenericsType(node.getUnresolvedSuperClass(false));
    writeGenericsBounds(ret, extendsPart, true);
    ClassNode[] interfaces = node.getInterfaces();
    for (ClassNode anInterface : interfaces) {
        GenericsType interfacePart = new GenericsType(anInterface);
        writeGenericsBounds(ret, interfacePart, false);
    }
    return ret.toString();
}
 
Example 10
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Parameter[] selectAccessibleConstructorFromSuper(ConstructorNode node) {
    ClassNode type = node.getDeclaringClass();
    ClassNode superType = type.getUnresolvedSuperClass();

    Parameter[] bestMatch = null;
    for (ConstructorNode c : superType.getDeclaredConstructors()) {
        // Only look at things we can actually call
        if (!c.isPublic() && !c.isProtected()) continue;
        Parameter[] parameters = c.getParameters();
        // workaround for GROOVY-5859: remove generic type info
        Parameter[] copy = new Parameter[parameters.length];
        for (int i = 0; i < copy.length; i++) {
            Parameter orig = parameters[i];
            copy[i] = new Parameter(orig.getOriginType().getPlainNodeReference(), orig.getName());
        }
        if (noExceptionToAvoid(node,c)) return copy;
        if (bestMatch==null) bestMatch = copy;
    }
    if (bestMatch!=null) return bestMatch;

    // fall back for parameterless constructor
    if (superType.isPrimaryClassNode()) {
        return Parameter.EMPTY_ARRAY;
    }

    return null;
}