Search in sources :

Example 31 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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.getVivify(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.getVivify(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.getVivify(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 32 with ClassOrInterfaceType

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(ObjectCreationExpr expr, AElement elem) {
    ClassOrInterfaceType type = expr.getType();
    AClass clazz = scene.classes.getVivify(type.getNameAsString());
    Expression scope = expr.getScope().orElse(null);
    List<Type> typeArgs = expr.getTypeArguments().orElse(null);
    List<Expression> args = expr.getArguments();
    NodeList<BodyDeclaration<?>> bodyDecls = expr.getAnonymousClassBody().orElse(null);
    if (scope != null) {
        scope.accept(this, elem);
    }
    if (args != null) {
        for (Expression arg : args) {
            arg.accept(this, elem);
        }
    }
    if (typeArgs != null) {
        for (Type typeArg : typeArgs) {
            typeArg.accept(this, elem);
        }
    }
    type.accept(this, clazz);
    if (bodyDecls != null) {
        for (BodyDeclaration<?> decl : bodyDecls) {
            decl.accept(this, clazz);
        }
    }
    return null;
}
Also used : 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) Expression(com.github.javaparser.ast.expr.Expression) AClass(scenelib.annotations.el.AClass) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 33 with ClassOrInterfaceType

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

the class WholeProgramInferenceJavaParserStorage method addExplicitReceiver.

/**
 * Adds an explicit receiver type to a JavaParser method declaration.
 *
 * @param methodDeclaration declaration to add a receiver to
 */
private static void addExplicitReceiver(MethodDeclaration methodDeclaration) {
    if (methodDeclaration.getReceiverParameter().isPresent()) {
        return;
    }
    com.github.javaparser.ast.Node parent = methodDeclaration.getParentNode().get();
    if (!(parent instanceof TypeDeclaration)) {
        return;
    }
    TypeDeclaration<?> parentDecl = (TypeDeclaration<?>) parent;
    ClassOrInterfaceType receiver = new ClassOrInterfaceType();
    receiver.setName(parentDecl.getName());
    if (parentDecl.isClassOrInterfaceDeclaration()) {
        ClassOrInterfaceDeclaration parentClassDecl = parentDecl.asClassOrInterfaceDeclaration();
        if (!parentClassDecl.getTypeParameters().isEmpty()) {
            NodeList<Type> typeArgs = new NodeList<>();
            for (TypeParameter typeParam : parentClassDecl.getTypeParameters()) {
                ClassOrInterfaceType typeArg = new ClassOrInterfaceType();
                typeArg.setName(typeParam.getNameAsString());
                typeArgs.add(typeArg);
            }
            receiver.setTypeArguments(typeArgs);
        }
    }
    methodDeclaration.setReceiverParameter(new ReceiverParameter(receiver, "this"));
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) Type(com.github.javaparser.ast.type.Type) TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) NodeList(com.github.javaparser.ast.NodeList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 34 with ClassOrInterfaceType

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

the class JointJavacJavaParserVisitor method visitParameterizedType.

@Override
public Void visitParameterizedType(ParameterizedTypeTree javacTree, Node javaParserNode) {
    ClassOrInterfaceType node = castNode(ClassOrInterfaceType.class, javaParserNode, javacTree);
    processParameterizedType(javacTree, node);
    javacTree.getType().accept(this, node);
    // TODO: In a parameterized type, will the first branch ever run?
    if (javacTree.getTypeArguments().isEmpty()) {
        assert !node.getTypeArguments().isPresent() || node.getTypeArguments().get().isEmpty();
    } else {
        assert node.getTypeArguments().isPresent();
        visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
    }
    return null;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 35 with ClassOrInterfaceType

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

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final ClassOrInterfaceType node1, final Node other) {
    ClassOrInterfaceType node2 = (ClassOrInterfaceType) other;
    defaultAction(node1, node2);
    node1.getName().accept(this, node2.getName());
    node1.getScope().ifPresent(l -> l.accept(this, node2.getScope().get()));
    node1.getTypeArguments().ifPresent(l -> visitLists(l, node2.getTypeArguments().get()));
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Aggregations

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7