Java Code Examples for com.github.javaparser.JavaParser#parse()

The following examples show how to use com.github.javaparser.JavaParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxContraints() throws IOException, ParseException{
    File file = new File("src/test/java/controller/ControllerWithConstraints.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    ControllerRouteModel route = getModelByPath(model,"/rahan");
    assertThat(route).isNotNull();
    assertThat(route.getParams()).hasSize(1);
    RouteParamModel param = (RouteParamModel) Iterables.get(route.getParams(), 0);

    //Annotated with Max constraint
    assertThat(param.getName()).isEqualTo("son");
    assertThat(param.getMax()).isEqualTo(2010);

    route = getModelByPath(model,"/crao");
    assertThat(route).isNotNull();
    assertThat(route.getParams()).hasSize(1);
    param = (RouteParamModel) Iterables.get(route.getParams(), 0);

    //Annotated with Max constraints that contains a message
    assertThat(param.getName()).isEqualTo("father");
    assertThat(param.getMax()).isEqualTo(2010);
}
 
Example 2
Source File: FinalRClassBuilder.java    From Briefness with Apache License 2.0 6 votes vote down vote up
public static void brewJava(File rFile, File outputDir, String packageName, String className, boolean useLegacyTypes) throws Exception {
  CompilationUnit compilationUnit = JavaParser.parse(rFile);
  TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);

  TypeSpec.Builder result = TypeSpec.classBuilder(className)
      .addModifiers(PUBLIC, FINAL);

  for (Node node : resourceClass.getChildNodes()) {
    if (node instanceof ClassOrInterfaceDeclaration) {
      addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node, useLegacyTypes);
    }
  }

  JavaFile finalR = JavaFile.builder(packageName, result.build())
      .addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!")
      .build();

  finalR.writeTo(outputDir);
}
 
Example 3
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testControllerUsingPath() throws IOException, ParseException {
    File file = new File("src/test/java/controller/ControllerWithPath.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    System.out.println(model.getRoutesAsMultiMap());
    ControllerRouteModel route = getModelByFullPath(model, "/root/simple");
    assertThat(route).isNotNull();

    route = getModelByFullPath(model, "/root/");
    assertThat(route).isNotNull();

    route = getModelByFullPath(model, "/root");
    assertThat(route).isNotNull();

    route = getModelByFullPath(model, "/rootstuff");
    assertThat(route).isNotNull();

}
 
Example 4
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotNullContraints() throws IOException, ParseException{
    File file = new File("src/test/java/controller/ControllerWithConstraints.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    ControllerRouteModel route = getModelByPath(model,"/superman");
    assertThat(route).isNotNull();
    assertThat(route.getParams()).hasSize(1);
    RouteParamModel param = (RouteParamModel) Iterables.get(route.getParams(), 0);

    //Annotated with NotNull constraints
    assertThat(param.getName()).isEqualTo("clark");
    assertThat(param.isMandatory()).isTrue();

    route = getModelByPath(model,"/batman");
    assertThat(route).isNotNull();
    assertThat(route.getParams()).hasSize(1);
    param = (RouteParamModel) Iterables.get(route.getParams(), 0);

    //Annotated with NotNull constraints that contains a message
    assertThat(param.getName()).isEqualTo("bruce");
    assertThat(param.isMandatory()).isTrue();
}
 
Example 5
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpVerbs() throws IOException, ParseException {
    File file = new File("src/test/java/controller/SimpleController.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    final Collection<ControllerRouteModel> routes = (Collection<ControllerRouteModel>) model.getRoutes().get("/simple");
    assertThat(routes).isNotNull();
    assertThat(routes).hasSize(6);

    for (ControllerRouteModel route : routes) {
        assertThat(route.getHttpMethod().name()).isEqualToIgnoringCase(route.getMethodName());
        assertThat(route.getParams()).isEmpty();
        assertThat(route.getPath()).isEqualToIgnoringCase("/simple");
    }
}
 
Example 6
Source File: ClassSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnAnnotation() throws IOException, ParseException {
    File file = new File("src/test/java/sample/MyAnnotation.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    final Boolean isController = visitor.visit(declaration, null);
    assertThat(isController).isFalse();
}
 
Example 7
Source File: ClassSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassExtendingDefaultController() throws IOException, ParseException {
    File file = new File("src/test/java/sample/MyDefaultController.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    final Boolean isController = visitor.visit(declaration, null);
    assertThat(isController).isTrue();
}
 
Example 8
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileItem() throws IOException, ParseException {
    File file = new File("src/test/java/controller/ParameterizedController.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    ControllerRouteModel route = getModelByPath(model, "/params/5");
    assertThat(route.getParams()).hasSize(1);
    final RouteParamModel o = (RouteParamModel) Iterables.get(route.getParams(), 0);
    assertThat(o.getParamType()).isEqualTo(RouteParamModel.ParamType.FORM);
    assertThat(o.getName()).isEqualTo("file");
    assertThat(o.getValueType()).isEqualTo(FileItem.class.getSimpleName());
}
 
Example 9
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyController() throws IOException, ParseException {
    File file = new File("src/test/java/controller/EmptyController.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);
    assertThat(model.getRoutes()).isEmpty();
}
 
Example 10
Source File: Sources.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public CompilationUnit apply(InputStream is) {
    try {
        return JavaParser.parse(is);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to parse stream.", ex);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 11
Source File: Sources.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public CompilationUnit apply(String resource) {
    InputStream is = null;
    try {
        is = getClass().getClassLoader().getResourceAsStream(resource);
        return JavaParser.parse(is);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to load resource: [" + resource + "] from classpath.");
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 12
Source File: JavaDocAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 5 votes vote down vote up
private static void parseJavaDoc(Path path, JavaDocParserVisitor visitor) {
    try {
        CompilationUnit cu = JavaParser.parse(path.toFile());
        cu.accept(visitor, null);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 13
Source File: ControllerSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testBody() throws IOException, ParseException {
    File file = new File("src/test/java/controller/ParameterizedController.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

    ControllerRouteModel route = getModelByPath(model, "/params/4");
    assertThat(route.getParams()).hasSize(1);
    final RouteParamModel o = (RouteParamModel) Iterables.get(route.getParams(), 0);
    assertThat(o.getParamType()).isEqualTo(RouteParamModel.ParamType.BODY);
    assertThat(o.getName()).isEqualTo("body"); // Special value.
    assertThat(o.getValueType()).isEqualTo(String.class.getSimpleName());
}
 
Example 14
Source File: ClassSourceVisitorTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testABasicClass() throws IOException, ParseException {
    File file = new File("src/test/java/sample/MyClass.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    final Boolean isController = visitor.visit(declaration, null);
    assertThat(isController).isFalse();
}
 
Example 15
Source File: SourceCodeProcessHandler.java    From molicode with Apache License 2.0 5 votes vote down vote up
@Override
public void doHandle(MoliCodeContext context) {
    String content = context.getDataString(MoliCodeConstant.CTX_KEY_SRC_CONTENT);
    if (StringUtils.isEmpty(content)) {
        return;
    }

    JavaSourceCodeDto sourceCodeDto = new JavaSourceCodeDto();
    CompilationUnit compilationUnit = JavaParser.parse(content);
    compilationUnit.accept(new JavaSourceCodeVisitor(sourceCodeDto), null);
    context.put(MoliCodeConstant.CTX_KEY_DEF_DATA, sourceCodeDto);
}
 
Example 16
Source File: AstTest.java    From dolphin with Apache License 2.0 4 votes vote down vote up
protected CompilationUnit generateAst(String source) throws ParseException {
  return JavaParser.parse(new StringReader(source), true);
}
 
Example 17
Source File: JavaSourceUtils.java    From dolphin with Apache License 2.0 4 votes vote down vote up
public static CompilationUnit generateAst(String source) throws ParseException {
    return JavaParser.parse(new StringReader(source), true);
}
 
Example 18
Source File: ClassSourceVisitorTest.java    From wisdom with Apache License 2.0 4 votes vote down vote up
@Test(expected = IOException.class)
public void testANotExistingClass() throws IOException, ParseException {
    File file = new File("src/test/java/sample/missing.java");
    JavaParser.parse(file);
}
 
Example 19
Source File: Parse.java    From genDoc with Apache License 2.0 4 votes vote down vote up
public static Class parse(InputStream inputStream, GenConfig genConfig){
    CompilationUnit cu = JavaParser.parse(inputStream);
    return innerParse(cu, genConfig);
}
 
Example 20
Source File: ClassCodeAnalyser.java    From CodeDefenders with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Iterates through a java class code to extract following information in a {@link CodeAnalysisResult}:
 * <ul>
 * <li>Strings of imports</li>
 * <li>Lines of compile time constants</li>
 * <li>Lines of non coverable code</li>
 * <li>Lines of not initialized fields</li>
 * <li>{@link Range Ranges} of methods signatures</li>
 * <li>{@link Range Ranges} of methods</li>
 * <li>{@link Range Ranges} if-condition statements and their bracket pair</li>
 * </ul>
 *
 * @param className The name of the visited class.
 * @param sourceCode the source code of the visited class.
 * @return a result, may be empty, but never {@code null}.
 */
public static CodeAnalysisResult visitCode(String className, String sourceCode) {
    final CodeAnalysisResult result = new CodeAnalysisResult();
    try {
        final CompilationUnit cu = JavaParser.parse(sourceCode);
        resultVisitor.visit(cu, result);
    } catch (ParseProblemException e) {
        logger.warn("Failed to parse {}. Aborting code visit.", className);
    }
    return result;
}