private TypeDeclaration getOuterClass (CompilationUnit unit) { for (TypeDeclaration type : unit.getTypes()) { if (type instanceof ClassOrInterfaceDeclaration || type instanceof EnumDeclaration) return type; } throw new RuntimeException("Couldn't find class, is your java file empty?"); }
private TypeDeclaration getOuterClass (CompilationUnit unit) { for (TypeDeclaration type : unit.getTypes()) { if (type instanceof ClassOrInterfaceDeclaration || type instanceof EnumDeclaration) return type; } throw new RuntimeException("Couldn't find class, is your java file empty?"); }
private TypeDeclaration getOuterClass (CompilationUnit unit) { for (TypeDeclaration type : unit.getTypes()) { if (type instanceof ClassOrInterfaceDeclaration || type instanceof EnumDeclaration) return type; } throw new RuntimeException("Couldn't find class, is your java file empty?"); }
private static boolean containsClass(CompilationUnit cu) { return cu.getTypes().stream() .anyMatch(t -> t instanceof ClassOrInterfaceDeclaration); }
private static boolean containsEnum(CompilationUnit cu) { return cu.getTypes().stream() .anyMatch(t -> t instanceof EnumDeclaration); }
@Override protected int getNumberOfTypes(CompilationUnit compilationUnit) { List<AnnotationExpr> annotations = compilationUnit.getTypes().get(0).getAnnotations(); return annotations.size(); }
public static Optional<TypeDeclaration<?>> findType(CompilationUnit cu, String qualifiedName) { if (cu.getTypes().isEmpty()) { return Optional.empty(); } final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = cu.getTypes().stream().filter((t) -> t.getName().getId().equals(typeName)).findFirst(); final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
public static Optional<TypeDeclaration<?>> findType(CompilationUnit cu, String qualifiedName) { if (cu.getTypes().isEmpty()) { return Optional.empty(); } final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = cu.getTypes().stream().filter((t) -> t.getName().getId().equals(typeName)).findFirst(); final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
@Override public void visit(CompilationUnit n, Void arg) { // performance for (final TypeDeclaration typeDeclaration : emptyIfNull(n.getTypes())) { typeDeclaration.accept(this, arg); } }
@Override protected String getTypeName(CompilationUnit compilationUnit, int index) { List<AnnotationExpr> annotations = compilationUnit.getTypes().get(0).getAnnotations(); return annotations.get(index).getNameAsString(); }
/** * Looks among the type declared in the Compilation Unit for one having the specified name. * The name can be qualified with respect to the compilation unit. For example, if the compilation * unit is in package a.b; and it contains two top level classes named C and D, with class E being defined inside D * then the qualifiedName that can be resolved are "C", "D", and "D.E". */ public static Optional<TypeDeclaration<?>> findType(CompilationUnit cu, String qualifiedName) { if (cu.getTypes().isEmpty()) { return Optional.empty(); } final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = cu.getTypes().stream().filter((t) -> t.getName().getId().equals(typeName)).findFirst(); final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
public void index(com.github.javaparser.ast.CompilationUnit compilationUnit, String fileName) { String packageName = compilationUnit.getPackageDeclaration().map(NodeWithName::getNameAsString).orElse(""); io.github.benas.jql.model.CompilationUnit cu = new io.github.benas.jql.model.CompilationUnit(fileName, packageName); int cuId = compilationUnitDao.save(cu); List<TypeDeclaration<?>> types = compilationUnit.getTypes(); for (TypeDeclaration<?> type : types) { typeIndexer.index(type, cuId); } }
@Override public Map<String, ResultType> parseStatements( List<String> statements, Collection<String> importedClasses, @Nullable ClassLoaderContext classLoaderContext ) throws ParseException { StringBuilder codeBuilder = new StringBuilder( "class X {" ); codeBuilder.append( "void run() {" ); for (String statement : statements) { codeBuilder.append( statement ); } codeBuilder.append( "} }" ); List<Statement> parsedStatements; try { CompilationUnit compilationUnit = JavaParser.parse( new StringReader( codeBuilder.toString() ), false ); MethodDeclaration methodDeclaration = ( MethodDeclaration ) compilationUnit .getTypes().get( 0 ) .getMembers().get( 0 ); parsedStatements = methodDeclaration.getBody().getStmts(); } catch ( com.github.javaparser.ParseException e ) { throw new ParseException( e.getMessage(), 0 ); } return resultTypes( new TypeDiscoverer( importedClasses, classLoaderContext ) .getTypesByVariableName( parsedStatements ) ); }
CompilationUnit cu = JavaParser.parse(javaFile); for (TypeDeclaration typeDec : cu.getTypes()) { List<BodyDeclaration> members = typeDec.getMembers(); if(members != null) { for (BodyDeclaration member : members) { //Check just members that are FieldDeclarations FieldDeclaration field = (FieldDeclaration) member; //Print the field's class typr System.out.println(field.getType()); //Print the field's name System.out.println(field.getVariables().get(0).getId().getName()); //Print the field's init value, if not null Object initValue = field.getVariables().get(0).getInit(); if(initValue != null) { System.out.println(field.getVariables().get(0).getInit().toString()); } } }
CompilationUnit cu = JavaParser.parse(file); List<TypeDeclaration> typeDeclarations = cu.getTypes(); for (TypeDeclaration typeDec : typeDeclarations) { List<BodyDeclaration> members = typeDec.getMembers(); if(members != null) { for (BodyDeclaration member : members) { if (member instanceof ConstructorDeclaration) { ConstructorDeclaration constructor = (ConstructorDeclaration) member; //Put your code here //The constructor instance contains all the information about it. constructor.getBeginLine(); //begin line constructor.getBlock(); //constructor body } } } }
private void addManualMembers(SimpleJavaGenerator gen, CompilationUnit existing) { for (TypeDeclaration<?> type : existing.getTypes()) { for (BodyDeclaration<?> member : type.getMembers()) { if (member instanceof MethodDeclaration || member instanceof FieldDeclaration || member instanceof ConstructorDeclaration) { if (!isGenerated(member)) { gen.addMember(new Member(member)); } } } } }
@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; }
@Override public CompilationUnit doMerge(CompilationUnit first, CompilationUnit second) { CompilationUnit unit = new CompilationUnit(); unit.setPackage(mergeSingle(first.getPackage(), second.getPackage())); unit.setImports(mergeCollections(first.getImports(), second.getImports())); unit.setTypes(mergeCollections(first.getTypes(), second.getTypes())); return unit; }
@Override public DocTag converter(String comment) { DocTag docTag = super.converter(comment); String path = ClassMapperUtils.getPath((String) docTag.getValues()); if (StringUtils.isBlank(path)) { return null; } Class<?> returnClassz; CompilationUnit cu; try (FileInputStream in = new FileInputStream(path)) { cu = JavaParser.parse(in); if (cu.getTypes().size() <= 0) { return null; } returnClassz = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + cu.getTypes().get(0).getNameAsString()); } catch (Exception e) { log.warn("读取java原文件失败:{}", path, e.getMessage()); return null; } String text = cu.getComment().isPresent() ? CommentUtils.parseCommentText(cu.getComment().get().getContent()) : ""; Map<String, String> commentMap = this.analysisFieldComments(returnClassz); List<FieldInfo> fields = this.analysisFields(returnClassz, commentMap); ObjectInfo objectInfo = new ObjectInfo(); objectInfo.setType(returnClassz); objectInfo.setFieldInfos(fields); objectInfo.setComment(text); return new SeeTagImpl(docTag.getTagName(), objectInfo); }
public static String mergeContent(CompilationUnit one, CompilationUnit two) throws Exception { // 包声明不同,返回null if (!one.getPackage().equals(two.getPackage())) return null; CompilationUnit cu = new CompilationUnit(); // add package declaration to the compilation unit PackageDeclaration pd = new PackageDeclaration(); pd.setName(one.getPackage().getName()); cu.setPackage(pd); // check and merge file comment; Comment fileComment = mergeSelective(one.getComment(), two.getComment()); cu.setComment(fileComment); // check and merge imports List<ImportDeclaration> ids = mergeListNoDuplicate(one.getImports(), two.getImports()); cu.setImports(ids); // check and merge Types List<TypeDeclaration> types = mergeTypes(one.getTypes(), two.getTypes()); cu.setTypes(types); return cu.toString(); }