com.github.javaparser.JavaParser类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(84)

本文整理了Java中com.github.javaparser.JavaParser类的一些代码示例,展示了JavaParser类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JavaParser类的具体详情如下:
包路径:com.github.javaparser.JavaParser
类名称:JavaParser

JavaParser介绍

暂无

代码示例

代码示例来源: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: stackoverflow.com

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException, IOException, RecognitionException {
  JavaLexer lexer = new JavaLexer(new ANTLRFileStream(args[1], "UTF-8"));
  JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
  CommonTree tree = (CommonTree)(parser.javaSource().getTree());
  int type = ((Integer)(JavaParser.class.getDeclaredField(args[0]).get(null))).intValue();
  System.out.println(count(tree, type));

代码示例来源: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: gradle.plugin.ru.javasaw/javasaw

private ModuleDefinition parseModuleInfo(File moduleInfo) {
    try {
      ParserConfiguration.LanguageLevel languageLevel = JAVA_10;
      JavaParser.getStaticConfiguration().setLanguageLevel(languageLevel); // todo  to extension
      LOGGER.debug("Parsing module info at language level " + languageLevel);
      CompilationUnit compilationUnit = JavaParser.parse(String.join("\n", Files.readAllLines(moduleInfo.toPath())));
      ModuleDeclaration moduleDeclaration = compilationUnit.getModule().orElseThrow(() -> new RuntimeException("can`t get module info metadata from parsing"));
      List<ModuleRequiresStmt> list = moduleDeclaration.getModuleStmts().stream()
          .filter(stms -> stms instanceof ModuleRequiresStmt).map(stms -> (ModuleRequiresStmt) stms)
          .collect(Collectors.toList());
      return new ModuleDefinition(moduleDeclaration.getNameAsString(), list);
    } catch (IOException e) {
      LOGGER.error("Catch error while parsing module info " + moduleInfo.getName(), e);
    }
    return null;
  }
}

代码示例来源:origin: beihaifeiwu/dolphin

/**
 * Util method to make source merge more convenient
 *
 * @param first  merge params, specifically for the existing source
 * @param second merge params, specifically for the new source
 * @return merged result
 * @throws ParseException cannot parse the input params
 */
public static String merge(String first, String second) throws ParseException {
 JavaParser.setDoNotAssignCommentsPreceedingEmptyLines(false);
 CompilationUnit cu1 = JavaParser.parse(new StringReader(first), true);
 CompilationUnit cu2 = JavaParser.parse(new StringReader(second), true);
 AbstractMerger<CompilationUnit> merger = AbstractMerger.getMerger(CompilationUnit.class);
 CompilationUnit result = merger.merge(cu1, cu2);
 return result.toString();
}

代码示例来源:origin: ftomassetti/analyze-java-code-examples

public static void main(String[] args) {
  Expression expressionNode = JavaParser.parseExpression("1 + 2");
  BodyDeclaration methodNode = JavaParser.parseClassBodyDeclaration(
      "boolean invert(boolean aFlag) { return !p; }");
  CompilationUnit compilationUnitNode = JavaParser.parse("class A { int aField; }");

代码示例来源:origin: stackoverflow.com

if (extension.equals("py")) {
  return new PythonParser();
}
else if(extension.equals("java")) {
  return new JavaParser();
}
else ...

代码示例来源:origin: stackoverflow.com

final JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
final List<String> variables = new ArrayList<>();
    variables.add(ctx.variableDeclaratorId().getText());
}, parser.compilationUnit());

代码示例来源:origin: stackoverflow.com

/**
 * Returns the number of variable declarations inside the given method, by parsing the method's AST
 * @param method The method text
 */
private int countVariableDeclarations(String method) {
  JavaLexer lex = new JavaLexer(new ANTLRInputStream(method));
  JavaParser parse = new JavaParser(new CommonTokenStream(lex));
  ParseTree tree = parse.methodDeclaration();

  ParseTreeWalker walker = new ParseTreeWalker();
  final AtomicInteger count = new AtomicInteger();
  walker.walk(new JavaBaseListener() {
    @Override public void enterLocalVariableDeclaration(JavaParser.LocalVariableDeclarationContext ctx) {
      count.incrementAndGet();
    }
  }, tree);

  return count.get();
}

代码示例来源:origin: javaparser/javasymbolsolver

@Test(expected = IllegalStateException.class)
public void toResolveDoubleWeNeedTheAST() {
  getExpressionType(typeResolver, JavaParser.parseExpression("new Double[]{2.0d, 3.0d}[1]"));
}

代码示例来源: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: gradle.plugin.org.javamodularity/moduleplugin

Path moduleInfoJava = moduleInfoSrcDir.get().toPath().resolve("module-info.java");
try {
  JavaParser.getStaticConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
  Optional<ModuleDeclaration> module = JavaParser.parse(moduleInfoJava).getModule();
  if (module.isPresent()) {
    Name name = module.get().getName();

代码示例来源:origin: com.github.javaparser/javaparser-symbol-solver-core

public JavaParserTypeSolver(Path srcDir, ParserConfiguration parserConfiguration) {
  if (!Files.exists(srcDir) || !Files.isDirectory(srcDir)) {
    throw new IllegalStateException("SrcDir does not exist or is not a directory: " + srcDir);
  }
  this.srcDir = srcDir;
  javaParser = new JavaParser(parserConfiguration);
}

代码示例来源:origin: stackoverflow.com

JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
}, parser.compilationUnit());

代码示例来源:origin: javaparser/javasymbolsolver

@Test(expected = IllegalStateException.class)
public void toResolveFloatWeNeedTheAST() {
  getExpressionType(typeResolver, JavaParser.parseExpression("new Float[]{2.0d, 3.0d}"));
}

代码示例来源: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: 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: Refactoring-Bot/Refactoring-Bot

JavaParser.getStaticConfiguration().setSymbolResolver(javaSymbolSolver);
CompilationUnit renameMethodUnit = LexicalPreservingPrinter.setup(JavaParser.parse(methodPath));

代码示例来源:origin: stackoverflow.com

public Parser getParser(String filename)  {
  String extension = filename.substring(filename.lastIndexOf("."));

  if( "py".equals(extension)  ) { return new PythonParser() }
  if( "java".equals(extension)) { return new JavaParser();  }
  if( "c".equals(extension)   ) { return new CParser();     }

  return new TextParser();
}

代码示例来源:origin: stackoverflow.com

public static void main(String... args) throws IOException {
  JavaLexer lexer = new JavaLexer(new ANTLRFileStream(sourceFile, "UTF-8"));
  JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
  ParseTree tree = parser.compilationUnit();

  ParseTreeWalker walker = new ParseTreeWalker();
  MyListener listener = new MyListener();
  walker.walk(listener, tree);
}

相关文章

微信公众号

最新文章

更多