1 19 20 package org.netbeans.modules.javadoc.hints; 21 22 import com.sun.javadoc.Doc; 23 import javax.lang.model.SourceVersion; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.ExecutableElement; 26 import javax.lang.model.element.NestingKind; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.element.TypeParameterElement; 29 import javax.lang.model.element.VariableElement; 30 import javax.lang.model.type.DeclaredType; 31 import javax.lang.model.type.TypeKind; 32 import javax.lang.model.type.TypeMirror; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.Document ; 35 import javax.swing.text.Position ; 36 import org.netbeans.api.java.source.CompilationInfo; 37 38 42 public final class JavadocGenerator { 43 44 private final SourceVersion srcVersion; 45 46 47 public JavadocGenerator(SourceVersion version) { 48 this.srcVersion = version; 49 } 50 51 public String generateComment(TypeElement clazz, CompilationInfo javac) { 52 StringBuilder builder = new StringBuilder ( 53 "/**\n" + " * \n" ); 56 57 if (clazz.getNestingKind() == NestingKind.TOP_LEVEL) { 58 builder.append(" * @author " + System.getProperty("user.name") + "\n"); } 60 61 if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0) { 62 for (TypeParameterElement param : clazz.getTypeParameters()) { 63 builder.append(" * @param " + param.getSimpleName().toString() + " \n"); } 65 } 66 67 if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 && 68 JavadocUtilities.isDeprecated(javac, clazz)) { 69 builder.append(" * @deprecated\n"); } 71 72 builder.append(" */\n"); 74 return builder.toString(); 75 } 76 77 public String generateComment(ExecutableElement method, CompilationInfo javac) { 78 StringBuilder builder = new StringBuilder ( 79 "/**\n" + " * \n" ); 82 83 for (VariableElement param : method.getParameters()) { 84 builder.append(" * @param ").append(param.getSimpleName().toString()).append(" \n"); } 86 87 if (method.getReturnType().getKind() != TypeKind.VOID) { 88 builder.append(" * @return \n"); } 90 91 for (TypeMirror exceptionType : method.getThrownTypes()) { 92 TypeElement exception = (TypeElement) ((DeclaredType) exceptionType).asElement(); 93 builder.append(" * @throws ").append(exception.getQualifiedName().toString()).append(" \n"); } 95 96 if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 && 97 JavadocUtilities.isDeprecated(javac, method)) { 98 builder.append(" * @deprecated\n"); } 100 101 builder.append(" */\n"); 103 return builder.toString(); 104 } 105 106 public String generateComment(VariableElement field, CompilationInfo javac) { 107 StringBuilder builder = new StringBuilder ( 108 "/**\n" + " * \n" ); 111 112 if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 && 113 JavadocUtilities.isDeprecated(javac, field)) { 114 builder.append(" * @deprecated\n"); } 116 117 118 builder.append(" */\n"); 120 return builder.toString(); 121 } 122 123 public String generateComment(Element elm, CompilationInfo javac) { 124 switch(elm.getKind()) { 125 case CLASS: 126 case ENUM: 127 case INTERFACE: 128 case ANNOTATION_TYPE: 129 return generateComment((TypeElement) elm, javac); 130 case CONSTRUCTOR: 131 case METHOD: 132 return generateComment((ExecutableElement) elm, javac); 133 case FIELD: 134 case ENUM_CONSTANT: 135 return generateComment((VariableElement) elm, javac); 136 default: 137 throw new UnsupportedOperationException (elm.getKind() + 138 ", " + elm.getClass() + ": " + elm.toString()); } 140 } 141 142 public String generateInheritComment() { 143 return "/** {@inheritDoc} */"; } 145 146 public static String indentJavadoc(String jdoc, String tab) { 147 return jdoc.replace("\n *", "\n" + tab + " *") + tab; } 149 150 public static String guessIndentation(Document doc, Position pos) throws BadLocationException { 151 String content = doc.getText(0, doc.getLength()); 152 int offset; 153 for (offset = pos.getOffset(); offset >= 0 && content.charAt(offset) != '\n'; offset--); 154 return content.substring(offset + 1, pos.getOffset()); 155 } 156 157 public static String guessJavadocIndentation(CompilationInfo javac, Document doc, Doc jdoc) throws BadLocationException { 158 Position [] jdBounds = JavadocUtilities.findDocBounds(javac, doc, jdoc); 159 if (jdBounds == null) { 160 return ""; } 162 163 String txt = doc.getText(0, doc.getLength()); 164 int count = 0; 165 for (int offset = jdBounds[0].getOffset() - 1; offset >= 0; offset--) { 166 char c = txt.charAt(offset); 167 if (c == '\n' || !Character.isWhitespace(c)) { 168 count = jdBounds[0].getOffset() - offset; 169 break; 170 } 171 } 172 173 char[] indent = new char[count]; 174 for (int i = 0; i < indent.length; i++) { 175 indent[i] = ' '; 176 } 177 178 return String.valueOf(indent); 179 } 180 181 } 182 | Popular Tags |