Check if an object method exists

I call a web service through ColdFusion, which returns an object, and I want to check if one of the methods of this object exists, since it does not always exist.

I found this source that seemed promising, but based on my tests, I see that the results are always negative, and the method never occurs when it is clear there.

<cfif structKeyExists("#Result.getNotifications().getValidationResult(0)#","getField")>

Resultis my main object, and my ultimate goal is to check if a method exists getField().

enter image description here

Is there a clean way to do this, unlike try / catch?

+4
source share
4 answers

Update:

, , IsInstanceOf() -, - , CF Proxy "" -. , - . ( ). , , , IsInstanceOf().

<cfif compare(yourObject.getClass().name, "org.tempuri.ValidationResultField") eq 0>
   Found ValidationResultField. do something
</cfif>

, /: ArrayOfValidationResult, ValidationResultField, etecetera. , , , , , -. , , IsInstanceOf() , . . . , X Y, , , .

<cfif IsInstanceOf(yourObject, "org.tempuri.ValidationResultField")>
     do something
</cfif>
+2

, structKeyExists , CF , . cfcomponent.

- :

<cftry>
    <cfset Result.getNotifications().getValidationResult(0).getClass().getMethod("getField", javaCast("null", ""))>
    <!--- method does exist --->
    <cfcatch type="coldfusion.runtime.CfJspPage$UnsupportedBaseTypeException">
        <!--- method does not exist --->
    </cfcatch>
</cftry>

, UnsupportedBaseTypeException, , , NoSuchMethodException.

, . , .

0

Miguel-F, , - getMetadata(). , :

<cfset funcs = getmetadata(nameOfObj).functions>

funcs [1].name, funcs [2].name ..

-1

In general, you can get the metadata of all the features of the web service, given the WSDL URL, with something like

<cfhttp method="get" url="http://www.webservicex.net/globalweather.asmx?WSDL" result="res">
<cfset wsXml=xmlparse(res.filecontent)>
<cfset wsOperations = xmlsearch(wsXml,"//wsdl:operation")>
<cfdump var="#wsOperations#">

Another method you can look at (possibly undocumented) is to get method names from class names in the stub directory.

Code to run:

<cfscript>
wsargs = structnew();
wsargs.savejava="yes";
</cfscript>
<cfset convert=createobject("webservice","url_of_wsdl",wsargs)>

Then figure out how to catch the names from the stub directory, {CF_INSTALL} / stubs. In my case, CF_INSTALL is C: / ColdFusion2016 / cfusion /

-1
source

Source: https://habr.com/ru/post/1658247/


All Articles