Search in sources :

Example 71 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class NodeTest method removeOrphanCommentNegativeCase.

@Test
public void removeOrphanCommentNegativeCase() {
    ClassOrInterfaceDeclaration aClass = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "A");
    FieldDeclaration aField = new FieldDeclaration(EnumSet.noneOf(Modifier.class), new VariableDeclarator(PrimitiveType.intType(), "f"));
    aClass.getMembers().add(aField);
    Comment c = new LineComment("A comment");
    aField.addOrphanComment(c);
    // the comment is an orphan comment of the field, so trying to remove it on the class should not work
    assertFalse(aClass.removeOrphanComment(c));
    assertEquals(1, aField.getOrphanComments().size());
    assertTrue(c.getParentNode().isPresent());
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) BlockComment(com.github.javaparser.ast.comments.BlockComment) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 72 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class NodeTest method cantFindCompilationUnit.

@Test
public void cantFindCompilationUnit() {
    VariableDeclarator x = new VariableDeclarator();
    assertFalse(x.findCompilationUnit().isPresent());
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 73 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class GenericsResolutionTest method resolveFieldWithGenericTypeToInteger.

@Test
public void resolveFieldWithGenericTypeToInteger() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "i");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("i", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("i", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("java.lang.Integer", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 74 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class GenericsResolutionTest method resolveElementOfList.

@Test
public void resolveElementOfList() {
    CompilationUnit cu = parseSample("ElementOfList");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ElementOfList");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    VariableDeclarator variableDeclarator = Navigator.demandVariableDeclaration(method, "a").get();
    Expression expression = variableDeclarator.getInitializer().get();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
    assertEquals(false, type.isTypeVariable());
    assertEquals("Comment", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 75 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class GenericsResolutionTest method resolveFieldWithGenericTypeToDeclaredClass.

@Test
public void resolveFieldWithGenericTypeToDeclaredClass() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    VariableDeclarator fieldS = Navigator.demandField(clazz, "g");
    SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
    Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("g", fieldS);
    assertEquals(true, symbolReference.isPresent());
    assertEquals("g", symbolReference.get().getName());
    ResolvedType type = symbolReference.get().getType();
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals("me.tomassetti.symbolsolver.javaparser.Generics", type.asReferenceType().typeParametersValues().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9