How can I find out if an object implements an interface? In other words I need a possibility to check if an object can be cast to a specific type (e.g. a specific interface).
Std.is(). Or use a safe cast and it will throw an error if it fails.
you can try to create the object and catch with a class not found exception i guess.
Related
I want to make the return type of my method generic. The caller will decide which type it should expect.
Actually my method will be a member of interface and the class which will implement it will have a decision making block to delegate the work to other methods.
Hence I want to make the return type of the interface method as generic.
I can achieve this by using dynamic or object keyword or c# generic type.
I am not able to figure it out which will be the best option to achieve it and what are the limitations and advantages of each type.
public interface ICoreWrapper
{
Response<T> ExecuteDeviceCommand<T>(DeviceCommand deviceCommand, object param = null);
}
Please suggest me.
Thanks in advance.
If you do not know the type at compile time you could use dynamics but they will be slower because they are using runtime invocation and less safe because if the type doesn't implement the method you are attempting to invoke you will get a runtime error.
Use dynamic return type, Based on the input type return the appropriate object.
I have created a class implementing GenericHandler to use in .handle() method. I have setters for the class, but if i have more than 1 setter with same argument type, i am getting "Found Ambiguous parameter type".
Why there is such restriction?
That's just because ServiceActivatingHandler is based on the MessagingMethodInvokerHelper logic on background to determine the appropriate messaging method. And setters are candidate for that purpose.
So, if you really hae several of them with the same param type, we end up with ambiguity issue.
To fix your case, I suggest mark your Object handle(P payload, Map<String, Object> headers); implementation with #ServiceActivator.
From other side I agree that it is not so good as we expect from Framework. So, feel free to raise a JIRA issue on the matter and we will fix .handle() to be more strict and rely only on the handle() method from the GenericHandler implementation.
I faced the same problem while using Spring integration while using a service adaptor. Could not define multiple properties of type java.lang.String - I would get a IllegalArgumentException claiming "ambiguous parameters".
After finding no solution to the issue, decided to just create a class to encapsulate those properties, configure this as a bean, and then inject it into the spring-integration config.
When I'm calling a function in Groovy, how do I determine the class of the caller?
For example, I want to know what the class of foo is, inside the printFoo() function:
foo.printFoo()
def printFoo() {
print(this.class)
}
And this should print out the class of foo
I don't know of any Groovy-specific way, but you can get the class name with object.getClass().name. Is that what you are looking for?
If you want to check if an object implements a particular interface or extends a particular class (e.g. Date) use:
object instanceof Date
or to check if the class of an object is exactly a particular class (not a subclass of it), use:
object.getClass() == Date
There's also the in operator: object in Date
The language and the runtime don't provide a reliable mechanism to do what you are asking about. You can do some pokey jiggery inspecting stack frames but that is really detailed low level stuff and won't really be reliable for a number of reasons. The short answer is, the language just doesn't support it.
I have a wcf service and i want to pass a string[] through it. I am receiving the below error. Can someone point me in the right direction ?
The error :
There was an error while trying to serialize parameter http://services.csssoftware.com/2.0/ComFacades:arParams. The InnerException message was 'Type 'System.String[]' with data contract name 'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
If you need more data plese let me know and i will edit the question.
For others who get to the same error, the fix was to add :[ServiceKnownType(typeof(string[]))] in my interface class.
For more information about this problem you can read : HERE
Is it possible in J2ME to call/invoke a method by its name.just like we have getDeclaredMethod in java .
The java.lang.reflect package is available only in CDC 1.1.2. If you're not on such configuration, you're out of luck. There is no way to invoke a method by name without reflection.
A workaround would be to create a map from strings (method names) to appropriate classes on which you can invoke the methods.
Closest you can get is to instantiate a class by name using Class.forName("com.class.ClassName").newInstance() -- that will execute a parameterless constructor.