private int findFieldInsertIndex( List<BodyDeclaration> decls, FieldDeclaration newDecl, int maxOffset) { if (maxOffset != -1) { for (int i = decls.size() - 1; i >= 0; i--) { BodyDeclaration curr = decls.get(i); if (maxOffset > curr.getStartPosition() + curr.getLength()) { return ASTNodes.getInsertionIndex(newDecl, decls.subList(0, i + 1)); } } return 0; } return ASTNodes.getInsertionIndex(newDecl, decls); }
public static void getMissingJavadocCommentProposals( IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException { ASTNode node = problem.getCoveringNode(context.getASTRoot()); if (node == null) { return; } BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node); if (declaration == null) { return; } ICompilationUnit cu = context.getCompilationUnit(); ITypeBinding binding = Bindings.getBindingOfParentType(declaration); if (binding == null) { return; } if (declaration instanceof MethodDeclaration) { MethodDeclaration methodDecl = (MethodDeclaration) declaration; IMethodBinding methodBinding = methodDecl.resolveBinding(); IMethodBinding overridden = null; if (methodBinding != null) { overridden = Bindings.findOverriddenMethod(methodBinding, true); } String string = CodeGeneration.getMethodComment( cu, binding.getName(), methodDecl, overridden, String.valueOf('\n')); if (string != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string)); } } else if (declaration instanceof AbstractTypeDeclaration) { String typeQualifiedName = Bindings.getTypeQualifiedName(binding); String[] typeParamNames; if (declaration instanceof TypeDeclaration) { List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters(); typeParamNames = new String[typeParams.size()]; for (int i = 0; i < typeParamNames.length; i++) { typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier(); } } else { typeParamNames = new String[0]; } String string = CodeGeneration.getTypeComment( cu, typeQualifiedName, typeParamNames, String.valueOf('\n')); if (string != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string)); } } else if (declaration instanceof FieldDeclaration) { String comment = "/**\n *\n */\n"; // $NON-NLS-1$ List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments(); if (fragments != null && fragments.size() > 0) { VariableDeclaration decl = fragments.get(0); String fieldName = decl.getName().getIdentifier(); String typeName = binding.getName(); comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n')); } if (comment != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment)); } } else if (declaration instanceof EnumConstantDeclaration) { EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration; String id = enumDecl.getName().getIdentifier(); String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n')); String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment)); } }