• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Reflection

 
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to create a method in Java that has an input parameter of type Object and use Java reflection to find out if the object has a property of type, Date or Calendar. How can I do this?
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want to know what type of object was passed to your method?

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need reflection to inspect your arguments you need a better design. Can't you introduce one or more interfaces that your classes can implement, and that you can use in your method? Using instanceof and casting is a much nicer way than using reflection.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:. . . instanceof and casting is a much nicer way than using reflection.

But there is bound to be a better way to do things than instanceof, which is usually another “code small”. I would start by no longer using java.util.Date or Calendar.
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Do you want to know what type of object was passed to your method?



I was thinking of having a method that takes an argument of type Object. Then, I would like to find out whether that object has any properties of type Date or Calendar. For example, if a Car object were passed to the method, I would like to find out if the Car object has any properties of type Date or Calendar. If a Person object were passed to the method, I would like to find out if the Person object has any properties of type Date or Calendar.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you need to do with these properties?
 
Ron McLeod
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use reflection to inspect the fields for their type:

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Victa wrote:. . . I would like to find out if the Person object has any properties of type Date or Calendar.

I agree with Rob that you have some very dubious design there.
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:You can use reflection to inspect the fields for their type:



Thank you.

Suppose the properties are private. If the field is of type Date or Calendar and it is not null, how can I convert the timestamp to UTC?
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:What do you need to do with these properties?



If they are of type Date or Calendar and they are not null, then convert the timestamp to UTC.

The properties are private.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Victa wrote:
Suppose the properties are private. If the field is of type Date or Calendar and it is not null, how can I convert the timestamp to UTC?



The java.lang.reflect.Field object has setters. So, you can set the value of the field too.

However, keep in mind, you will need permission. If there is a security manager, and your application don't have the rights, the operation will fail (also you can request access to private fields via the setAccessible() method call too, which can also fail if the security manager doesn't allow it).

Henry
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Victa wrote:

Rob Spoor wrote:What do you need to do with these properties?



If they are of type Date or Calendar and they are not null, then convert the timestamp to UTC.

The properties are private.



How do you know the properties are private? You asked about getting date or calendar properties from objects of arbitrary classes, so those properties could have any level of accessibility. And what if the class has several date properties?

Or if you just have a specific list of classes you're interested in here, let's bring up the idea of making those classes implement a specific interface again.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
Or if you just have a specific list of classes you're interested in here, let's bring up the idea of making those classes implement a specific interface again.



I am going to speculate that the OP is trying to change fields from a third party library. And changing interfaces (API) is probably not an option here.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic