• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Java Reflection  RSS feed

 
Ranch Hand
Posts: 213
1
  • Mark post as helpful
  • send pies
  • 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?
 
Bartender
Posts: 2073
276
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to know what type of object was passed to your method?

 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 60806
190
  • Mark post as helpful
  • send pies
  • 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: 213
1
  • Mark post as helpful
  • send pies
  • 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: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you need to do with these properties?
 
Ron McLeod
Bartender
Posts: 2073
276
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use reflection to inspect the fields for their type:

 
Campbell Ritchie
Marshal
Posts: 60806
190
  • Mark post as helpful
  • send pies
  • 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: 213
1
  • Mark post as helpful
  • send pies
  • 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: 213
1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!