Search in sources :

Example 46 with NodeList

use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.

the class LexicalDifferenceCalculator method calculatedSyntaxModelAfterListAddition.

// Visible for testing
CalculatedSyntaxModel calculatedSyntaxModelAfterListAddition(Node container, ObservableProperty observableProperty, int index, Node nodeAdded) {
    CsmElement csm = ConcreteSyntaxModel.forClass(container.getClass());
    Object rawValue = observableProperty.getRawValue(container);
    if (!(rawValue instanceof NodeList)) {
        throw new IllegalStateException("Expected NodeList, found " + rawValue.getClass().getCanonicalName());
    }
    NodeList nodeList = (NodeList) rawValue;
    return calculatedSyntaxModelAfterListAddition(csm, observableProperty, nodeList, index, nodeAdded);
}
Also used : NodeList(com.github.javaparser.ast.NodeList)

Example 47 with NodeList

use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.

the class YamlPrinter method output.

public void output(Node node, String name, int level, StringBuilder builder) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    if (outputNodeType)
        builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
    else
        builder.append(System.lineSeparator() + indent(level) + name + ": ");
    level++;
    for (PropertyMetaModel a : attributes) {
        builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" + a.getValue(node).toString() + "\"");
    }
    for (PropertyMetaModel sn : subNodes) {
        Node nd = (Node) sn.getValue(node);
        if (nd != null)
            output(nd, sn.getName(), level, builder);
    }
    for (PropertyMetaModel sl : subLists) {
        NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
        if (nl != null && nl.isNonEmpty()) {
            builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
            String slName = sl.getName();
            slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
            for (Node nd : nl) output(nd, "- " + slName, level + 1, builder);
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 48 with NodeList

use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.

the class AnonymousClassDeclarationContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    List<com.github.javaparser.ast.body.TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(com.github.javaparser.ast.body.TypeDeclaration.class);
    Optional<SymbolReference<ResolvedTypeDeclaration>> exactMatch = typeDeclarations.stream().filter(internalType -> internalType.getName().getId().equals(name)).findFirst().map(internalType -> SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)));
    if (exactMatch.isPresent()) {
        return exactMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> recursiveMatch = typeDeclarations.stream().filter(internalType -> name.startsWith(String.format("%s.", internalType.getName()))).findFirst().map(internalType -> JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1), typeSolver));
    if (recursiveMatch.isPresent()) {
        return recursiveMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> typeArgumentsMatch = wrappedNode.getTypeArguments().map(nodes -> ((NodeWithTypeArguments<?>) nodes).getTypeArguments().orElse(new NodeList<>())).orElse(new NodeList<>()).stream().filter(type -> type.toString().equals(name)).findFirst().map(matchingType -> SymbolReference.solved(new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
    if (typeArgumentsMatch.isPresent()) {
        return typeArgumentsMatch.get();
    }
    // Look into extended classes and implemented interfaces
    for (ResolvedReferenceType ancestor : myDeclaration.getAncestors()) {
        // look at names of extended classes and implemented interfaces (this may not be important because they are checked in CompilationUnitContext)
        if (ancestor.getTypeDeclaration().getName().equals(name)) {
            return SymbolReference.solved(ancestor.getTypeDeclaration());
        }
        // look into internal types of extended classes and implemented interfaces
        try {
            for (ResolvedTypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
                if (internalTypeDeclaration.getName().equals(name)) {
                    return SymbolReference.solved(internalTypeDeclaration);
                }
            }
        } catch (UnsupportedOperationException e) {
        // just continue using the next ancestor
        }
    }
    return getParent().solveType(name, typeSolver);
}
Also used : JavaParserFactory(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory) NodeList(com.github.javaparser.ast.NodeList) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeParameter(com.github.javaparser.ast.type.TypeParameter) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) NodeWithTypeArguments(com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) JavaParserAnonymousClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnonymousClassDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) TypeParameter(com.github.javaparser.ast.type.TypeParameter) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Example 49 with NodeList

use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.

the class GeneratedJavaParserBase method generateLambda.

/**
 * Workaround for rather complex ambiguity that lambda's create
 */
Expression generateLambda(Expression ret, Statement lambdaBody) {
    if (ret instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) ret).getInner();
        SimpleName id = ((NameExpr) inner).getName();
        NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), EnumSet.noneOf(Modifier.class), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
        ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, true);
    } else if (ret instanceof NameExpr) {
        SimpleName id = ((NameExpr) ret).getName();
        NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), EnumSet.noneOf(Modifier.class), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
        ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, false);
    } else if (ret instanceof LambdaExpr) {
        ((LambdaExpr) ret).setBody(lambdaBody);
        propagateRangeGrowthOnRight(ret, lambdaBody);
    } else if (ret instanceof CastExpr) {
        CastExpr castExpr = (CastExpr) ret;
        Expression inner = generateLambda(castExpr.getExpression(), lambdaBody);
        castExpr.setExpression(inner);
    } else {
        addProblem("Failed to parse lambda expression! Please create an issue at https://github.com/javaparser/javaparser/issues");
    }
    return ret;
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) NodeList(com.github.javaparser.ast.NodeList) Parameter(com.github.javaparser.ast.body.Parameter) Modifier(com.github.javaparser.ast.Modifier)

Example 50 with NodeList

use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.

the class CloneVisitorTest method cloneJavaDocTest.

@Test
public void cloneJavaDocTest() {
    NodeList<BodyDeclaration<?>> bodyDeclarationList = new NodeList<>();
    bodyDeclarationList.add(new AnnotationMemberDeclaration().setJavadocComment("javadoc"));
    bodyDeclarationList.add(new ConstructorDeclaration().setJavadocComment("javadoc"));
    bodyDeclarationList.add(new EnumConstantDeclaration().setJavadocComment("javadoc"));
    bodyDeclarationList.add(new FieldDeclaration().setJavadocComment("javadoc"));
    bodyDeclarationList.add(new InitializerDeclaration().setJavadocComment("javadoc"));
    bodyDeclarationList.add(new MethodDeclaration().setJavadocComment("javadoc"));
    NodeList<TypeDeclaration<?>> typeDeclarationList = new NodeList<>();
    AnnotationDeclaration annotationDeclaration = new AnnotationDeclaration();
    annotationDeclaration.setName("nnotationDeclarationTest");
    typeDeclarationList.add(annotationDeclaration.setJavadocComment("javadoc"));
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration2 = new ClassOrInterfaceDeclaration();
    classOrInterfaceDeclaration2.setName("emptyTypeDeclarationTest");
    typeDeclarationList.add(classOrInterfaceDeclaration2.setJavadocComment("javadoc"));
    EnumDeclaration enumDeclaration = new EnumDeclaration();
    enumDeclaration.setName("enumDeclarationTest");
    typeDeclarationList.add(enumDeclaration.setJavadocComment("javadoc"));
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration();
    classOrInterfaceDeclaration.setName("classOrInterfaceDeclarationTest");
    typeDeclarationList.add(classOrInterfaceDeclaration.setJavadocComment("javadoc"));
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration1 = new ClassOrInterfaceDeclaration();
    classOrInterfaceDeclaration1.setName("emptyTypeDeclarationTest1");
    typeDeclarationList.add(classOrInterfaceDeclaration2.setMembers(bodyDeclarationList));
    cu.setTypes(typeDeclarationList);
    CompilationUnit cuClone = (CompilationUnit) new CloneVisitor().visit(cu, null);
    NodeList<TypeDeclaration<?>> typeDeclarationListClone = cuClone.getTypes();
    Iterator<TypeDeclaration<?>> typeItr = typeDeclarationListClone.iterator();
    TypeDeclaration<?> typeDeclaration;
    while (typeItr.hasNext()) {
        typeDeclaration = typeItr.next();
        if (typeDeclaration.getMembers() == null) {
            assertEquals(typeDeclaration.getComment().get().getContent(), " javadoc");
        } else {
            Iterator<BodyDeclaration<?>> bodyItr = typeDeclaration.getMembers().iterator();
            while (bodyItr.hasNext()) {
                BodyDeclaration<?> bodyDeclaration = bodyItr.next();
                assertEquals(bodyDeclaration.getComment().get().getContent(), " javadoc");
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14