Search in sources :

Example 31 with MethodDeclaration

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

the class NodeWithParametersBuildersTest method testGetParamByName.

@Test
public void testGetParamByName() {
    MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC);
    Parameter addAndGetParameter = addMethod.addAndGetParameter(int.class, "yay");
    assertEquals(addAndGetParameter, addMethod.getParameterByName("yay").get());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) Test(org.junit.Test)

Example 32 with MethodDeclaration

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

the class NodeWithParametersBuildersTest method testGetParamByType.

@Test
public void testGetParamByType() {
    MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC);
    Parameter addAndGetParameter = addMethod.addAndGetParameter(int.class, "yay");
    assertEquals(addAndGetParameter, addMethod.getParameterByType("int").get());
    assertEquals(addAndGetParameter, addMethod.getParameterByType(int.class).get());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) Test(org.junit.Test)

Example 33 with MethodDeclaration

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

the class NodeWithThrownExceptionsBuildersTest method testThrows.

@Test
public void testThrows() {
    MethodDeclaration addMethod = cu.addClass("test").addMethod("foo", Modifier.PUBLIC);
    addMethod.addThrownException(IllegalStateException.class);
    assertEquals(1, addMethod.getThrownExceptions().size());
    assertEquals(true, addMethod.isThrown(IllegalStateException.class));
    addMethod.addThrownException(parseClassOrInterfaceType("Test"));
    assertEquals(2, addMethod.getThrownExceptions().size());
    assertEquals("Test", addMethod.getThrownException(1).toString());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 34 with MethodDeclaration

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

the class NodeWithJavadocTest method getJavadocOnMethodWithLineCommentShouldReturnEmptyOptional.

@Test
public void getJavadocOnMethodWithLineCommentShouldReturnEmptyOptional() {
    MethodDeclaration method = new MethodDeclaration();
    method.setLineComment("Lorem Ipsum.");
    assertFalse(method.getJavadocComment().isPresent());
    assertFalse(method.getJavadoc().isPresent());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 35 with MethodDeclaration

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

the class ArrayTypeTest method getParameterWithArrays.

@Test
public void getParameterWithArrays() {
    MethodDeclaration methodDeclaration = parseBodyDeclaration("void a(@C int @A[] a @B[]) {}").asMethodDeclaration();
    Parameter parameter = methodDeclaration.getParameter(0);
    ArrayType outerArrayType = parameter.getType().asArrayType();
    ArrayType innerArrayType = outerArrayType.getComponentType().asArrayType();
    PrimitiveType elementType = innerArrayType.getComponentType().asPrimitiveType();
    assertThat(elementType).isInstanceOf(PrimitiveType.class);
    assertThat(outerArrayType.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
    assertThat(innerArrayType.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
    assertThat(parameter.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
    assertThat(parameter.getType().getParentNode().get()).isSameAs(parameter);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.Test)

Aggregations

MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)325 Test (org.junit.Test)166 CompilationUnit (com.github.javaparser.ast.CompilationUnit)143 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)107 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)100 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)84 Expression (com.github.javaparser.ast.expr.Expression)82 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)77 NameExpr (com.github.javaparser.ast.expr.NameExpr)58 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)57 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)47 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)46 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)41 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)40 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)38 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)35 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)35 NodeList (com.github.javaparser.ast.NodeList)32 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)27