Class Method - java.lang.Object - Oracle Help Center

文章推薦指數: 80 %
投票人數:10人

A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance ... Skipnavigationlinks Overview Package Class Use Tree Deprecated Index Help Java™ PlatformStandard Ed. 8 Prev Class Next Class Frames No Frames All Classes Summary:  Nested |  Field |  Constr |  Method Detail:  Field |  Constr |  Method java.lang.Object java.lang.reflect.AccessibleObject java.lang.reflect.Executable java.lang.reflect.Method AllImplementedInterfaces: AnnotatedElement,GenericDeclaration,Member publicfinalclassMethod extendsExecutable AMethodprovidesinformationabout,andaccessto,asinglemethod onaclassorinterface.Thereflectedmethodmaybeaclassmethod oraninstancemethod(includinganabstractmethod). AMethodpermitswideningconversionstooccurwhenmatchingthe actualparameterstoinvokewiththeunderlyingmethod'sformal parameters,butitthrowsanIllegalArgumentExceptionifa narrowingconversionwouldoccur. SeeAlso: Member, Class, Class.getMethods(), Class.getMethod(String,Class[]), Class.getDeclaredMethods(), Class.getDeclaredMethod(String,Class[]) FieldSummary Fieldsinheritedfrominterface java.lang.reflect.Member DECLARED,PUBLIC MethodSummary AllMethods InstanceMethods ConcreteMethods  ModifierandType MethodandDescription boolean equals(Object obj) ComparesthisMethodagainstthespecifiedobject. AnnotatedType getAnnotatedReturnType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofatypeto specifythereturntypeofthemethod/constructorrepresentedbythis Executable. T getAnnotation(Class annotationClass) Returnsthiselement'sannotationforthespecifiedtypeif suchanannotationispresent,elsenull. Annotation[] getDeclaredAnnotations() Returnsannotationsthataredirectlypresentonthiselement. Class> getDeclaringClass() ReturnstheClassobjectrepresentingtheclassorinterface thatdeclarestheexecutablerepresentedbythisobject. Object getDefaultValue() Returnsthedefaultvaluefortheannotationmemberrepresentedby thisMethodinstance. Class>[] getExceptionTypes() ReturnsanarrayofClassobjectsthatrepresentthe typesofexceptionsdeclaredtobethrownbytheunderlying executablerepresentedbythisobject. Type[] getGenericExceptionTypes() ReturnsanarrayofTypeobjectsthatrepresentthe exceptionsdeclaredtobethrownbythisexecutableobject. Type[] getGenericParameterTypes() ReturnsanarrayofTypeobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutablerepresentedby thisobject. Type getGenericReturnType() ReturnsaTypeobjectthatrepresentstheformalreturn typeofthemethodrepresentedbythisMethodobject. int getModifiers() ReturnstheJavalanguagemodifiersfor theexecutablerepresentedbythisobject. String getName() ReturnsthenameofthemethodrepresentedbythisMethod object,asaString. Annotation[][] getParameterAnnotations() ReturnsanarrayofarraysofAnnotationsthat representtheannotationsontheformalparameters,in declarationorder,oftheExecutablerepresentedby thisobject. int getParameterCount() Returnsthenumberofformalparameters(whetherexplicitly declaredorimplicitlydeclaredorneither)fortheexecutable representedbythisobject. Class>[] getParameterTypes() ReturnsanarrayofClassobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutable representedbythisobject. Class> getReturnType() ReturnsaClassobjectthatrepresentstheformalreturntype ofthemethodrepresentedbythisMethodobject. TypeVariable[] getTypeParameters() ReturnsanarrayofTypeVariableobjectsthatrepresentthe typevariablesdeclaredbythegenericdeclarationrepresentedbythis GenericDeclarationobject,indeclarationorder. int hashCode() ReturnsahashcodeforthisMethod. Object invoke(Object obj, Object... args) InvokestheunderlyingmethodrepresentedbythisMethod object,onthespecifiedobjectwiththespecifiedparameters. boolean isBridge() Returnstrueifthismethodisabridge method;returnsfalseotherwise. boolean isDefault() Returnstrueifthismethodisadefault method;returnsfalseotherwise. boolean isSynthetic() Returnstrueifthisexecutableisasynthetic construct;returnsfalseotherwise. boolean isVarArgs() Returnstrueifthisexecutablewasdeclaredtotakea variablenumberofarguments;returnsfalseotherwise. String toGenericString() ReturnsastringdescribingthisMethod,including typeparameters. String toString() ReturnsastringdescribingthisMethod. Methodsinheritedfromclass java.lang.reflect.Executable getAnnotatedExceptionTypes,getAnnotatedParameterTypes,getAnnotatedReceiverType,getAnnotationsByType,getParameters Methodsinheritedfromclass java.lang.reflect.AccessibleObject getAnnotations,getDeclaredAnnotation,getDeclaredAnnotationsByType,isAccessible,isAnnotationPresent,setAccessible,setAccessible Methodsinheritedfromclass java.lang.Object clone,finalize,getClass,notify,notifyAll,wait,wait,wait Methodsinheritedfrominterface java.lang.reflect.AnnotatedElement getAnnotations,getDeclaredAnnotation,getDeclaredAnnotationsByType,isAnnotationPresent MethodDetail getDeclaringClass public Class> getDeclaringClass() ReturnstheClassobjectrepresentingtheclassorinterface thatdeclarestheexecutablerepresentedbythisobject. Specifiedby: getDeclaringClass ininterface Member Specifiedby: getDeclaringClass inclass Executable Returns: anobjectrepresentingthedeclaringclassofthe underlyingmember getName public String getName() ReturnsthenameofthemethodrepresentedbythisMethod object,asaString. Specifiedby: getName ininterface Member Specifiedby: getName inclass Executable Returns: thesimplenameoftheunderlyingmember getModifiers public int getModifiers() ReturnstheJavalanguagemodifiersfor theexecutablerepresentedbythisobject. Specifiedby: getModifiers ininterface Member Specifiedby: getModifiers inclass Executable Returns: theJavalanguagemodifiersfortheunderlyingmember SeeAlso: Modifier getTypeParameters public TypeVariable[] getTypeParameters() ReturnsanarrayofTypeVariableobjectsthatrepresentthe typevariablesdeclaredbythegenericdeclarationrepresentedbythis GenericDeclarationobject,indeclarationorder.Returnsan arrayoflength0iftheunderlyinggenericdeclarationdeclaresnotype variables. Specifiedby: getTypeParameters ininterface GenericDeclaration Specifiedby: getTypeParameters inclass Executable Returns: anarrayofTypeVariableobjectsthatrepresent thetypevariablesdeclaredbythisgenericdeclaration Throws: GenericSignatureFormatError-ifthegeneric signatureofthisgenericdeclarationdoesnotconformto theformatspecifiedin TheJava™VirtualMachineSpecification Since: 1.5 getReturnType public Class> getReturnType() ReturnsaClassobjectthatrepresentstheformalreturntype ofthemethodrepresentedbythisMethodobject. Returns: thereturntypeforthemethodthisobjectrepresents getGenericReturnType public Type getGenericReturnType() ReturnsaTypeobjectthatrepresentstheformalreturn typeofthemethodrepresentedbythisMethodobject. Ifthereturntypeisaparameterizedtype, theTypeobjectreturnedmustaccuratelyreflect theactualtypeparametersusedinthesourcecode. Ifthereturntypeisatypevariableoraparameterizedtype,it iscreated.Otherwise,itisresolved. Returns: aTypeobjectthatrepresentstheformalreturn typeoftheunderlyingmethod Throws: GenericSignatureFormatError-ifthegenericmethodsignaturedoesnotconformtotheformat specifiedin TheJava™VirtualMachineSpecification TypeNotPresentException-iftheunderlyingmethod's returntypereferstoanon-existenttypedeclaration MalformedParameterizedTypeException-ifthe underlyingmethod'sreturntypedreferstoaparameterized typethatcannotbeinstantiatedforanyreason Since: 1.5 getParameterTypes public Class>[] getParameterTypes() ReturnsanarrayofClassobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutable representedbythisobject.Returnsanarrayoflength 0iftheunderlyingexecutabletakesnoparameters. Specifiedby: getParameterTypes inclass Executable Returns: theparametertypesfortheexecutablethisobject represents getParameterCount public int getParameterCount() Returnsthenumberofformalparameters(whetherexplicitly declaredorimplicitlydeclaredorneither)fortheexecutable representedbythisobject. Overrides: getParameterCount inclass Executable Returns: Thenumberofformalparametersfortheexecutablethis objectrepresents Since: 1.8 getGenericParameterTypes public Type[] getGenericParameterTypes() ReturnsanarrayofTypeobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutablerepresentedby thisobject.Returnsanarrayoflength0ifthe underlyingexecutabletakesnoparameters. Ifaformalparametertypeisaparameterizedtype, theTypeobjectreturnedforitmustaccuratelyreflect theactualtypeparametersusedinthesourcecode. Ifaformalparametertypeisatypevariableoraparameterized type,itiscreated.Otherwise,itisresolved. Overrides: getGenericParameterTypes inclass Executable Returns: anarrayofTypesthatrepresenttheformal parametertypesoftheunderlyingexecutable,indeclarationorder Throws: GenericSignatureFormatError-ifthegenericmethodsignaturedoesnotconformtotheformat specifiedin TheJava™VirtualMachineSpecification TypeNotPresentException-ifanyoftheparameter typesoftheunderlyingexecutablereferstoanon-existenttype declaration MalformedParameterizedTypeException-ifanyof theunderlyingexecutable'sparametertypesrefertoaparameterized typethatcannotbeinstantiatedforanyreason Since: 1.5 getExceptionTypes public Class>[] getExceptionTypes() ReturnsanarrayofClassobjectsthatrepresentthe typesofexceptionsdeclaredtobethrownbytheunderlying executablerepresentedbythisobject.Returnsanarrayof length0iftheexecutabledeclaresnoexceptionsinitsthrowsclause. Specifiedby: getExceptionTypes inclass Executable Returns: theexceptiontypesdeclaredasbeingthrownbythe executablethisobjectrepresents getGenericExceptionTypes public Type[] getGenericExceptionTypes() ReturnsanarrayofTypeobjectsthatrepresentthe exceptionsdeclaredtobethrownbythisexecutableobject. Returnsanarrayoflength0iftheunderlyingexecutabledeclares noexceptionsinitsthrowsclause. Ifanexceptiontypeisatypevariableoraparameterized type,itiscreated.Otherwise,itisresolved. Overrides: getGenericExceptionTypes inclass Executable Returns: anarrayofTypesthatrepresenttheexceptiontypes thrownbytheunderlyingexecutable Throws: GenericSignatureFormatError-ifthegenericmethodsignaturedoesnotconformtotheformat specifiedin TheJava™VirtualMachineSpecification TypeNotPresentException-iftheunderlyingexecutable's throwsclausereferstoanon-existenttypedeclaration MalformedParameterizedTypeException-if theunderlyingexecutable'sthrowsclausereferstoa parameterizedtypethatcannotbeinstantiatedforanyreason Since: 1.5 equals public boolean equals(Object obj) ComparesthisMethodagainstthespecifiedobject.Returns trueiftheobjectsarethesame.TwoMethodsarethesameif theyweredeclaredbythesameclassandhavethesamename andformalparametertypesandreturntype. Overrides: equals inclass Object Parameters: obj-thereferenceobjectwithwhichtocompare. Returns: trueifthisobjectisthesameastheobj argument;falseotherwise. SeeAlso: Object.hashCode(), HashMap hashCode public int hashCode() ReturnsahashcodeforthisMethod.Thehashcodeiscomputed astheexclusive-orofthehashcodesfortheunderlying method'sdeclaringclassnameandthemethod'sname. Overrides: hashCode inclass Object Returns: ahashcodevalueforthisobject. SeeAlso: Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object) toString public String toString() ReturnsastringdescribingthisMethod.Thestringis formattedasthemethodaccessmodifiers,ifany,followedby themethodreturntype,followedbyaspace,followedbythe classdeclaringthemethod,followedbyaperiod,followedby themethodname,followedbyaparenthesized,comma-separated listofthemethod'sformalparametertypes.Ifthemethod throwscheckedexceptions,theparameterlistisfollowedbya space,followedbythewordthrowsfollowedbya comma-separatedlistofthethrownexceptiontypes. Forexample: publicbooleanjava.lang.Object.equals(java.lang.Object) Theaccessmodifiersareplacedincanonicalorderas specifiedby"TheJavaLanguageSpecification".Thisis public,protectedorprivatefirst, andthenothermodifiersinthefollowingorder: abstract,default,static,final, synchronized,native,strictfp. Overrides: toString inclass Object Returns: astringdescribingthisMethod SeeTheJava™LanguageSpecification: 8.4.3MethodModifiers toGenericString public String toGenericString() ReturnsastringdescribingthisMethod,including typeparameters.Thestringisformattedasthemethodaccess modifiers,ifany,followedbyanangle-bracketed comma-separatedlistofthemethod'stypeparameters,ifany, followedbythemethod'sgenericreturntype,followedbya space,followedbytheclassdeclaringthemethod,followedby aperiod,followedbythemethodname,followedbya parenthesized,comma-separatedlistofthemethod'sgeneric formalparametertypes. Ifthismethodwasdeclaredtotakeavariablenumberof arguments,insteadofdenotingthelastparameteras "Type[]",itisdenotedas "Type...". Aspaceisusedtoseparateaccessmodifiersfromoneanother andfromthetypeparametersorreturntype.Ifthereareno typeparameters,thetypeparameterlistiselided;ifthetype parameterlistispresent,aspaceseparatesthelistfromthe classname.Ifthemethodisdeclaredtothrowexceptions,the parameterlistisfollowedbyaspace,followedbytheword throwsfollowedbyacomma-separatedlistofthegenericthrown exceptiontypes. Theaccessmodifiersareplacedincanonicalorderas specifiedby"TheJavaLanguageSpecification".Thisis public,protectedorprivatefirst, andthenothermodifiersinthefollowingorder: abstract,default,static,final, synchronized,native,strictfp. Specifiedby: toGenericString inclass Executable Returns: astringdescribingthisMethod, includetypeparameters Since: 1.5 SeeTheJava™LanguageSpecification: 8.4.3MethodModifiers invoke public Object invoke(Object obj, Object... args) throwsIllegalAccessException, IllegalArgumentException, InvocationTargetException InvokestheunderlyingmethodrepresentedbythisMethod object,onthespecifiedobjectwiththespecifiedparameters. Individualparametersareautomaticallyunwrappedtomatch primitiveformalparameters,andbothprimitiveandreference parametersaresubjecttomethodinvocationconversionsas necessary. Iftheunderlyingmethodisstatic,thenthespecifiedobj argumentisignored.Itmaybenull. Ifthenumberofformalparametersrequiredbytheunderlyingmethodis 0,thesuppliedargsarraymaybeoflength0ornull. Iftheunderlyingmethodisaninstancemethod,itisinvoked usingdynamicmethodlookupasdocumentedinTheJavaLanguage Specification,SecondEdition,section15.12.4.4;inparticular, overridingbasedontheruntimetypeofthetargetobjectwilloccur. Iftheunderlyingmethodisstatic,theclassthatdeclared themethodisinitializedifithasnotalreadybeeninitialized. Ifthemethodcompletesnormally,thevalueitreturnsis returnedtothecallerofinvoke;ifthevaluehasaprimitive type,itisfirstappropriatelywrappedinanobject.However, ifthevaluehasthetypeofanarrayofaprimitivetype,the elementsofthearrayarenotwrappedinobjects;in otherwords,anarrayofprimitivetypeisreturned.Ifthe underlyingmethodreturntypeisvoid,theinvocationreturns null. Parameters: obj-theobjecttheunderlyingmethodisinvokedfrom args-theargumentsusedforthemethodcall Returns: theresultofdispatchingthemethodrepresentedby thisobjectonobjwithparameters args Throws: IllegalAccessException-ifthisMethodobject isenforcingJavalanguageaccesscontrolandtheunderlying methodisinaccessible. IllegalArgumentException-ifthemethodisan instancemethodandthespecifiedobjectargument isnotaninstanceoftheclassorinterface declaringtheunderlyingmethod(orofasubclass orimplementorthereof);ifthenumberofactual andformalparametersdiffer;ifanunwrapping conversionforprimitiveargumentsfails;orif, afterpossibleunwrapping,aparametervalue cannotbeconvertedtothecorrespondingformal parametertypebyamethodinvocationconversion. InvocationTargetException-iftheunderlyingmethod throwsanexception. NullPointerException-ifthespecifiedobjectisnull andthemethodisaninstancemethod. ExceptionInInitializerError-iftheinitialization provokedbythismethodfails. isBridge public boolean isBridge() Returnstrueifthismethodisabridge method;returnsfalseotherwise. Returns: trueifandonlyifthismethodisabridge methodasdefinedbytheJavaLanguageSpecification. Since: 1.5 isVarArgs public boolean isVarArgs() Returnstrueifthisexecutablewasdeclaredtotakea variablenumberofarguments;returnsfalseotherwise. Overrides: isVarArgs inclass Executable Returns: trueifanonlyifthisexecutablewasdeclared totakeavariablenumberofarguments. Since: 1.5 isSynthetic public boolean isSynthetic() Returnstrueifthisexecutableisasynthetic construct;returnsfalseotherwise. Specifiedby: isSynthetic ininterface Member Overrides: isSynthetic inclass Executable Returns: trueifandonlyifthisexecutableisasynthetic constructasdefinedby TheJava™LanguageSpecification. Since: 1.5 SeeTheJava™LanguageSpecification: 13.1TheFormofaBinary isDefault public boolean isDefault() Returnstrueifthismethodisadefault method;returnsfalseotherwise. Adefaultmethodisapublicnon-abstractinstancemethod,that is,anon-staticmethodwithabody,declaredinaninterface type. Returns: trueifandonlyifthismethodisadefault methodasdefinedbytheJavaLanguageSpecification. Since: 1.8 getDefaultValue public Object getDefaultValue() Returnsthedefaultvaluefortheannotationmemberrepresentedby thisMethodinstance.Ifthememberisofaprimitivetype, aninstanceofthecorrespondingwrappertypeisreturned.Returns nullifnodefaultisassociatedwiththemember,orifthemethod instancedoesnotrepresentadeclaredmemberofanannotationtype. Returns: thedefaultvaluefortheannotationmemberrepresented bythisMethodinstance. Throws: TypeNotPresentException-iftheannotationisoftype Classandnodefinitioncanbefoundforthe defaultclassvalue. Since: 1.5 getAnnotation public  T getAnnotation(Class annotationClass) Returnsthiselement'sannotationforthespecifiedtypeif suchanannotationispresent,elsenull. Specifiedby: getAnnotation ininterface AnnotatedElement Overrides: getAnnotation inclass Executable TypeParameters: T-thetypeoftheannotationtoqueryforandreturnifpresent Parameters: annotationClass-theClassobjectcorrespondingtothe annotationtype Returns: thiselement'sannotationforthespecifiedannotationtypeif presentonthiselement,elsenull Throws: NullPointerException-ifthegivenannotationclassisnull Since: 1.5 getDeclaredAnnotations public Annotation[] getDeclaredAnnotations() Returnsannotationsthataredirectlypresentonthiselement. Thismethodignoresinheritedannotations. Iftherearenoannotationsdirectlypresentonthiselement, thereturnvalueisanarrayoflength0. Thecallerofthismethodisfreetomodifythereturnedarray;itwill havenoeffectonthearraysreturnedtoothercallers. Specifiedby: getDeclaredAnnotations ininterface AnnotatedElement Overrides: getDeclaredAnnotations inclass Executable Returns: annotationsdirectlypresentonthiselement Since: 1.5 getParameterAnnotations public Annotation[][] getParameterAnnotations() ReturnsanarrayofarraysofAnnotationsthat representtheannotationsontheformalparameters,in declarationorder,oftheExecutablerepresentedby thisobject.Syntheticandmandatedparameters(see explanationbelow),suchastheouter"this"parametertoan innerclassconstructorwillberepresentedinthereturned array.Iftheexecutablehasnoparameters(meaningnoformal, nosynthetic,andnomandatedparameters),azero-lengtharray willbereturned.IftheExecutablehasoneormore parameters,anestedarrayoflengthzeroisreturnedforeach parameterwithnoannotations.Theannotationobjectscontained inthereturnedarraysareserializable.Thecallerofthis methodisfreetomodifythereturnedarrays;itwillhaveno effectonthearraysreturnedtoothercallers. Acompilermayaddextraparametersthatareimplicitly declaredinsource("mandated"),aswellasparametersthat areneitherimplicitlynorexplicitlydeclaredinsource ("synthetic")totheparameterlistforamethod.SeeParameterformoreinformation. Specifiedby: getParameterAnnotations inclass Executable Returns: anarrayofarraysthatrepresenttheannotationson theformalandimplicitparameters,indeclarationorder,of theexecutablerepresentedbythisobject Since: 1.5 SeeAlso: Parameter, Parameter.getAnnotations() getAnnotatedReturnType public AnnotatedType getAnnotatedReturnType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofatypeto specifythereturntypeofthemethod/constructorrepresentedbythis Executable. IfthisExecutableobjectrepresentsaconstructor,theAnnotatedTypeobjectrepresentsthetypeoftheconstructedobject. IfthisExecutableobjectrepresentsamethod,theAnnotatedTypeobjectrepresentstheuseofatypetospecifythereturn typeofthemethod. Specifiedby: getAnnotatedReturnType inclass Executable Returns: anobjectrepresentingthereturntypeofthemethod orconstructorrepresentedbythisExecutable Since: 1.8 Skipnavigationlinks Overview Package Class Use Tree Deprecated Index Help Java™ PlatformStandard Ed. 8 Prev Class Next Class Frames No Frames All Classes Summary:  Nested |  Field |  Constr |  Method Detail:  Field |  Constr |  Method SubmitabugorfeatureForfurtherAPIreferenceanddeveloperdocumentation,seeJavaSEDocumentation.Thatdocumentationcontainsmoredetailed,developer-targeteddescriptions,withconceptualoverviews,definitionsofterms,workarounds,andworkingcodeexamples.Copyright©1993,2022,Oracleand/oritsaffiliates.Allrightsreserved.Useissubjecttolicenseterms.Alsoseethedocumentationredistributionpolicy.



請為這篇文章評分?