當前位置: 首頁>>代碼示例>>Java>>正文


Java CompilationUnit.getTypes方法代碼示例

本文整理匯總了Java中com.github.javaparser.ast.CompilationUnit.getTypes方法的典型用法代碼示例。如果您正苦於以下問題:Java CompilationUnit.getTypes方法的具體用法?Java CompilationUnit.getTypes怎麽用?Java CompilationUnit.getTypes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.github.javaparser.ast.CompilationUnit的用法示例。


在下文中一共展示了CompilationUnit.getTypes方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: extractClassName

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Nullable
private static String extractClassName( String code, PrintStream err ) {
    InputStream inputStream = new ByteArrayInputStream( code.getBytes( StandardCharsets.UTF_8 ) );
    try {
        CompilationUnit compilationUnit = JavaParser.parse( inputStream );
        List<TypeDeclaration> types = compilationUnit.getTypes();
        if ( types.size() == 1 ) {
            String simpleType = types.get( 0 ).getName();
            return Optional.ofNullable( compilationUnit.getPackage() )
                    .map( PackageDeclaration::getPackageName )
                    .map( it -> it + "." + simpleType )
                    .orElse( simpleType );
        } else if ( types.size() == 0 ) {
            err.println( "No class definition found" );
        } else {
            err.println( "Too many class definitions found. Only one class can be defined at a time." );
        }
    } catch ( ParseException e ) {
        // ignore error, let the compiler provide an error message
        return "Err";
    }

    return null;
}
 
開發者ID:renatoathaydes,項目名稱:osgiaas,代碼行數:25,代碼來源:JavaCommand.java

示例2: doIsEquals

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override
public boolean doIsEquals(CompilationUnit first, CompilationUnit second) {
  // 檢測包聲明
  if (!isEqualsUseMerger(first.getPackage(), second.getPackage())) return false;

  // 檢查公共類聲明
  for (TypeDeclaration outer : first.getTypes()) {
    for (TypeDeclaration inner : second.getTypes()) {
      if (ModifierSet.isPublic(outer.getModifiers()) && ModifierSet.isPublic(inner.getModifiers())) {
        if (outer.getName().equals(inner.getName())) {
          return true;
        }
      }
    }
  }

  return false;
}
 
開發者ID:beihaifeiwu,項目名稱:dolphin,代碼行數:19,代碼來源:CompilationUnitMerger.java

示例3: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override public void visit(final CompilationUnit n, final A arg) {
	visitComment(n.getComment(), arg);
	if (n.getPackage() != null) {
		n.getPackage().accept(this, arg);
	}
	if (n.getImports() != null) {
		for (final ImportDeclaration i : n.getImports()) {
			i.accept(this, arg);
		}
	}
	if (n.getTypes() != null) {
		for (final TypeDeclaration typeDeclaration : n.getTypes()) {
			typeDeclaration.accept(this, arg);
		}
	}
}
 
開發者ID:plum-umd,項目名稱:java-sketch,代碼行數:17,代碼來源:VoidVisitorAdapter.java

示例4: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override public void visit(final CompilationUnit n, final A arg) {
    visitComment(n.getComment(), arg);
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (final ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
    }
    if (n.getTypes() != null) {
        for (final TypeDeclaration typeDeclaration : n.getTypes()) {
            typeDeclaration.accept(this, arg);
        }
    }
}
 
開發者ID:plum-umd,項目名稱:java-sketch,代碼行數:17,代碼來源:JsonVisitorAdapter.java

示例5: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override public Node visit(final CompilationUnit n, final A arg) {
	if (n.getPackage() != null) {
		n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
	}
	final List<ImportDeclaration> imports = n.getImports();
	if (imports != null) {
		for (int i = 0; i < imports.size(); i++) {
			imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
		}
		removeNulls(imports);
	}
	final List<TypeDeclaration> types = n.getTypes();
	if (types != null) {
		for (int i = 0; i < types.size(); i++) {
			types.set(i, (TypeDeclaration) types.get(i).accept(this, arg));
		}
		removeNulls(types);
	}
	return n;
}
 
開發者ID:plum-umd,項目名稱:java-sketch,代碼行數:21,代碼來源:ModifierVisitorAdapter.java

示例6: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override public void visit(final CompilationUnit n, final Object arg) {
	printJavaComment(n.getComment(), arg);

	if (n.getPackage() != null) {
		n.getPackage().accept(this, arg);
	}

	if (n.getImports() != null) {
		for (final ImportDeclaration i : n.getImports()) {
			i.accept(this, arg);
		}
		printer.printLn();
	}

	if (n.getTypes() != null) {
		for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
			i.next().accept(this, arg);
			printer.printLn();
			if (i.hasNext()) {
				printer.printLn();
			}
		}
	}

       printOrphanCommentsEnding(n);
}
 
開發者ID:javaparser,項目名稱:javasymbolsolver,代碼行數:27,代碼來源:DumpVisitor.java

示例7: fromCode

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
public static CodeStubBuilder fromCode(String code) {
  CompilationUnit cu = JavaParser.parse(code);

  for (TypeDeclaration<?> type : cu.getTypes()) {

    if (type instanceof ClassOrInterfaceDeclaration
            && type.getModifiers().contains(Modifier.PUBLIC)) {

      String className = type.getNameAsString();
      return new CodeStubBuilder(className);
    }
  }

  throw new IllegalArgumentException("Task instance's code did not contain any public class");
}
 
開發者ID:tdd-pingis,項目名稱:tdd-pingpong,代碼行數:16,代碼來源:CodeStubBuilder.java

示例8: changeMethods

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
private static void changeMethods(CompilationUnit cu) {
    List<TypeDeclaration> types = cu.getTypes();
    for (TypeDeclaration type : types) {
        List<BodyDeclaration> members = type.getMembers();
        for (BodyDeclaration member : members) {
            if (member instanceof MethodDeclaration) {
                MethodDeclaration method = (MethodDeclaration) member;
                changeMethod(method);
            }
        }
    }
}
 
開發者ID:bingoohuang,項目名稱:javacode-demo,代碼行數:13,代碼來源:MethodChangerWithoutVisitor.java

示例9: calculateRelations

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
public void calculateRelations(Collection<File> files) {
    System.out.println();
    int totalFiles = files.size();
    int fileIndex = 1;
    for (File file : files) {
        try {
            CompilationUnit cu = parse(file);
            NodeList<TypeDeclaration<?>> types = cu.getTypes();
            for (TypeDeclaration<?> type : types) {
                boolean isInterface = type instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) type).isInterface();
                boolean isAnnotation = type instanceof AnnotationDeclaration;
                boolean isEnumeration = type instanceof EnumDeclaration;
                boolean isClass = !isAnnotation && !isEnumeration && !isInterface;
                if (isInterface) {
                    // check if this interface extends another interface and persist relation in EXTENDS table
                    ClassOrInterfaceDeclaration interfaceDeclaration = (ClassOrInterfaceDeclaration) type;
                    extendsRelationCalculator.calculate(interfaceDeclaration, cu);
                }
                if (isClass) {
                    ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) type;
                    // check if this class implements an interface and persist relation in IMPLEMENTS table
                    implementsRelationCalculator.calculate(classDeclaration, cu);
                    // check if this class extends another class and persist relation in EXTENDS table
                    extendsRelationCalculator.calculate(classDeclaration, cu);
                }
                if (isClass || isInterface) {
                    annotatedWithCalculator.calculate((ClassOrInterfaceDeclaration) type, cu);
                }
            }
        } catch (ParseProblemException | IOException e) {
            System.err.println("Error while parsing " + file.getAbsolutePath());
        }
        System.out.print("\rCalculating relations: " + getPercent(fileIndex, totalFiles) + "% " + ("(" + fileIndex + "/" + totalFiles + ")"));
        fileIndex++;
    }
    System.out.println();
}
 
開發者ID:benas,項目名稱:jql,代碼行數:38,代碼來源:RelationCalculator.java

示例10: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override
public void visit(final CompilationUnit n, final Object arg) {
    printer.printLn("CompilationUnit");
    printJavaComment(n.getComment(), arg);

    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }

    if (n.getImports() != null) {
        for (final ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
        printer.printLn();
    }

    if (n.getTypes() != null) {
        for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext(); ) {
            i.next().accept(this, arg);
            printer.printLn();
            if (i.hasNext()) {
                printer.printLn();
            }
        }
    }

    printOrphanCommentsEnding(n);
}
 
開發者ID:pcgomes,項目名稱:javaparser2jctree,代碼行數:29,代碼來源:ASTDumpVisitor.java

示例11: visit

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Override
public JCTree visit(final CompilationUnit n, final Object arg) {
    // ARG0: List<JCAnnotation> packageAnnotations
    List<JCAnnotation> arg0 = List.<JCAnnotation>nil();

    // ARG1: JCExpression pid
    // Package declaration
    JCExpression arg1 = null;

    if (n.getPackage() != null) {
        arg1 = (JCExpression) n.getPackage().accept(this, arg);

        if (n.getPackage().getAnnotations() != null) {
            for (final AnnotationExpr packageAnnotation : n.getPackage().getAnnotations()) {
                arg0 = arg0.append((JCAnnotation) packageAnnotation.accept(this, arg));
            }
        }
    }

    // ARG2 = List<JCTree> defs
    // Imports and classes are passed in the same structure
    List<JCTree> arg2 = List.<JCTree>nil();
    if (n.getImports() != null) {
        for (final ImportDeclaration i : n.getImports()) {
            arg2 = arg2.append(i.accept(this, arg));
        }
    }
    if (n.getTypes() != null) {
        for (final TypeDeclaration typeDeclaration : n.getTypes()) {
            arg2 = arg2.append(typeDeclaration.accept(this, arg));
        }
    }

    return new AJCCompilationUnit(make.TopLevel(arg0, arg1, arg2), ((n.getComment() != null) ? n.getComment().getContent() : null));
}
 
開發者ID:pcgomes,項目名稱:javaparser2jctree,代碼行數:36,代碼來源:JavaParser2JCTree.java

示例12: testParser

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
@Test
public void testParser() throws Exception {

    CompilationUnit cu = Sources.FROM_CLASSPATH_TO_COMPILATIONUNIT.apply("io/sundr/builder/BaseFluent.java");
    String packageName = cu.getPackage().getPackageName();
    Assert.assertEquals("io.sundr.builder", packageName);

    for (TypeDeclaration typeDeclaration : cu.getTypes()) {
        TypeDef typeDef = Sources.TYPEDEF.apply(typeDeclaration);
        System.out.print(typeDef);
    }
}
 
開發者ID:sundrio,項目名稱:sundrio,代碼行數:13,代碼來源:PareserTest.java

示例13: getClassName

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
public static ClassOrInterfaceDeclaration getClassName(CompilationUnit cu) {
	List<TypeDeclaration> types = cu.getTypes();
	if (Utils.hasLength(types)) {
		TypeDeclaration real = types.get(0);
		if (real instanceof ClassOrInterfaceDeclaration) {
			ClassOrInterfaceDeclaration rl = (ClassOrInterfaceDeclaration) real;
			return rl;
		}
	}
	return null;
}
 
開發者ID:niaoge,項目名稱:spring-dynamic,代碼行數:12,代碼來源:ParserUtils.java

示例14: addTypeDeclaration

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
/**
 * Adds the given type declaration to the compilation unit. The list of
 * types will be initialized if it is <code>null</code>.
 * 
 * @param cu
 *            compilation unit
 * @param type
 *            type declaration
 */
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
    List<TypeDeclaration> types = cu.getTypes();
    if (isNullOrEmpty(types)) {
        types = new ArrayList<TypeDeclaration>();
        cu.setTypes(types);
    }
    types.add(type);

}
 
開發者ID:plum-umd,項目名稱:java-sketch,代碼行數:19,代碼來源:ASTHelper.java

示例15: addTypeDeclaration

import com.github.javaparser.ast.CompilationUnit; //導入方法依賴的package包/類
/**
 * Adds the given type declaration to the compilation unit. The list of
 * types will be initialized if it is <code>null</code>.
 * 
 * @param cu
 *            compilation unit
 * @param type
 *            type declaration
 */
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
    List<TypeDeclaration> types = cu.getTypes();
    if (types == null) {
        types = new ArrayList<TypeDeclaration>();
        cu.setTypes(types);
    }
    types.add(type);

}
 
開發者ID:javaparser,項目名稱:javasymbolsolver,代碼行數:19,代碼來源:ASTHelper.java


注:本文中的com.github.javaparser.ast.CompilationUnit.getTypes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。