1 19 20 package org.netbeans.modules.java.source; 21 22 import com.sun.javadoc.ClassDoc; 23 import com.sun.tools.javac.code.Flags; 24 import com.sun.tools.javac.code.Kinds; 25 import com.sun.tools.javac.code.Symbol.ClassSymbol; 26 import com.sun.tools.javac.code.Symbol.MethodSymbol; 27 import com.sun.tools.javac.code.Symbol.PackageSymbol; 28 import com.sun.tools.javac.code.Symbol.TypeSymbol; 29 import com.sun.tools.javac.code.Symbol.VarSymbol; 30 import com.sun.tools.javac.tree.JCTree.JCClassDecl; 31 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 32 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; 33 import com.sun.tools.javac.util.Context; 34 import com.sun.tools.javac.util.Name; 35 import com.sun.tools.javac.util.Position; 36 import com.sun.tools.javadoc.AnnotationTypeDocImpl; 37 import com.sun.tools.javadoc.AnnotationTypeElementDocImpl; 38 import com.sun.tools.javadoc.ClassDocImpl; 39 import com.sun.tools.javadoc.ConstructorDocImpl; 40 import com.sun.tools.javadoc.DocEnv; 41 import com.sun.tools.javadoc.ExecutableMemberDocImpl; 42 import com.sun.tools.javadoc.FieldDocImpl; 43 import com.sun.tools.javadoc.MethodDocImpl; 44 import com.sun.tools.javadoc.ModifierFilter; 45 import com.sun.tools.javadoc.PackageDocImpl; 46 import java.io.IOException ; 47 import java.util.StringTokenizer ; 48 import javax.lang.model.element.Element; 49 import org.netbeans.api.java.source.CancellableTask; 50 import org.netbeans.api.java.source.ClasspathInfo; 51 import org.netbeans.api.java.source.CompilationController; 52 import org.netbeans.api.java.source.ElementHandle; 53 import org.netbeans.api.java.source.JavaSource; 54 import org.netbeans.api.java.source.JavaSource.Phase; 55 import org.netbeans.api.java.source.SourceUtils; 56 import org.openide.filesystems.FileObject; 57 import org.openide.util.Exceptions; 58 59 63 public class JavadocEnv extends DocEnv { 64 65 public static void preRegister(final Context context, final ClasspathInfo cpInfo) { 66 context.put(docEnvKey, new Context.Factory<DocEnv>() { 67 public DocEnv make() { 68 return new JavadocEnv(context, cpInfo); 69 } 70 }); 71 } 72 73 private ClasspathInfo cpInfo; 74 private Context ctx; 75 76 private JavadocEnv(Context context, ClasspathInfo cpInfo) { 77 super(context); 78 this.ctx = context; 79 this.cpInfo = cpInfo; 80 this.showAccess = new ModifierFilter(ModifierFilter.ALL_ACCESS); 81 this.legacyDoclet = false; 82 } 83 84 @Override 85 public ClassDocImpl getClassDoc(ClassSymbol clazz) { 86 ClassDocImpl result = classMap.get(clazz); 87 if (result != null) return result; 88 if (isAnnotationType(clazz)) { 89 result = new JavadocAnnotation(this, clazz); 90 } else { 91 result = new JavadocClass(this, clazz); 92 } 93 classMap.put(clazz, result); 94 return result; 95 } 96 97 @Override 98 protected void makeClassDoc(ClassSymbol clazz, String docComment, JCClassDecl tree, Position.LineMap lineMap) { 99 ClassDocImpl result = classMap.get(clazz); 100 if (result != null) { 101 if (docComment != null) result.setRawCommentText(docComment); 102 return; 103 } 104 if (isAnnotationType(tree)) { result = new JavadocAnnotation(this, clazz, docComment); 106 } else { 107 result = new JavadocClass(this, clazz, docComment); 108 } 109 classMap.put(clazz, result); 110 } 111 112 @Override 113 public FieldDocImpl getFieldDoc(VarSymbol var) { 114 FieldDocImpl result = fieldMap.get(var); 115 if (result != null) return result; 116 result = new JavadocField(this, var); 117 fieldMap.put(var, result); 118 return result; 119 } 120 121 @Override 122 protected void makeFieldDoc(VarSymbol var, String docComment, JCVariableDecl tree, Position.LineMap lineMap) { 123 FieldDocImpl result = fieldMap.get(var); 124 if (result != null) { 125 if (docComment != null) result.setRawCommentText(docComment); 126 } else { 127 result = new JavadocField(this, var, docComment); 128 fieldMap.put(var, result); 129 } 130 } 131 132 @Override 133 public MethodDocImpl getMethodDoc(MethodSymbol meth) { 134 ExecutableMemberDocImpl docImpl = methodMap.get(meth); 135 if (docImpl != null && !docImpl.isMethod()) 136 return null; 137 MethodDocImpl result = (MethodDocImpl)docImpl; 138 if (result != null) return result; 139 result = new JavadocMethod(this, meth); 140 methodMap.put(meth, result); 141 return result; 142 } 143 144 @Override 145 protected void makeMethodDoc(MethodSymbol meth, String docComment, JCMethodDecl tree, Position.LineMap lineMap) { 146 MethodDocImpl result = (MethodDocImpl)methodMap.get(meth); 147 if (result != null) { 148 if (docComment != null) result.setRawCommentText(docComment); 149 } else { 150 result = new JavadocMethod(this, meth, docComment); 151 methodMap.put(meth, result); 152 } 153 } 154 155 @Override 156 public ConstructorDocImpl getConstructorDoc(MethodSymbol meth) { 157 ConstructorDocImpl result = (ConstructorDocImpl)methodMap.get(meth); 158 if (result != null) return result; 159 result = new JavadocConstructor(this, meth); 160 methodMap.put(meth, result); 161 return result; 162 } 163 164 @Override 165 protected void makeConstructorDoc(MethodSymbol meth, String docComment, JCMethodDecl tree, Position.LineMap lineMap) { 166 ConstructorDocImpl result = (ConstructorDocImpl)methodMap.get(meth); 167 if (result != null) { 168 if (docComment != null) result.setRawCommentText(docComment); 169 } else { 170 result = new JavadocConstructor(this, meth, docComment); 171 methodMap.put(meth, result); 172 } 173 } 174 175 @Override 176 public AnnotationTypeElementDocImpl getAnnotationTypeElementDoc(MethodSymbol meth) { 177 AnnotationTypeElementDocImpl result = (AnnotationTypeElementDocImpl)methodMap.get(meth); 178 if (result != null) return result; 179 result = new JavadocAnnotationTypeElement(this, meth); 180 methodMap.put(meth, result); 181 return result; 182 } 183 184 @Override 185 protected void makeAnnotationTypeElementDoc(MethodSymbol meth, String docComment, JCMethodDecl tree, Position.LineMap lineMap) { 186 AnnotationTypeElementDocImpl result = (AnnotationTypeElementDocImpl)methodMap.get(meth); 187 if (result != null) { 188 if (docComment != null) result.setRawCommentText(docComment); 189 } else { 190 result = new JavadocAnnotationTypeElement(this, meth, docComment); 191 methodMap.put(meth, result); 192 } 193 } 194 195 199 @Override 200 public PackageDocImpl getPackageDoc(PackageSymbol pack) { 201 PackageDocImpl result = packageMap.get(pack); 202 if (result != null) return result; 203 result = new JavaDocPackage(this, pack, ctx); 204 packageMap.put(pack, result); 205 return result; 206 } 207 208 @Override 209 public ClassDocImpl lookupClass(String name) { 210 ClassDocImpl cls = super.lookupClass(name); 211 if (cls == null) 212 cls = loadClass(name); 213 return cls; 214 } 215 216 public interface ElementHolder { 217 Element getElement(); 218 } 219 220 private String getRawCommentFor(Element element) { 221 try { 222 FileObject fo = SourceUtils.getFile(element, cpInfo); 223 if (fo != null) { 224 JavaSource js = JavaSource.forFileObject(fo); 225 if (js != null) { 226 final String [] ret = new String [1]; 227 final ElementHandle<? extends Element> handle = ElementHandle.create(element); 228 js.runUserActionTask(new CancellableTask<CompilationController>() { 229 public void cancel() { 230 } 231 public void run(CompilationController controller) throws Exception { 232 controller.toPhase(Phase.ELEMENTS_RESOLVED); 233 Element e = handle.resolve(controller); 234 if (e != null) 235 ret[0] = controller.getElements().getDocComment(e); 236 } 237 },true); 238 return ret[0] != null ? ret[0] : ""; } 240 } 241 } catch (IOException ex) { 242 Exceptions.printStackTrace(ex); 243 } 244 return ""; } 246 247 private class JavadocClass extends ClassDocImpl implements ElementHolder { 248 249 private JavadocClass(DocEnv env, ClassSymbol sym) { 250 this(env, sym, null); 251 } 252 253 private JavadocClass(DocEnv env, ClassSymbol sym, String docComment) { 254 super(env, sym, docComment, null, null); 255 } 256 257 @Override 258 protected String documentation() { 259 if (documentation == null) { 260 setRawCommentText(getRawCommentFor(getElement())); 261 } 262 return documentation; 263 } 264 265 public Element getElement() { 266 return tsym; 267 } 268 } 269 270 private class JavadocAnnotation extends AnnotationTypeDocImpl implements ElementHolder { 271 272 private JavadocAnnotation(DocEnv env, ClassSymbol sym) { 273 this(env, sym, null); 274 } 275 276 private JavadocAnnotation(DocEnv env, ClassSymbol sym, String docComment) { 277 super(env, sym, docComment, null, null); 278 } 279 280 @Override 281 protected String documentation() { 282 if (documentation == null) { 283 setRawCommentText(getRawCommentFor(getElement())); 284 } 285 return documentation; 286 } 287 288 public Element getElement() { 289 return tsym; 290 } 291 } 292 293 private class JavadocField extends FieldDocImpl implements ElementHolder { 294 295 private JavadocField(DocEnv env, VarSymbol sym) { 296 this(env, sym, null); 297 } 298 299 private JavadocField(DocEnv env, VarSymbol sym, String docComment) { 300 super(env, sym, docComment, null, null); 301 } 302 303 @Override 304 protected String documentation() { 305 if (documentation == null) { 306 setRawCommentText(getRawCommentFor(getElement())); 307 } 308 return documentation; 309 } 310 311 public Element getElement() { 312 return sym; 313 } 314 } 315 316 private class JavadocMethod extends MethodDocImpl implements ElementHolder { 317 318 private JavadocMethod(DocEnv env, MethodSymbol sym) { 319 this(env, sym, null); 320 } 321 322 private JavadocMethod(DocEnv env, MethodSymbol sym, String docComment) { 323 super(env, sym, docComment, null, null); 324 } 325 326 @Override 327 protected String documentation() { 328 if (documentation == null) { 329 setRawCommentText(getRawCommentFor(getElement())); 330 } 331 return documentation; 332 } 333 334 public Element getElement() { 335 return sym; 336 } 337 } 338 339 private class JavadocConstructor extends ConstructorDocImpl implements ElementHolder { 340 341 private JavadocConstructor(DocEnv env, MethodSymbol sym) { 342 this(env, sym, null); 343 } 344 345 private JavadocConstructor(DocEnv env, MethodSymbol sym, String docComment) { 346 super(env, sym, docComment, null, null); 347 } 348 349 @Override 350 protected String documentation() { 351 if (documentation == null) { 352 setRawCommentText(getRawCommentFor(getElement())); 353 } 354 return documentation; 355 } 356 357 public Element getElement() { 358 return sym; 359 } 360 } 361 362 private class JavadocAnnotationTypeElement extends AnnotationTypeElementDocImpl implements ElementHolder { 363 364 private JavadocAnnotationTypeElement(DocEnv env, MethodSymbol sym) { 365 this(env, sym, null); 366 } 367 368 private JavadocAnnotationTypeElement(DocEnv env, MethodSymbol sym, String docComment) { 369 super(env, sym, docComment, null, null); 370 } 371 372 @Override 373 protected String documentation() { 374 if (documentation == null) { 375 setRawCommentText(getRawCommentFor(getElement())); 376 } 377 return documentation; 378 } 379 380 public Element getElement() { 381 return sym; 382 } 383 } 384 385 private class JavaDocPackage extends PackageDocImpl implements ElementHolder { 386 387 private JavaDocPackage(DocEnv env, PackageSymbol sym, Context ctx) { 388 super(env, sym); 389 } 390 391 public ClassDoc findClass(String className) { 392 Name.Table nameTable = Name.Table.instance(ctx); 393 StringTokenizer st = new StringTokenizer (className, "."); TypeSymbol s = sym; 395 while(s != null && st.hasMoreTokens()) { 396 Name clsName = nameTable.fromString(st.nextToken()); 397 com.sun.tools.javac.code.Scope.Entry e = s.members().lookup(clsName); 398 s = null; 399 while (e.scope != null) { 400 if (e.sym.kind == Kinds.TYP && (e.sym.flags_field & Flags.SYNTHETIC) == 0) { 401 s = (TypeSymbol)e.sym; 402 break; 403 } 404 e = e.next(); 405 } 406 } 407 return s instanceof ClassSymbol ? env.getClassDoc((ClassSymbol)s) : null; 408 } 409 410 public Element getElement() { 411 return sym; 412 } 413 } 414 } 415 | Popular Tags |