private void getJavaMethods (ArrayList<JavaMethod> methods, TypeDeclaration type) { classStack.push(type); if (type.getMembers() != null) { for (BodyDeclaration member : type.getMembers()) { if (member instanceof ClassOrInterfaceDeclaration || member instanceof EnumDeclaration) { getJavaMethods(methods, (TypeDeclaration)member); } else { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration)member; if (!ModifierSet.hasModifier(((MethodDeclaration)member).getModifiers(), ModifierSet.NATIVE)) continue; methods.add(createMethod(method)); } } } } classStack.pop(); }
private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
if (td.getParentNode() instanceof TypeDeclaration) { TypeDeclaration containerTd = (TypeDeclaration) td.getParentNode(); String parentClassSimpleName = containerTd.getName().toString(); containerClass = sourceFileInfo.getClassInfoBySimpleName(parentClassSimpleName); } else { classInfo.className = getClassName(packageName, containerClass == null ? "" : containerClass.className, td.getName().toString()); classInfo.isStaticNested = containerClass != null && ModifierSet.isStatic(td.getModifiers()); classInfo.reflectionClassName = getReflectionClassName(packageName, containerClass == null ? "" : containerClass.reflectionClassName, td.getName().toString()); classInfo.visibility = Visibility.fromModifiers(td.getModifiers()); classInfo.isAbstract = ModifierSet.isAbstract(td.getModifiers()); List<AnnotationExpr> annotationList = td.getAnnotations(); List<BodyDeclaration> bodyDeclarationList = td.getMembers(); if (bodyDeclarationList == null) bodyDeclarationList = new LinkedList<BodyDeclaration>();
public void index(TypeDeclaration<?> type, int cuId) { EnumSet<Modifier> modifiers = type.getModifiers(); boolean isInterface = type instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) type).isInterface(); boolean isAnnotation = type instanceof AnnotationDeclaration; boolean isEnumeration = type instanceof EnumDeclaration; boolean isClass = !isAnnotation && !isEnumeration && !isInterface; Type t = new Type(type.getNameAsString(), type.isPublic(), type.isStatic(), modifiers.contains(FINAL), modifiers.contains(ABSTRACT), isClass, isInterface, isEnumeration, isAnnotation, cuId); int typeId = typeDao.save(t); for (BodyDeclaration member : type.getMembers()) { bodyDeclarationIndexer.index(member, typeId); } } }
@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; }
Optional<Node> pnode = n.getParentNode(); Node node; if (pnode.isPresent() && (node = pnode.get()) instanceof CompilationUnit) { CompilationUnit cu2 = (CompilationUnit) node; className = cu2.getType(0).getNameAsString(); String packageAndClassName = (className.contains("."))
protected void prepareFieldComment(FieldDeclaration fieldDeclaration, TypeDocMeta typeDocMeta) { if (fieldDeclaration.getVariables().stream().anyMatch(variable -> variable.getNameAsString().equals(typeDocMeta.getName()))) { String comment = adjustComment(fieldDeclaration); if (DfStringUtil.is_NotNull_and_NotEmpty(comment)) { if (DfStringUtil.is_Null_or_Empty(typeDocMeta.getComment()) || fieldDeclaration.getParentNode().map(parentNode -> { @SuppressWarnings("unchecked") TypeDeclaration<TypeDeclaration<?>> typeDeclaration = (TypeDeclaration<TypeDeclaration<?>>) parentNode; return typeDeclaration.getNameAsString().equals(clazz.getSimpleName()); }).orElse(false)) { typeDocMeta.setComment(comment); Matcher matcher = FIELD_COMMENT_END_PATTERN.matcher(saveFieldCommentSpecialExp(comment)); if (matcher.find()) { String description = matcher.group(1).trim(); typeDocMeta.setDescription(restoreFieldCommentSpecialExp(description)); } } } } }
final Class<?> moduleType = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + typeDeclaration.getNameAsString()); IgnoreApi ignoreApi = moduleType.getAnnotation(IgnoreApi.class); if (ignoreApi == null) { final Class<?> moduleType = Class.forName(cu.getPackageDeclaration().get().getNameAsString() + "." + typeDeclaration.getNameAsString()); if (typeDeclaration.getComment().isPresent()) { String commentText = CommentUtils.parseCommentText(typeDeclaration.getComment().get().getContent()); commentText = commentText.split("\n")[0].split("\r")[0]; apiModule.setComment(commentText);
@Override protected int getNumberOfTypes(CompilationUnit compilationUnit) { List<AnnotationExpr> annotations = compilationUnit.getTypes().get(0).getAnnotations(); return annotations.size(); }
public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
if (td.getParentNode() instanceof TypeDeclaration) { TypeDeclaration containerTd = (TypeDeclaration) td.getParentNode(); String parentClassSimpleName = containerTd.getName().toString(); containerClass = sourceFileInfo.getClassInfoBySimpleName(parentClassSimpleName); } else { classInfo.className = getClassName(packageName, containerClass == null ? "" : containerClass.className, td.getName().toString()); classInfo.isStaticNested = containerClass != null && ModifierSet.isStatic(td.getModifiers()); classInfo.reflectionClassName = getReflectionClassName(packageName, containerClass == null ? "" : containerClass.reflectionClassName, td.getName().toString()); classInfo.visibility = Visibility.fromModifiers(td.getModifiers()); classInfo.isAbstract = ModifierSet.isAbstract(td.getModifiers()); List<AnnotationExpr> annotationList = td.getAnnotations(); List<BodyDeclaration> bodyDeclarationList = td.getMembers(); if (bodyDeclarationList == null) bodyDeclarationList = new LinkedList<BodyDeclaration>();
private Collection<Member> gatherFinalMembers(List<Member> members, CompilationUnit cu) { Map<String, Member> memberMap = new LinkedHashMap<>(); for (Member member : members) { String key = member.getKey(); if (!memberMap.containsKey(key)) { memberMap.put(key, member); } else { BodyDeclaration<?> copy = member.getDeclaration().clone(); if (copy instanceof ConstructorDeclaration) { ((ConstructorDeclaration) copy).setBody(JavaParser.parseBlock("{}")); ((ConstructorDeclaration) copy).setComment(null); } else if (copy instanceof MethodDeclaration) { ((MethodDeclaration) copy).setBody(null); ((MethodDeclaration) copy).setComment(null); } else if (copy instanceof FieldDeclaration) { ((FieldDeclaration) copy).getVariable(0).setInitializer((Expression) null); ((FieldDeclaration) copy).setComment(null); } copy.setAnnotations(new NodeList<>()); Logger.getGlobal().warning(String.format("Suppressing already-present generated member in type %s: %s", cu.getType(0).getNameAsString(), copy.toString())); } } return memberMap.values(); }
@Override protected String getTypeName(CompilationUnit compilationUnit, int index) { List<AnnotationExpr> annotations = compilationUnit.getTypes().get(0).getAnnotations(); return annotations.get(index).getNameAsString(); }
@Nonnull private String resolveReferencedType(@Nonnull Qualifier qualifier, @Nonnull TypeDeclaration type) { Qualifier parentQualifier = qualifier.getParentQualifier(); if (parentQualifier != null) { for (TypeDeclaration innerType : emptyIfNull(type.getMembers()).filter(TypeDeclaration.class)) { if (parentQualifier.getName().equals(innerType.getName())) { return resolveReferencedType(parentQualifier, innerType); } } } return getTypeName(type); }
private void getJavaMethods (ArrayList<JavaMethod> methods, TypeDeclaration type) { classStack.push(type); if (type.getMembers() != null) { for (BodyDeclaration member : type.getMembers()) { if (member instanceof ClassOrInterfaceDeclaration || member instanceof EnumDeclaration) { getJavaMethods(methods, (TypeDeclaration)member); } else { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration)member; if (!ModifierSet.hasModifier(((MethodDeclaration)member).getModifiers(), ModifierSet.NATIVE)) continue; methods.add(createMethod(method)); } } } } classStack.pop(); }
private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
@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); }
/** * Looks among the type declared in the TypeDeclaration for one having the specified name. * The name can be qualified with respect to the TypeDeclaration. For example, if the class declarationd defines class D * and class D contains an internal class named E then the qualifiedName that can be resolved are "D", and "D.E". */ public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
/** * Returns the {@code (i+1)}'th constructor of the given type declaration, in textual order. The constructor that * appears first has the index 0, the second one the index 1, and so on. * * @param td The type declaration to search in. Note that only classes and enums have constructors. * @param index The index of the desired constructor. * @return The desired ConstructorDeclaration if it was found, and {@code null} otherwise. */ public static ConstructorDeclaration demandConstructor(TypeDeclaration<?> td, int index) { ConstructorDeclaration found = null; int i = 0; for (BodyDeclaration<?> bd : td.getMembers()) { if (bd instanceof ConstructorDeclaration) { ConstructorDeclaration cd = (ConstructorDeclaration) bd; if (i == index) { found = cd; break; } i++; } } if (found == null) { throw new IllegalStateException("No constructor with index " + index); } return found; }