/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import com.sun.javadoc.*; import com.sun.tools.doclets.*; import org.clearsilver.HDF; import org.clearsilver.CS; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; public class Converter { private static RootDoc root; public static void makeInfo(RootDoc r) { root = r; int N, i; // create the objects ClassDoc[] classDocs = r.classes(); N = classDocs.length; for (i=0; i classesNeedingInit2 = new ArrayList(); // fill in the fields that reference other classes while (mClassesNeedingInit.size() > 0) { i = mClassesNeedingInit.size()-1; ClassNeedingInit clni = mClassesNeedingInit.get(i); mClassesNeedingInit.remove(i); initClass(clni.c, clni.cl); classesNeedingInit2.add(clni.cl); } mClassesNeedingInit = null; for (ClassInfo cl: classesNeedingInit2) { cl.init2(); } finishAnnotationValueInit(); // fill in the "root" stuff mRootClasses = Converter.convertClasses(r.classes()); } private static ClassInfo[] mRootClasses; public static ClassInfo[] rootClasses() { return mRootClasses; } public static ClassInfo[] allClasses() { return (ClassInfo[])mClasses.all(); } private static void initClass(ClassDoc c, ClassInfo cl) { MethodDoc[] annotationElements; if (c instanceof AnnotationTypeDoc) { annotationElements = ((AnnotationTypeDoc)c).elements(); } else { annotationElements = new MethodDoc[0]; } cl.init(Converter.obtainType(c), Converter.convertClasses(c.interfaces()), Converter.convertTypes(c.interfaceTypes()), Converter.convertClasses(c.innerClasses()), Converter.convertMethods(c.constructors(false)), Converter.convertMethods(c.methods(false)), Converter.convertMethods(annotationElements), Converter.convertFields(c.fields(false)), Converter.convertFields(c.enumConstants()), Converter.obtainPackage(c.containingPackage()), Converter.obtainClass(c.containingClass()), Converter.obtainClass(c.superclass()), Converter.obtainType(c.superclassType()), Converter.convertAnnotationInstances(c.annotations()) ); cl.setHiddenMethods(Converter.getHiddenMethods(c.methods(false))); cl.setNonWrittenConstructors(Converter.convertNonWrittenConstructors(c.constructors(false))); cl.init3(Converter.convertTypes(c.typeParameters()), Converter.convertClasses(c.innerClasses(false))); } public static ClassInfo obtainClass(String className) { return Converter.obtainClass(root.classNamed(className)); } public static PackageInfo obtainPackage(String packageName) { return Converter.obtainPackage(root.packageNamed(packageName)); } private static TagInfo convertTag(Tag tag) { return new TextTagInfo(tag.name(), tag.kind(), tag.text(), Converter.convertSourcePosition(tag.position())); } private static ThrowsTagInfo convertThrowsTag(ThrowsTag tag, ContainerInfo base) { return new ThrowsTagInfo(tag.name(), tag.text(), tag.kind(), Converter.obtainClass(tag.exception()), tag.exceptionComment(), base, Converter.convertSourcePosition(tag.position())); } private static ParamTagInfo convertParamTag(ParamTag tag, ContainerInfo base) { return new ParamTagInfo(tag.name(), tag.kind(), tag.text(), tag.isTypeParameter(), tag.parameterComment(), tag.parameterName(), base, Converter.convertSourcePosition(tag.position())); } private static SeeTagInfo convertSeeTag(SeeTag tag, ContainerInfo base) { return new SeeTagInfo(tag.name(), tag.kind(), tag.text(), base, Converter.convertSourcePosition(tag.position())); } private static SourcePositionInfo convertSourcePosition(SourcePosition sp) { if (sp == null) { return null; } return new SourcePositionInfo(sp.file().toString(), sp.line(), sp.column()); } public static TagInfo[] convertTags(Tag[] tags, ContainerInfo base) { int len = tags.length; TagInfo[] out = new TagInfo[len]; for (int i=0; i mClassesNeedingInit = new ArrayList(); static ClassInfo obtainClass(ClassDoc o) { return (ClassInfo)mClasses.obtain(o); } private static Cache mClasses = new Cache() { @Override protected Object make(Object o) { ClassDoc c = (ClassDoc)o; ClassInfo cl = new ClassInfo( c, c.getRawCommentText(), Converter.convertSourcePosition(c.position()), c.isPublic(), c.isProtected(), c.isPackagePrivate(), c.isPrivate(), c.isStatic(), c.isInterface(), c.isAbstract(), c.isOrdinaryClass(), c.isException(), c.isError(), c.isEnum(), (c instanceof AnnotationTypeDoc), c.isFinal(), c.isIncluded(), c.name(), c.qualifiedName(), c.qualifiedTypeName(), c.isPrimitive()); if (mClassesNeedingInit != null) { mClassesNeedingInit.add(new ClassNeedingInit(c, cl)); } return cl; } @Override protected void made(Object o, Object r) { if (mClassesNeedingInit == null) { initClass((ClassDoc)o, (ClassInfo)r); ((ClassInfo)r).init2(); } } @Override ClassInfo[] all() { return (ClassInfo[])mCache.values().toArray(new ClassInfo[mCache.size()]); } }; private static MethodInfo[] getHiddenMethods(MethodDoc[] methods){ if (methods == null) return null; ArrayList out = new ArrayList(); int N = methods.length; for (int i=0; i out = new ArrayList(); int N = methods.length; for (int i=0; i out = new ArrayList(); int N = methods.length; for (int i=0; i out = new ArrayList(); int N = methods.length; for (int i=0; i out = new ArrayList(); int N = fields.length; for (int i=0; i mCache = new HashMap(); protected abstract Object make(Object o); protected void made(Object o, Object r) { } protected Object keyFor(Object o) { return o; } Object[] all() { return null; } } // annotation values private static HashMap mAnnotationValues = new HashMap(); private static HashSet mAnnotationValuesNeedingInit = new HashSet(); private static AnnotationValueInfo obtainAnnotationValue(AnnotationValue o, MethodInfo element) { if (o == null) { return null; } AnnotationValueInfo v = mAnnotationValues.get(o); if (v != null) return v; v = new AnnotationValueInfo(element); mAnnotationValues.put(o, v); if (mAnnotationValuesNeedingInit != null) { mAnnotationValuesNeedingInit.add(o); } else { initAnnotationValue(o, v); } return v; } private static void initAnnotationValue(AnnotationValue o, AnnotationValueInfo v) { Object orig = o.value(); Object converted; if (orig instanceof Type) { // class literal converted = Converter.obtainType((Type)orig); } else if (orig instanceof FieldDoc) { // enum constant converted = Converter.obtainField((FieldDoc)orig); } else if (orig instanceof AnnotationDesc) { // annotation instance converted = Converter.obtainAnnotationInstance((AnnotationDesc)orig); } else if (orig instanceof AnnotationValue[]) { AnnotationValue[] old = (AnnotationValue[])orig; AnnotationValueInfo[] array = new AnnotationValueInfo[old.length]; for (int i=0; i 0) { HashSet set = mAnnotationValuesNeedingInit; mAnnotationValuesNeedingInit = new HashSet(); for (AnnotationValue o: set) { AnnotationValueInfo v = mAnnotationValues.get(o); initAnnotationValue(o, v); } depth++; } mAnnotationValuesNeedingInit = null; } }