Search in sources :

Example 1 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project javaparser by javaparser.

the class Issue1364 method setup.

@Before
public void setup() {
    ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
    fakeObject.setName(new SimpleName("java.lang.Object"));
    TypeSolver typeSolver = new TypeSolver() {

        @Override
        public TypeSolver getParent() {
            return null;
        }

        @Override
        public void setParent(TypeSolver parent) {
        }

        @Override
        public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
            if ("java.lang.Object".equals(name)) {
                // custom handling
                return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
            }
            return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
        }
    };
    ParserConfiguration config = new ParserConfiguration();
    config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
    javaParser = new JavaParser(config);
}
Also used : JavaParser(com.github.javaparser.JavaParser) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) SimpleName(com.github.javaparser.ast.expr.SimpleName) ParserConfiguration(com.github.javaparser.ParserConfiguration) Before(org.junit.Before)

Example 2 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project javaparser by javaparser.

the class NodeListTest method replaceAll.

@Test
public void replaceAll() {
    List<String> changes = new LinkedList<>();
    String code = "class A { int a; int b; int c; }";
    CompilationUnit cu = JavaParser.parse(code);
    ClassOrInterfaceDeclaration cd = cu.getClassByName("A").get();
    cd.getMembers().register(createObserver(changes));
    cd.getMembers().replaceAll(bodyDeclaration -> {
        FieldDeclaration clone = (FieldDeclaration) bodyDeclaration.clone();
        SimpleName id = clone.getVariable(0).getName();
        id.setIdentifier(id.getIdentifier().toUpperCase());
        return clone;
    });
    assertEquals(Arrays.asList("'int a;' REMOVAL in list at 0", "'int A;' ADDITION in list at 0", "'int b;' REMOVAL in list at 1", "'int B;' ADDITION in list at 1", "'int c;' REMOVAL in list at 2", "'int C;' ADDITION in list at 2"), changes);
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) LinkedList(java.util.LinkedList) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) Test(org.junit.Test)

Example 3 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project javaparser by javaparser.

the class AnnotationMemberDeclarationTest method removeDefaultValueWhenDefaultValueIsPresent.

@Test
public void removeDefaultValueWhenDefaultValueIsPresent() {
    AnnotationMemberDeclaration decl = new AnnotationMemberDeclaration();
    SimpleName name = new SimpleName("foo");
    decl.setName(name);
    Expression defaultValue = new IntegerLiteralExpr("2");
    decl.setDefaultValue(defaultValue);
    decl.removeDefaultValue();
    assertFalse(defaultValue.getParentNode().isPresent());
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 4 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project javaparser by javaparser.

the class AnnotationMemberDeclarationTest method removeDefaultValueWhenNoDefaultValueIsPresent.

@Test
public void removeDefaultValueWhenNoDefaultValueIsPresent() {
    AnnotationMemberDeclaration decl = new AnnotationMemberDeclaration();
    SimpleName name = new SimpleName("foo");
    decl.setName(name);
    decl.removeDefaultValue();
    assertFalse(decl.getDefaultValue().isPresent());
}
Also used : SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 5 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project meghanada-server by mopemope.

the class ParameterNameVisitor method visit.

@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
    super.visit(n, arg);
    final EnumSet<Modifier> modifiers = n.getModifiers();
    if (!modifiers.contains(Modifier.PRIVATE)) {
        final List<BodyDeclaration<?>> members = n.getMembers();
        final SimpleName simpleName = n.getName();
        final String clazz = simpleName.getId();
        // String clazz = n.getName();
        this.className = this.pkg + '.' + clazz;
        log.debug("class {}", this.className);
        int i = 0;
        for (final BodyDeclaration<?> body : members) {
            if (body instanceof MethodDeclaration) {
                MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                this.getParameterNames(methodDeclaration, n.isInterface());
                i++;
            } else if (body instanceof ConstructorDeclaration) {
            // Constructor
            } else if (body instanceof ClassOrInterfaceDeclaration) {
                final ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) body;
                String name = classOrInterfaceDeclaration.getName().getIdentifier();
                String key = this.pkg + '.' + name;
                name = this.originClassName + '.' + name;
                for (MethodParameterNames mpn : this.parameterNamesList) {
                    if (mpn != null && mpn.className != null && mpn.className.equals(key)) {
                        mpn.className = name;
                    }
                }
            }
        }
        if (i > 0 && this.names.className != null) {
            this.parameterNamesList.add(this.names);
            this.names = new MethodParameterNames();
        }
    }
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Modifier(com.github.javaparser.ast.Modifier)

Aggregations

SimpleName (com.github.javaparser.ast.expr.SimpleName)34 Test (org.junit.Test)17 Expression (com.github.javaparser.ast.expr.Expression)15 NameExpr (com.github.javaparser.ast.expr.NameExpr)11 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)8 Parameter (com.github.javaparser.ast.body.Parameter)7 NodeList (com.github.javaparser.ast.NodeList)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)6 Node (com.github.javaparser.ast.Node)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)5 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)5 HashMap (java.util.HashMap)5 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4