本文整理了Java中com.github.javaparser.JavaParser.parse()
方法的一些代码示例,展示了JavaParser.parse()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JavaParser.parse()
方法的具体详情如下:
包路径:com.github.javaparser.JavaParser
类名称:JavaParser
方法名:parse
暂无
代码示例来源:origin: JCTools/JCTools
public static void main(String[] args) throws Exception {
for (String file : args) {
System.out.println("Opening " + file);
CompilationUnit cu = JavaParser.parse(new File(file));
new TraceVisitor(System.out, false).visit(cu, null);
System.out.println();
System.out.println();
System.out.println();
}
}
代码示例来源:origin: libgdx/libgdx
@Override
public ArrayList<JavaSegment> parse (String classFile) throws Exception {
CompilationUnit unit = JavaParser.parse(new ByteArrayInputStream(classFile.getBytes()));
ArrayList<JavaMethod> methods = new ArrayList<JavaMethod>();
getJavaMethods(methods, getOuterClass(unit));
ArrayList<JniSection> methodBodies = getNativeCodeBodies(classFile);
ArrayList<JniSection> sections = getJniSections(classFile);
alignMethodBodies(methods, methodBodies);
ArrayList<JavaSegment> segments = sortMethodsAndSections(methods, sections);
return segments;
}
代码示例来源:origin: libgdx/libgdx
@Override
public ArrayList<JavaSegment> parse (String classFile) throws Exception {
CompilationUnit unit = JavaParser.parse(new ByteArrayInputStream(classFile.getBytes()));
ArrayList<JavaMethod> methods = new ArrayList<JavaMethod>();
getJavaMethods(methods, getOuterClass(unit));
ArrayList<JniSection> methodBodies = getNativeCodeBodies(classFile);
ArrayList<JniSection> sections = getJniSections(classFile);
alignMethodBodies(methods, methodBodies);
ArrayList<JavaSegment> segments = sortMethodsAndSections(methods, sections);
return segments;
}
代码示例来源:origin: JCTools/JCTools
public static void main(String[] args) throws Exception {
if (args.length < 2) {
throw new IllegalArgumentException("Usage: outputDirectory inputSourceFiles");
}
File outputDirectory = new File(args[0]);
for (int i = 1; i < args.length; i++) {
File file = new File(args[i]);
System.out.println("Processing " + file);
CompilationUnit cu = JavaParser.parse(file);
new JavaParsingAtomicArrayQueueGenerator(file.getName()).visit(cu, null);
organiseImports(cu);
FileWriter writer = null;
String outputFileName = file.getName();
if (outputFileName.endsWith(".java")) {
outputFileName = translateQueueName(outputFileName.replace(".java", ""));
} else {
outputFileName = translateQueueName(outputFileName);
}
outputFileName += ".java";
try {
writer = new FileWriter(new File(outputDirectory, outputFileName));
writer.write(cu.toString());
} finally {
if (writer != null) {
writer.close();
}
}
}
}
代码示例来源:origin: JCTools/JCTools
File file = new File(args[i]);
System.out.println("Processing " + file);
CompilationUnit cu = JavaParser.parse(file);
new JavaParsingAtomicLinkedQueueGenerator(file.getName()).visit(cu, null);
代码示例来源:origin: hs-web/hsweb-framework
File oldClassFile = new File(file);
if (oldClassFile.exists()) {
CompilationUnit old = JavaParser.parse(oldClassFile);
CompilationUnit newClazz = JavaParser.parse(code);
Map<String, FieldDeclaration> oldFields = old
.findAll(FieldDeclaration.class)
代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".java")) {
if (printFileName) {
out.println("- parsing " + file.toAbsolutePath());
}
CompilationUnit cu = JavaParser.parse(file);
solveMethodCalls(cu);
}
return FileVisitResult.CONTINUE;
}
});
代码示例来源:origin: sdaschner/jaxrs-analyzer
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);
}
}
代码示例来源:origin: com.sebastian-daschner/jaxrs-analyzer
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);
}
}
代码示例来源:origin: com.infotel.seleniumRobot/core
private static void parseWebPage(Path path) throws FileNotFoundException {
javadoc.append(String.format("\nh2. Page: %s\n", path.getFileName().toString()));
FileInputStream in = new FileInputStream(path.toAbsolutePath().toString());
// parse the file
CompilationUnit cu = JavaParser.parse(in);
// prints the resulting compilation unit to default system output
cu.accept(new ClassVisitor(), "Pages");
cu.accept(new WebPageMethodVisitor(), null);
}
代码示例来源:origin: javaparser/javasymbolsolver
private CompilationUnit parse(String fileName) throws IOException, ParseException {
File sourceFile = new File(src.getAbsolutePath() + "/" + fileName + ".java");
CompilationUnit cu = JavaParser.parse(sourceFile);
return cu;
}
代码示例来源:origin: javaparser/javasymbolsolver
private CompilationUnit parseSample(String sampleName, String extension) throws ParseException {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(sampleName + "." + extension);
if (is == null) {
throw new RuntimeException("Unable to find sample " + sampleName);
}
CompilationUnit cu = JavaParser.parse(is);
if (cu == null) {
throw new IllegalStateException();
}
return cu;
}
}
代码示例来源:origin: com.badlogicgames.gdx/gdx-jnigen
@Override
public ArrayList<JavaSegment> parse (String classFile) throws Exception {
CompilationUnit unit = JavaParser.parse(new ByteArrayInputStream(classFile.getBytes()));
ArrayList<JavaMethod> methods = new ArrayList<JavaMethod>();
getJavaMethods(methods, getOuterClass(unit));
ArrayList<JniSection> methodBodies = getNativeCodeBodies(classFile);
ArrayList<JniSection> sections = getJniSections(classFile);
alignMethodBodies(methods, methodBodies);
ArrayList<JavaSegment> segments = sortMethodsAndSections(methods, sections);
return segments;
}
代码示例来源:origin: com.infotel.seleniumRobot/core
public List<TestStep> analyzeFile(InputStream inputStream) {
CompilationUnit cu = JavaParser.parse(inputStream);
ClassVisitor clsVisitor = new ClassVisitor();
clsVisitor.visit(cu, null);
MethodVisitor visitor = new MethodVisitor();
visitor.visit(cu, null);
return visitor.getSteps();
}
代码示例来源:origin: javaparser/javasymbolsolver
@Test
public void issue113providedCodeDoesNotCrash() throws FileNotFoundException {
String pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java");
CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
JavaParserFacade parserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration methodDeclaration = cu.getNodesByType(MethodDeclaration.class).stream()
.filter(node -> node.getName().getIdentifier().equals("doSomething")).findAny().orElse(null);
methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve);
}
代码示例来源:origin: javaparser/javasymbolsolver
private CompilationUnit parseWithSymbolResolution(File f) throws IOException {
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
return new JavaParser(parserConfiguration).parse(ParseStart.COMPILATION_UNIT, new StreamProvider(new FileInputStream(f))).getResult().get();
}
代码示例来源:origin: javaparser/javasymbolsolver
@Test
public void annotationMemberDeclarationResolve() throws IOException {
File f = adaptPath(new File("src/test/resources/Annotations.java.txt"));
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit cu = new JavaParser(parserConfiguration).parse(ParseStart.COMPILATION_UNIT, new StreamProvider(new FileInputStream(f))).getResult().get();
AnnotationDeclaration declaration = (AnnotationDeclaration)cu.getType(2);
assertEquals("MyAnnotationWithFields", declaration.getNameAsString());
AnnotationMemberDeclaration memberDeclaration = (AnnotationMemberDeclaration)declaration.getMember(0);
ResolvedAnnotationMemberDeclaration resolvedDeclaration = memberDeclaration.resolve();
}
代码示例来源:origin: javaparser/javasymbolsolver
private CompilationUnit parseWithTypeSolver(String code) {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
JavaParser javaParser = new JavaParser(parserConfiguration);
return javaParser.parse(ParseStart.COMPILATION_UNIT, new StringProvider(code)).getResult().get();
}
代码示例来源:origin: javaparser/javasymbolsolver
@Test
public void testIssue() throws FileNotFoundException {
File src = adaptPath(new File("src/test/resources/recursion-issue"));
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new JavaParserTypeSolver(src));
combinedTypeSolver.add(new ReflectionTypeSolver());
CompilationUnit agendaCu = JavaParser.parse(adaptPath(new File("src/test/resources/recursion-issue/Usage.java")));
MethodCallExpr foo = Navigator.findMethodCall(agendaCu, "foo");
assert foo != null;
JavaParserFacade.get(combinedTypeSolver).getType(foo);
}
代码示例来源:origin: javaparser/javasymbolsolver
@Test(expected = UnsolvedSymbolException.class)
public void aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageImportingIt() {
String code = "package myPackage; import B; class A extends B {}";
MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
memoryTypeSolver.addDeclaration("B", new MyClassDeclaration("B"));
ClassOrInterfaceType jpType = JavaParser.parse(code).getClassByName("A").get().getExtendedTypes(0);
ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(jpType);
assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
}
内容来源于网络,如有侵权,请联系作者删除!