当前位置: 首页>>代码示例>>Java>>正文


Java JavaParser.parse方法代码示例

本文整理汇总了Java中com.github.javaparser.JavaParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java JavaParser.parse方法的具体用法?Java JavaParser.parse怎么用?Java JavaParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.github.javaparser.JavaParser的用法示例。


在下文中一共展示了JavaParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: analyze

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
public List<EntityField> analyze(File source) throws IOException, ParseException {
    List<EntityField> entityFields = new ArrayList<>();
    CompilationUnit cu = JavaParser.parse(source);
    cu.accept(new VoidVisitorAdapter<List<EntityField>>() {
        @Override
        public void visit(FieldDeclaration fd, List<EntityField> f) {
            if (fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Column"))) {
                Class<?> type = null;
                switch (fd.getType().toString()) {
                    case "String": type = String.class; break;
                    case "Long": type = Long.class; break;
                    case "Integer": type = Integer.class; break;
                    case "boolean": type = boolean.class; break;
                }
                if (type == null) return;
                f.add(new EntityField(
                        fd.getVariables().get(0).getId().getName(),
                        type,
                        fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Id"))));
            }
        }
    }, entityFields);

    return entityFields;
}
 
开发者ID:kawasima,项目名称:enkan,代码行数:26,代码来源:EntitySourceAnalyzer.java

示例2: brewJava

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
public static void brewJava(File rFile, File outputDir, String packageName, String className)
    throws Exception {
  CompilationUnit compilationUnit = JavaParser.parse(rFile);
  TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);

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

  for (Node node : resourceClass.getChildrenNodes()) {
    if (node instanceof TypeDeclaration) {
      addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (TypeDeclaration) node);
    }
  }

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

  finalR.writeTo(outputDir);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:21,代码来源:FinalRClassBuilder.java

示例3: setUp

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@BeforeEach
void setUp() throws IOException {
    String source = "package com.example; " +
            "import org.dataj.Data; " +
            "import javax.annotation.Nonnull;" +
            "import javax.annotation.Nullable;" +
            "@Data class NullableFields { @Nonnull String name; @Nullable Integer age; }";

    Compilation compilation = javac()
            .withProcessors(new AnnotationProcessor())
            .compile(
                    JavaFileObjects.forSourceString("NullableFields", source)
            );

    JavaFileObject outputFile = compilation.generatedSourceFile("com.example.NullableFieldsData").get();
    actualSource = JavaParser.parse(outputFile.openInputStream());
    referenceSource = JavaParser.parseResource("NullableFieldsData.java");
}
 
开发者ID:alek-sys,项目名称:dataj,代码行数:19,代码来源:NullableFieldsTest.java

示例4: createFile

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
protected void createFile(String withPackageName,
                          List<FieldInfo> withFields,
                          String withEntrySeparator,
                          String withValueDelimiterPrefix,
                          String withValueDelimiterSuffix,
                          String withKeyValueSeparator) throws Exception {

    String fileContents = victim.createALogFormatEnforcer(
            withPackageName,
            withFields,
            withEntrySeparator,
            withValueDelimiterPrefix,
            withValueDelimiterSuffix,
            withKeyValueSeparator
    );

    try (ByteArrayInputStream in = new ByteArrayInputStream(fileContents.getBytes())) {
        compilationUnit = JavaParser.parse(in);
    }
}
 
开发者ID:leandronunes85,项目名称:log-format-enforcer,代码行数:21,代码来源:AbstractTest.java

示例5: resolvingReferenceToEnumDeclarationInSameFile

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void resolvingReferenceToEnumDeclarationInSameFile() throws FileNotFoundException {
    String code = "package foo.bar;\nenum Foo {\n" +
            "    FOO_A, FOO_B\n" +
            "}\n" +
            "\n" +
            "class UsingFoo {\n" +
            "    Foo myFooField;\n" +
            "}";
    CompilationUnit cu = JavaParser.parse(code);
    FieldDeclaration fieldDeclaration = Navigator.findNodeOfGivenClass(cu, FieldDeclaration.class);
    ResolvedType fieldType = javaParserFacade.getType(fieldDeclaration);
    assertEquals(true, fieldType.isReferenceType());
    assertEquals(true, fieldType.asReferenceType().getTypeDeclaration().isEnum());
    assertEquals("foo.bar.Foo", fieldType.asReferenceType().getQualifiedName());
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:17,代码来源:Issue347.java

示例6: testHasDirectlyAnnotation

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void testHasDirectlyAnnotation() throws IOException, ParseException {
    TypeSolver typeSolver = new ReflectionTypeSolver();

    CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt")));

    JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver);
    assertEquals(true, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation"));
    assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, ca.hasDirectlyAnnotation("MyAnnotation"));
    assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation"));

    JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);
    assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation"));
    assertEquals(true, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, cb.hasDirectlyAnnotation("MyAnnotation"));
    assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation"));
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:19,代码来源:JavaParserClassDeclarationTest.java

示例7: testHasAnnotation

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void testHasAnnotation() throws IOException, ParseException {
    TypeSolver typeSolver = new ReflectionTypeSolver();

    CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt")));

    JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver);
    assertEquals(true, ca.hasAnnotation("foo.bar.MyAnnotation"));
    assertEquals(false, ca.hasAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, ca.hasAnnotation("MyAnnotation"));
    assertEquals(false, ca.hasAnnotation("foo.bar.MyUnexistingAnnotation"));

    JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);
    assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation"));
    assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, cb.hasAnnotation("MyAnnotation"));
    assertEquals(false, cb.hasAnnotation("foo.bar.MyUnexistingAnnotation"));
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:19,代码来源:JavaParserClassDeclarationTest.java

示例8: testSolveStaticallyImportedMemberType

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void testSolveStaticallyImportedMemberType() throws FileNotFoundException {
    CompilationUnit cu = JavaParser.parse(new File(adaptPath("src/test/resources/issue276/foo/C.java")));
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "C");
    TypeSolver typeSolver = new CombinedTypeSolver(
    		new ReflectionTypeSolver(), 
    		new JavaParserTypeSolver(adaptPath(new File("src/test/resources/issue276"))));
    List<MethodDeclaration> methods = Navigator.findAllNodesOfGivenClass(cls, MethodDeclaration.class);
    boolean isSolved = false;
    for (MethodDeclaration method: methods) {
    	if (method.getNameAsString().equals("overrideMe")) {
    		MethodContext context = new MethodContext(method, typeSolver);
    		isSolved = context.solveType("FindMeIfYouCan", typeSolver).isSolved();
    	}
    }
    Assert.assertTrue(isSolved);
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:18,代码来源:Issue276.java

示例9: resolveReferenceToFieldInheritedByInterface

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void resolveReferenceToFieldInheritedByInterface() throws FileNotFoundException {
    String code = "package foo.bar;\n"+
            "interface  A {\n" +
            "        int a = 0;\n" +
            "    }\n" +
            "    \n" +
            "    class B implements A {\n" +
            "        int getA() {\n" +
            "            return a;\n" +
            "        }\n" +
            "    }";
    CompilationUnit cu = JavaParser.parse(code);
    NameExpr refToA = Navigator.findNameExpression(Navigator.demandClass(cu, "B"), "a");
    SymbolReference<? extends ResolvedValueDeclaration> symbolReference = javaParserFacade.solve(refToA);
    assertEquals(true, symbolReference.isSolved());
    assertEquals(true, symbolReference.getCorrespondingDeclaration().isField());
    assertEquals("a", symbolReference.getCorrespondingDeclaration().getName());
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:20,代码来源:Issue314.java

示例10: testGetAllGenericFields

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void testGetAllGenericFields() throws IOException, ParseException {
    TypeSolver typeSolver = new ReflectionTypeSolver();

    CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/GenericFields.java.txt")));
    JavaParserClassDeclaration classDeclaration = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);

    assertEquals(3, classDeclaration.getAllFields().size());

    ReferenceTypeImpl rtClassDeclaration = new ReferenceTypeImpl(classDeclaration, typeSolver);

    assertEquals("s", classDeclaration.getAllFields().get(0).getName());
    assertEquals(string, classDeclaration.getAllFields().get(0).getType());
    assertEquals(string, rtClassDeclaration.getFieldType("s").get());

    assertEquals("t", classDeclaration.getAllFields().get(1).getName());
    assertEquals("java.util.List<java.lang.Boolean>", classDeclaration.getAllFields().get(1).getType().describe());
    assertEquals(listOfBoolean, rtClassDeclaration.getFieldType("t").get());

    assertEquals("i", classDeclaration.getAllFields().get(2).getName());
    assertEquals(ResolvedPrimitiveType.INT, classDeclaration.getAllFields().get(2).getType());
    assertEquals(ResolvedPrimitiveType.INT, rtClassDeclaration.getFieldType("i").get());
}
 
开发者ID:javaparser,项目名称:javasymbolsolver,代码行数:24,代码来源:JavaParserClassDeclarationTest.java

示例11: changeTestMethodName

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
/**
 * Change test annotation groups.
 *
 * @param caseRunnerRelativeFilePathName
 *            the case runner relative file path name
 * @param groups
 *            the groups
 * @param testMethodName
 *            the test method name
 * @throws ParseException
 *             the parse exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void changeTestMethodName(String caseRunnerRelativeFilePathName,
		String testMethodName) throws ParseException, IOException {
	// creates an input stream for the file to be parsed
	FileInputStream fTemp = new FileInputStream(
			caseRunnerRelativeFilePathName);
	CompilationUnit caseRunnerCU;
	try {
		// parse the file
		caseRunnerCU = JavaParser.parse(fTemp);
	} finally {
		fTemp.close();

	}
	// visit and print the methods names
	new MethodNameVisitor().visit(caseRunnerCU, testMethodName);
	String newClass = caseRunnerCU.toString();
	fTemp.close();
	// prints the changed compilation unit
	FileWriter out = new FileWriter(caseRunnerRelativeFilePathName, false);
	out.write(newClass);
	out.close();
}
 
开发者ID:bigtester,项目名称:automation-test-engine,代码行数:37,代码来源:CaseRunnerGenerator.java

示例12: changeTestAnnotationGroups

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
/**
 * Change test annotation groups.
 *
 * @param caseRunnerAbsoluteFilePathName
 *            the case runner relative file path name
 * @param groups
 *            the groups
 * @param testMethodName
 *            the test method name
 * @throws ParseException
 *             the parse exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void changeTestAnnotationGroups(
		String caseRunnerAbsoluteFilePathName, List<String> groups,
		String testMethodName) throws ParseException, IOException {
	// creates an input stream for the file to be parsed
	FileInputStream fTemp = new FileInputStream(
			caseRunnerAbsoluteFilePathName);
	CompilationUnit caseRunnerCU;
	try {
		// parse the file
		caseRunnerCU = JavaParser.parse(fTemp);
	} finally {
		fTemp.close();

	}

	groups.add(0, testMethodName);
	new MethodGroupAnnotationVisitor().visit(caseRunnerCU, groups);
	String newClass = caseRunnerCU.toString();
	fTemp.close();
	// prints the changed compilation unit
	FileWriter out = new FileWriter(caseRunnerAbsoluteFilePathName, false);
	out.write(newClass);
	out.close();
}
 
开发者ID:bigtester,项目名称:automation-test-engine,代码行数:39,代码来源:CaseRunnerGenerator.java

示例13: testReferencingConstant

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@Test
public void testReferencingConstant() throws IOException, ParseException {
    File file = new File("src/test/java/controller/ControllerReferencingConstant.java");
    final CompilationUnit declaration = JavaParser.parse(file);
    ControllerModel model = new ControllerModel();
    visitor.visit(declaration, model);

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

    for (ControllerRouteModel route : routes) {
        assertThat(route.getHttpMethod().name()).isEqualToIgnoringCase(route.getMethodName());
        assertThat(route.getParams()).isEmpty();
        assertThat(route.getPath()).isEqualToIgnoringCase("/constant");
    }
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:18,代码来源:ControllerSourceVisitorTest.java

示例14: testHttpVerbs

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
@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");
    }
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:18,代码来源:ControllerSourceVisitorTest.java

示例15: brewJava

import com.github.javaparser.JavaParser; //导入方法依赖的package包/类
public static void brewJava(File rFile, File outputDir, String packageName, String className)
    throws Exception {
  CompilationUnit compilationUnit = JavaParser.parse(rFile);
  TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);

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

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

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

  finalR.writeTo(outputDir);
}
 
开发者ID:JakeWharton,项目名称:butterknife,代码行数:21,代码来源:FinalRClassBuilder.java


注:本文中的com.github.javaparser.JavaParser.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。