File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Converting all String fields to upper case. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting all String fields to upper case." Watch "Converting all String fields to upper case." New topic
Author

Converting all String fields to upper case.

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
I have the following code, and I want to covert all the string fields of my class to upper case.

I have no idea on how many string fields are currently available in the class. I want to see through it programmatically each field and if it is of type 'String' I must turn it to uppercase. How can I achieve this?



Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Am not much into reflection, so there might be other elegant solutions.

1. Get all declared fields of the class using getDeclaredFields() method in Class.

2. Iterate over each field, and get the field value (check for null) against String.class

3. Get the existing field value using Field's get(Object) where Object will be the object you want to modify

4. Set the new value using Field's set method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40


Of course, you can do this with the reflection library. However, what do you want to do with final variables? What do you want to do with private and protected variables that you aren't allowed to access? What do you want to do with variables are are inherited? etc. The reflection library allows some dangerous actions, which can of course, break your application.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Wow Henry, I changed one of the test String to final and got the below Exception.

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
John Jai wrote:
2. Iterate over each field, and get the field value (check for null) against String.class

How do you do this?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akhilesh Trivedi wrote:I have no idea on how many string fields are currently available in the class. I want to see through it programmatically each field and if it is of type 'String' I must turn it to uppercase. How can I achieve this?

My question: Why?

This sounds like a design issue, not a programming one.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Even I wanted to say the same, the most dangerous thing is either "Exception" or at the most "Throwable" and both must be catch-able.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Winston Gutkowski wrote:

This sounds like a design issue, not a programming one.

Winston


It is.

And that is why I skipped "Beginning Java".
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akhilesh Trivedi wrote:And that is why I skipped "Beginning Java".

Yes, but you don't fix design issues by programming around them.

Why would you need to convert all String fields of an instantiated object to upper case? It sounds like a solution in search of a problem to me.

Winston
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Akhilesh Trivedi wrote:
How do you do this?


On second thought on reading Winston's reply, why don't just use toUpperCase() wherever you set the field's value?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Jai,

I was through with first. It was on second step I was stuck.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I just checked for relevant methods in the Field API.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akhilesh Trivedi wrote:I was through with first. It was on second step I was stuck.

Akhilesh, I ask again: Why are you doing this?

Reflection is evil, and should only be used in cases where it's really needed; so if you're simply using it to fix a problem that should be solved elsewhere, you're going about it wrong.

Back up and explain the problem, not your solution to it.

Winston
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

John Jai wrote:Wow Henry, I changed one of the test String to final and got the below Exception.



Call setAccessible(true) on the Field instance -- and try again.

Henry
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Winston Gutkowski wrote:
Why are you doing this?


I want to talk in terms of 'Objects' that are case-insensitive.

I push it through a middle layer (preferably reflection layer) which will make everything to uppercase and back when i get anything from this layer i get everything again in upper case.

Evil still?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akhilesh Trivedi wrote:I want to talk in terms of 'Objects' that are case-insensitive.

That's what, not why.

I push it through a middle layer (preferably reflection layer) which will make everything to uppercase and back when i get anything from this layer i get everything again in upper case.

None of which has answered my question.

First: The minute you start using reflection, you are abandoning Java. Reflective code is difficult to read, difficult to write, error-prone and slow (see below). I can still count the number of times I've used it on the fingers of both hands (apart from basic stuff like getClass()) in 11 years of writing Java.

Second: What you are proposing could slow your application down by a factor of 50. Obviously, if you absolutely must do it, then you must; but I don't see any reason to, and you haven't provided one.

Third: There is a perfectly good alternative, and it's String.toUpperCase(). Adding an entire layer of reflection just to save yourself a bit of coding strikes me as madness, but perhaps you'll yet convince me by explaining why you think you need to do this.

Evil still?

Fraid so.

The only thing I can compare it to is someone wanting to add an entire layer of reflection to make all indexes 1-based instead of 0-based because they don't like writing List.get(0) to get the first element. You could do it, but would you want to?

Winston

[Edit] Actually, you could probably do what you want to do using dependency injection with a product like Guice (I say probably because I'm not absolutely sure). Just write a String wrapper that converts its Strings to uppercase, and bind all requests for java.lang.String to it. It might just work, and it'd be lot more lightweight than an entire layer of reflection.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Akhilesh Trivedi wrote:
I want to talk in terms of 'Objects' that are case-insensitive.

I push it through a middle layer (preferably reflection layer) which will make everything to uppercase and back when i get anything from this layer i get everything again in upper case.

Evil still?

Yes, I agree with Winston that your current approach is questionable at best and quite possibly evil. I think Winston would also agree with me that it's possibly evil because it has potential unexpected side-effects (properties are suddenly all uppercase even when you don't want them to be) that other developers may fall victim to since you may not be around all the time to explain what's happening. Besides, it's usually bad form to change properties directly the way you want to do it.

If you really want to do this, I suggest you use Dynamic Proxies as Decorators instead.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Junilu Lacar wrote:If you really want to do this, I suggest you use Dynamic Proxies as Decorators instead.

Hmmm. Interesting. I edited my post to add the possibility of doing it with something like Guice, but I have to admit I have no idea what the effects of a binding for something as fundamental as java.lang.String would be.

Winston
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Yeah, when I hear somebody say "Well, I want to change those attributes so it's easier for me to work with them," the first things that pop into my head are Decorators, Adapters, and Bridges. One of those usually fits the bill for this kind of requirement, when it's appropriate, of course.

As a tip to the OP, you always want to keep these things in mind: Encapsulation, Principle of Least Astonishment, and Prefer Immutable Objects
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Junilu Lacar wrote:Yeah, when I hear somebody say "Well, I want to change those attributes so it's easier for me to work with them," the first things that pop into my head are Decorators, Adapters, and Bridges. One of those usually fits the bill for this kind of requirement, when it's appropriate, of course.

Yup, quite agree, and it has the added bonus of forcing code that needs the "effect" to use a different class, which minimizes the side-effects you referred to. The Guice solution (if it actually works) would actually substitute another class for java.lang.String, which I suspect could get very hairy.

Winston
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Thanks John, Winston and Junilu. I am reading Deocrators.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akhilesh Trivedi wrote:Thanks John, Winston and Junilu. I am reading Deocrators.

Good idea.

One word of warning: be very careful if you want to make a Decorator class like UpperCaseString or CaseInsensitiveString mutually comparable with an ordinary String. It's actually quite tricky to do without violating the contracts for equals() and compareTo(), so you might want to read up on it a bit.

Good luck.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting all String fields to upper case.
 
Similar Threads
Question on static variable
using this in a class itself
Is this a Java Program?
How to implement Data Structure with custom methods
Why this private member can be seen?