Search in sources :

Example 1 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.

the class ToIndexFileConverter method visit.

@Override
public Void visit(MethodDeclaration decl, AElement elem) {
    Type type = decl.getType();
    List<Parameter> params = decl.getParameters();
    List<TypeParameter> typeParams = decl.getTypeParameters();
    Optional<ReceiverParameter> rcvrParam = decl.getReceiverParameter();
    BlockStmt body = decl.getBody().orElse(null);
    StringBuilder sb = new StringBuilder(decl.getNameAsString()).append('(');
    AClass clazz = (AClass) elem;
    AMethod method;
    if (params != null) {
        for (Parameter param : params) {
            Type ptype = param.getType();
            sb.append(getJVML(ptype));
        }
    }
    sb.append(')').append(getJVML(type));
    method = clazz.methods.vivify(sb.toString());
    visitDecl(decl, method);
    visitType(type, method.returnType);
    if (params != null) {
        for (int i = 0; i < params.size(); i++) {
            Parameter param = params.get(i);
            AField field = method.parameters.vivify(i);
            visitType(param.getType(), field.type);
        }
    }
    if (rcvrParam.isPresent()) {
        for (AnnotationExpr expr : rcvrParam.get().getAnnotations()) {
            Annotation anno = extractAnnotation(expr);
            method.receiver.type.tlAnnotationsHere.add(anno);
        }
    }
    if (typeParams != null) {
        for (int i = 0; i < typeParams.size(); i++) {
            TypeParameter typeParam = typeParams.get(i);
            List<ClassOrInterfaceType> bounds = typeParam.getTypeBound();
            if (bounds != null) {
                for (int j = 0; j < bounds.size(); j++) {
                    ClassOrInterfaceType bound = bounds.get(j);
                    BoundLocation loc = new BoundLocation(i, j);
                    bound.accept(this, method.bounds.vivify(loc));
                }
            }
        }
    }
    return body == null ? null : body.accept(this, method);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Annotation(scenelib.annotations.Annotation) AField(scenelib.annotations.el.AField) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) AClass(scenelib.annotations.el.AClass) BoundLocation(scenelib.annotations.el.BoundLocation) AMethod(scenelib.annotations.el.AMethod)

Example 2 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterAsFirstWhenThereAreSome.

@Test
public void addingTypeParameterAsFirstWhenThereAreSome() throws IOException {
    ClassOrInterfaceDeclaration cid = consider("class A<U> {}");
    cid.getTypeParameters().addFirst(new TypeParameter("T", new NodeList<>()));
    assertTransformedToString("class A<T, U> {}", cid);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 3 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project javaparser by javaparser.

the class JavaParserTypeAdapter method solveType.

public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    if (wrappedNode instanceof NodeWithTypeParameters<?>) {
        NodeList<TypeParameter> typeParameters = ((NodeWithTypeParameters<?>) wrappedNode).getTypeParameters();
        for (com.github.javaparser.ast.type.TypeParameter typeParameter : typeParameters) {
            if (typeParameter.getName().getId().equals(name)) {
                return SymbolReference.solved(new JavaParserTypeVariableDeclaration(typeParameter, typeSolver));
            }
        }
    }
    // Internal classes
    for (BodyDeclaration<?> member : this.wrappedNode.getMembers()) {
        if (member instanceof com.github.javaparser.ast.body.TypeDeclaration) {
            com.github.javaparser.ast.body.TypeDeclaration<?> internalType = (com.github.javaparser.ast.body.TypeDeclaration<?>) member;
            String prefix = internalType.getName() + ".";
            if (internalType.getName().getId().equals(name)) {
                if (internalType instanceof ClassOrInterfaceDeclaration) {
                    return SymbolReference.solved(new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver));
                } else if (internalType instanceof EnumDeclaration) {
                    return SymbolReference.solved(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver));
                } else {
                    throw new UnsupportedOperationException();
                }
            } else if (name.startsWith(prefix) && name.length() > prefix.length()) {
                if (internalType instanceof ClassOrInterfaceDeclaration) {
                    return new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver).solveType(name.substring(prefix.length()), typeSolver);
                } else if (internalType instanceof EnumDeclaration) {
                    return new SymbolSolver(typeSolver).solveTypeInType(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver), name.substring(prefix.length()));
                } else {
                    throw new UnsupportedOperationException();
                }
            }
        }
    }
    return SymbolReference.unsolved(ResolvedTypeDeclaration.class);
}
Also used : NodeWithTypeParameters(com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters) TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeParameter(com.github.javaparser.ast.type.TypeParameter) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)

Example 4 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.

the class StubParser method annotateTypeParameters.

private void annotateTypeParameters(// for debugging
BodyDeclaration<?> decl, // for debugging; TypeElement or ExecutableElement
Object elt, Map<Element, AnnotatedTypeMirror> atypes, List<? extends AnnotatedTypeMirror> typeArguments, List<TypeParameter> typeParameters) {
    if (typeParameters == null) {
        return;
    }
    if (typeParameters.size() != typeArguments.size()) {
        String msg = String.format("annotateTypeParameters: mismatched sizes:  typeParameters (size %d)=%s;  typeArguments (size %d)=%s;  decl=%s;  elt=%s (%s).", typeParameters.size(), typeParameters, typeArguments.size(), typeArguments, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass());
        if (!debugStubParser) {
            msg = msg + "%n  For more details, run with -AstubDebug";
        }
        stubWarn(msg);
    }
    for (int i = 0; i < typeParameters.size(); ++i) {
        TypeParameter param = typeParameters.get(i);
        AnnotatedTypeVariable paramType = (AnnotatedTypeVariable) typeArguments.get(i);
        if (param.getTypeBound() == null || param.getTypeBound().isEmpty()) {
            // No bound so annotations are both lower and upper bounds
            annotate(paramType, param.getAnnotations());
        } else if (param.getTypeBound() != null && param.getTypeBound().size() > 0) {
            annotate(paramType.getLowerBound(), param.getAnnotations());
            annotate(paramType.getUpperBound(), param.getTypeBound().get(0), null);
            if (param.getTypeBound().size() > 1) {
                // TODO: add support for intersection types
                stubWarnNotFound("Annotations on intersection types are not yet supported");
            }
        }
        putNew(atypes, paramType.getUnderlyingType().asElement(), paramType);
    }
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 5 with TypeParameter

use of com.github.javaparser.ast.type.TypeParameter in project checker-framework by typetools.

the class StubParser method processType.

/**
 * @return list of AnnotatedTypeVariable of the type's type parameter declarations
 */
private List<AnnotatedTypeVariable> processType(ClassOrInterfaceDeclaration decl, TypeElement elt) {
    annotateDecl(declAnnos, elt, decl.getAnnotations());
    AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
    annotate(type, decl.getAnnotations());
    final List<? extends AnnotatedTypeMirror> typeArguments = type.getTypeArguments();
    final List<TypeParameter> typeParameters = decl.getTypeParameters();
    if (debugStubParser) {
        int numParams = (typeParameters == null ? 0 : typeParameters.size());
        int numArgs = (typeArguments == null ? 0 : typeArguments.size());
        if (numParams != numArgs) {
            stubDebug(String.format("parseType:  mismatched sizes for typeParameters=%s (size %d) and typeArguments=%s (size %d); decl=%s; elt=%s (%s); type=%s (%s); parseState=%s", typeParameters, numParams, typeArguments, numArgs, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass(), type, type.getClass(), parseState));
            stubDebug("Proceeding despite mismatched sizes");
        }
    }
    annotateTypeParameters(decl, elt, atypes, typeArguments, typeParameters);
    annotateSupertypes(decl, type);
    putNew(atypes, elt, type);
    List<AnnotatedTypeVariable> typeVariables = new ArrayList<>();
    for (AnnotatedTypeMirror typeV : type.getTypeArguments()) {
        if (typeV.getKind() != TypeKind.TYPEVAR) {
            stubWarn("expected an AnnotatedTypeVariable but found type kind " + typeV.getKind() + ": " + typeV);
        } else {
            typeVariables.add((AnnotatedTypeVariable) typeV);
        }
    }
    return typeVariables;
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

TypeParameter (com.github.javaparser.ast.type.TypeParameter)9 NodeList (com.github.javaparser.ast.NodeList)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 AbstractLexicalPreservingTest (com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)3 Test (org.junit.Test)3 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)2 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)2 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 NodeWithTypeArguments (com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments)1 NodeWithTypeParameters (com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ArrayType (com.github.javaparser.ast.type.ArrayType)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 ReferenceType (com.github.javaparser.ast.type.ReferenceType)1 Type (com.github.javaparser.ast.type.Type)1