/* * 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 org.clearsilver.HDF; import java.util.*; public class MethodInfo extends MemberInfo { public static final Comparator comparator = new Comparator() { public int compare(MethodInfo a, MethodInfo b) { return a.name().compareTo(b.name()); } }; private class InlineTags implements InheritedTags { public TagInfo[] tags() { return comment().tags(); } public InheritedTags inherited() { MethodInfo m = findOverriddenMethod(name(), signature()); if (m != null) { return m.inlineTags(); } else { return null; } } } private static void addInterfaces(ClassInfo[] ifaces, ArrayList queue) { for (ClassInfo i: ifaces) { queue.add(i); } for (ClassInfo i: ifaces) { addInterfaces(i.interfaces(), queue); } } // first looks for a superclass, and then does a breadth first search to // find the least far away match public MethodInfo findOverriddenMethod(String name, String signature) { if (mReturnType == null) { // ctor return null; } if (mOverriddenMethod != null) { return mOverriddenMethod; } ArrayList queue = new ArrayList(); addInterfaces(containingClass().interfaces(), queue); for (ClassInfo iface: queue) { for (MethodInfo me: iface.methods()) { if (me.name().equals(name) && me.signature().equals(signature) && me.inlineTags().tags() != null && me.inlineTags().tags().length > 0) { return me; } } } return null; } private static void addRealInterfaces(ClassInfo[] ifaces, ArrayList queue) { for (ClassInfo i: ifaces) { queue.add(i); if (i.realSuperclass() != null && i.realSuperclass().isAbstract()) { queue.add(i.superclass()); } } for (ClassInfo i: ifaces) { addInterfaces(i.realInterfaces(), queue); } } public MethodInfo findRealOverriddenMethod(String name, String signature, HashSet notStrippable) { if (mReturnType == null) { // ctor return null; } if (mOverriddenMethod != null) { return mOverriddenMethod; } ArrayList queue = new ArrayList(); if (containingClass().realSuperclass() != null && containingClass().realSuperclass().isAbstract()) { queue.add(containingClass()); } addInterfaces(containingClass().realInterfaces(), queue); for (ClassInfo iface: queue) { for (MethodInfo me: iface.methods()) { if (me.name().equals(name) && me.signature().equals(signature) && me.inlineTags().tags() != null && me.inlineTags().tags().length > 0 && notStrippable.contains(me.containingClass())) { return me; } } } return null; } public MethodInfo findSuperclassImplementation(HashSet notStrippable) { if (mReturnType == null) { // ctor return null; } if (mOverriddenMethod != null) { // Even if we're told outright that this was the overridden method, we want to // be conservative and ignore mismatches of parameter types -- they arise from // extending generic specializations, and we want to consider the derived-class // method to be a non-override. if (this.signature().equals(mOverriddenMethod.signature())) { return mOverriddenMethod; } } ArrayList queue = new ArrayList(); if (containingClass().realSuperclass() != null && containingClass().realSuperclass().isAbstract()) { queue.add(containingClass()); } addInterfaces(containingClass().realInterfaces(), queue); for (ClassInfo iface: queue) { for (MethodInfo me: iface.methods()) { if (me.name().equals(this.name()) && me.signature().equals(this.signature()) && notStrippable.contains(me.containingClass())) { return me; } } } return null; } public ClassInfo findRealOverriddenClass(String name, String signature) { if (mReturnType == null) { // ctor return null; } if (mOverriddenMethod != null) { return mOverriddenMethod.mRealContainingClass; } ArrayList queue = new ArrayList(); if (containingClass().realSuperclass() != null && containingClass().realSuperclass().isAbstract()) { queue.add(containingClass()); } addInterfaces(containingClass().realInterfaces(), queue); for (ClassInfo iface: queue) { for (MethodInfo me: iface.methods()) { if (me.name().equals(name) && me.signature().equals(signature) && me.inlineTags().tags() != null && me.inlineTags().tags().length > 0) { return iface; } } } return null; } private class FirstSentenceTags implements InheritedTags { public TagInfo[] tags() { return comment().briefTags(); } public InheritedTags inherited() { MethodInfo m = findOverriddenMethod(name(), signature()); if (m != null) { return m.firstSentenceTags(); } else { return null; } } } private class ReturnTags implements InheritedTags { public TagInfo[] tags() { return comment().returnTags(); } public InheritedTags inherited() { MethodInfo m = findOverriddenMethod(name(), signature()); if (m != null) { return m.returnTags(); } else { return null; } } } public boolean isDeprecated() { boolean deprecated = false; if (!mDeprecatedKnown) { boolean commentDeprecated = (comment().deprecatedTags().length > 0); boolean annotationDeprecated = false; for (AnnotationInstanceInfo annotation : annotations()) { if (annotation.type().qualifiedName().equals("java.lang.Deprecated")) { annotationDeprecated = true; break; } } if (commentDeprecated != annotationDeprecated) { Errors.error(Errors.DEPRECATION_MISMATCH, position(), "Method " + mContainingClass.qualifiedName() + "." + name() + ": @Deprecated annotation and @deprecated doc tag do not match"); } mIsDeprecated = commentDeprecated | annotationDeprecated; mDeprecatedKnown = true; } return mIsDeprecated; } public TypeInfo[] getTypeParameters(){ return mTypeParameters; } public MethodInfo cloneForClass(ClassInfo newContainingClass) { MethodInfo result = new MethodInfo(getRawCommentText(), mTypeParameters, name(), signature(), newContainingClass, realContainingClass(), isPublic(), isProtected(), isPackagePrivate(), isPrivate(), isFinal(), isStatic(), isSynthetic(), mIsAbstract, mIsSynchronized, mIsNative, mIsAnnotationElement, kind(), mFlatSignature, mOverriddenMethod, mReturnType, mParameters, mThrownExceptions, position(), annotations()); result.init(mDefaultAnnotationElementValue); return result; } public MethodInfo(String rawCommentText, TypeInfo[] typeParameters, String name, String signature, ClassInfo containingClass, ClassInfo realContainingClass, boolean isPublic, boolean isProtected, boolean isPackagePrivate, boolean isPrivate, boolean isFinal, boolean isStatic, boolean isSynthetic, boolean isAbstract, boolean isSynchronized, boolean isNative, boolean isAnnotationElement, String kind, String flatSignature, MethodInfo overriddenMethod, TypeInfo returnType, ParameterInfo[] parameters, ClassInfo[] thrownExceptions, SourcePositionInfo position, AnnotationInstanceInfo[] annotations) { // Explicitly coerce 'final' state of Java6-compiled enum values() method, to match // the Java5-emitted base API description. super(rawCommentText, name, signature, containingClass, realContainingClass, isPublic, isProtected, isPackagePrivate, isPrivate, ((name.equals("values") && containingClass.isEnum()) ? true : isFinal), isStatic, isSynthetic, kind, position, annotations); // The underlying MethodDoc for an interface's declared methods winds up being marked // non-abstract. Correct that here by looking at the immediate-parent class, and marking // this method abstract if it is an unimplemented interface method. if (containingClass.isInterface()) { isAbstract = true; } mReasonOpened = "0:0"; mIsAnnotationElement = isAnnotationElement; mTypeParameters = typeParameters; mIsAbstract = isAbstract; mIsSynchronized = isSynchronized; mIsNative = isNative; mFlatSignature = flatSignature; mOverriddenMethod = overriddenMethod; mReturnType = returnType; mParameters = parameters; mThrownExceptions = thrownExceptions; } public void init(AnnotationValueInfo defaultAnnotationElementValue) { mDefaultAnnotationElementValue = defaultAnnotationElementValue; } public boolean isAbstract() { return mIsAbstract; } public boolean isSynchronized() { return mIsSynchronized; } public boolean isNative() { return mIsNative; } public String flatSignature() { return mFlatSignature; } public InheritedTags inlineTags() { return new InlineTags(); } public InheritedTags firstSentenceTags() { return new FirstSentenceTags(); } public InheritedTags returnTags() { return new ReturnTags(); } public TypeInfo returnType() { return mReturnType; } public String prettySignature() { String s = "("; int N = mParameters.length; for (int i=0; i rv = new ArrayList(); int len = documented.length; for (int i=0; i= 0) { comments[index] = tag.parameterComment(); positions[index] = tag.position(); } else { Errors.error(Errors.UNKNOWN_PARAM_TAG_NAME, tag.position(), "@param tag with name that doesn't match the parameter list: '" + tag.parameterName() + "'"); } } // get our parent's tags to fill in the blanks MethodInfo overridden = this.findOverriddenMethod(name(), signature()); if (overridden != null) { ParamTagInfo[] maternal = overridden.paramTags(); for (int i=0; i typeVariables() { HashSet result = TypeInfo.typeVariables(mTypeParameters); ClassInfo cl = containingClass(); while (cl != null) { TypeInfo[] types = cl.asTypeInfo().typeArguments(); if (types != null) { TypeInfo.typeVariables(types, result); } cl = cl.containingClass(); } return result; } @Override public boolean isExecutable() { return true; } public ClassInfo[] thrownExceptions() { return mThrownExceptions; } public String typeArgumentsName(HashSet typeVars) { if (mTypeParameters == null || mTypeParameters.length == 0) { return ""; } else { return TypeInfo.typeArgumentsName(mTypeParameters, typeVars); } } public boolean isAnnotationElement() { return mIsAnnotationElement; } public AnnotationValueInfo defaultAnnotationElementValue() { return mDefaultAnnotationElementValue; } public void setVarargs(boolean set){ mIsVarargs = set; } public boolean isVarArgs(){ return mIsVarargs; } @Override public String toString(){ return this.name(); } public void setReason(String reason) { mReasonOpened = reason; } public String getReason() { return mReasonOpened; } private String mFlatSignature; private MethodInfo mOverriddenMethod; private TypeInfo mReturnType; private boolean mIsAnnotationElement; private boolean mIsAbstract; private boolean mIsSynchronized; private boolean mIsNative; private boolean mIsVarargs; private boolean mDeprecatedKnown; private boolean mIsDeprecated; private ParameterInfo[] mParameters; private ClassInfo[] mThrownExceptions; private String[] mParamStrings; ThrowsTagInfo[] mThrowsTags; private ParamTagInfo[] mParamTags; private TypeInfo[] mTypeParameters; private AnnotationValueInfo mDefaultAnnotationElementValue; private String mReasonOpened; }