com.github.javaparser.ast.CompilationUnit.findAll()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(233)

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

CompilationUnit.findAll介绍

暂无

代码示例

代码示例来源:origin: hs-web/hsweb-framework

CompilationUnit newClazz = JavaParser.parse(code);
Map<String, FieldDeclaration> oldFields = old
    .findAll(FieldDeclaration.class)
    .stream()
    .collect(Collectors.toMap(declaration -> declaration.getVariable(0).getNameAsString(), Function.identity()));
    .findAll(MethodDeclaration.class)
    .stream()
    .collect(Collectors.toMap(NodeWithSimpleName::getNameAsString, Function.identity()));
newClazz.findAll(FieldDeclaration.class).forEach(declaration -> {
  String name = declaration.getVariable(0).getNameAsString();
  if (oldFields.get(name) == null) {
newClazz.findAll(MethodDeclaration.class).forEach(declaration -> {
  String name = declaration.getNameAsString();
  if (oldMethod.get(name) == null) {

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

/**
 * Finds a method in a compilation unit that starts at the specified line number
 * 
 * @param lineNumber
 * @param cu
 * @return MethodDeclaration or null if none found
 */
public static MethodDeclaration getMethodByLineNumberOfMethodName(int lineNumber, CompilationUnit cu) {
  MethodDeclaration result = null;
  List<MethodDeclaration> methods = cu.findAll(MethodDeclaration.class);
  for (MethodDeclaration method : methods) {
    if (isMethodDeclarationAtLine(method, lineNumber)) {
      result = method;
    }
  }
  return result;
}

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

/**
 * Finds a field in a compilation unit that starts at the specified line number
 * 
 * @param lineNumber
 * @param cu
 * @return FieldDeclaration or null if none found
 */
public static FieldDeclaration getFieldDeclarationByLineNumber(int lineNumber, CompilationUnit cu) {
  FieldDeclaration result = null;
  List<FieldDeclaration> fields = cu.findAll(FieldDeclaration.class);
  for (FieldDeclaration field : fields) {
    if (field.getBegin().isPresent()) {
      if (field.getBegin().get().line == lineNumber) {
        result = field;
      }
    }
  }
  return result;
}

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<PackageDeclaration> packageDeclarations = compilationUnit.findAll(PackageDeclaration.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<MethodDeclaration> fileMethods = compilationUnit.findAll(MethodDeclaration.class);
List<MethodCallExpr> fileMethodCalls = compilationUnit.findAll(MethodCallExpr.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<MethodDeclaration> fileMethods = compilationUnit.findAll(MethodDeclaration.class);
List<MethodCallExpr> fileMethodCalls = compilationUnit.findAll(MethodCallExpr.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

/**
 * This method checks all given Java files for methods that equal the given
 * method signature. This is relevant, for example, to check whether a method
 * overwrites a method of a superclass after refactoring.
 * 
 * @param javaFiles
 * @param methodSignature
 * @throws BotRefactoringException
 *             if there is a duplicate
 * @throws FileNotFoundException
 */
public static void checkForDuplicatedMethodSignatures(List<String> javaFiles, String methodSignature)
    throws BotRefactoringException, FileNotFoundException {
  // Iterate all Javafiles
  for (String javaFile : javaFiles) {
    // Create compilation unit
    FileInputStream methodPath = new FileInputStream(javaFile);
    CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(JavaParser.parse(methodPath));
    // Get all Methods and MethodCalls of File
    List<MethodDeclaration> fileMethods = compilationUnit.findAll(MethodDeclaration.class);
    // Check if class contains a method equal to the given method signature
    for (MethodDeclaration fileMethod : fileMethods) {
      if (getMethodSignatureAsString(fileMethod).equals(methodSignature)) {
        throw new BotRefactoringException(
            "File '" + javaFile + "' has a method with the same signature as our refactored method!");
      }
    }
  }
}

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(JavaParser.parse(filepath));
List<MethodCallExpr> methodCalls = compilationUnit.findAll(MethodCallExpr.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

/**
 * This method gets the Method signature that our methods will have without the
 * parameter.
 * 
 * @param refactoring
 * @param paramName
 * @return signature
 * @throws FileNotFoundException
 * @throws BotRefactoringException
 */
private String getPostRefactoringSignature(ParserRefactoring refactoring, String methodName)
    throws FileNotFoundException, BotRefactoringException {
  for (String javaFile : refactoring.getJavaFiles()) {
    // Create compilation unit
    FileInputStream methodPath = new FileInputStream(javaFile);
    CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(JavaParser.parse(methodPath));
    // Get all Methods and MethodCalls of File
    List<MethodDeclaration> fileMethods = compilationUnit.findAll(MethodDeclaration.class);
    // Rename one method and return method signature
    for (MethodDeclaration fileMethod : fileMethods) {
      if (refactoring.getMethods().contains(fileMethod)) {
        performRenameMethod(fileMethod, methodName);
        return RefactoringHelper.getMethodSignatureAsString(fileMethod);
      }
    }
  }
  throw new BotRefactoringException("Error reading methods that need their parameter removed!");
}

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

/**
 * This method gets the Method signature that our methods will have without the
 * parameter.
 * 
 * @param refactoring
 * @param paramName
 * @return signature
 * @throws FileNotFoundException
 * @throws BotRefactoringException
 */
private String getPostRefactoringSignature(ParserRefactoring refactoring, String paramName)
    throws FileNotFoundException, BotRefactoringException {
  for (String javaFile : refactoring.getJavaFiles()) {
    // Create compilation unit
    FileInputStream methodPath = new FileInputStream(javaFile);
    CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(JavaParser.parse(methodPath));
    // Get all Methods and MethodCalls of File
    List<MethodDeclaration> fileMethods = compilationUnit.findAll(MethodDeclaration.class);
    // Rename all Methods
    for (MethodDeclaration fileMethod : fileMethods) {
      if (refactoring.getMethods().contains(fileMethod)) {
        performRemoveMethodParameter(fileMethod, paramName);
        return RefactoringHelper.getMethodSignatureAsString(fileMethod);
      }
    }
  }
  throw new BotRefactoringException("Error reading methods that need their parameter removed!");
}

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<ClassOrInterfaceDeclaration> classes = compilationUnit.findAll(ClassOrInterfaceDeclaration.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<ClassOrInterfaceDeclaration> classes = compilationUnit.findAll(ClassOrInterfaceDeclaration.class);

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<ClassOrInterfaceDeclaration> classes = renameMethodUnit.findAll(ClassOrInterfaceDeclaration.class);
boolean foundMethod = false;

代码示例来源:origin: Refactoring-Bot/Refactoring-Bot

List<ClassOrInterfaceDeclaration> classes = renameMethodUnit.findAll(ClassOrInterfaceDeclaration.class);
boolean foundMethod = false;

相关文章