Will
Will

Reputation: 8651

Getting a methods Return type via reflection

I am getting the attributes from an incoming object which I will check it's values.

I wish to know if there is a way via reflection that I can obtain not only the value from attribute but also it's type?

would

result instanceof Integer.Class

work for example?

Upvotes: 0

Views: 310

Answers (3)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299218

This should be the safest and most universal way. It's null-safe, you don't need an extra != null check:

SomeClass.class.isInstance(result)

Reference:

Class.isInstance(Object)

Upvotes: 1

cadrian
cadrian

Reputation: 7376

No.

Use Class.isAssignableFrom()

Upvotes: 0

beny23
beny23

Reputation: 35068

You can use result.getClass()

Upvotes: 0

Related Questions