6

I want to set all String members of an object to an empty string if they are null.

Pseudocode:

foreach member in object {
    if (member instanceof String and member == null) {
        member = '';
    }
}

What is the simplest way to achieve that? Any framework / tool that I can use? Write my own solution via reflection?

4
  • 1
    For what purpose? It's easier to do that in the corresponding get() method, if you always call it, than it is to affect the actual value. Commented Nov 28, 2011 at 9:05
  • The classes are generated by JAXB2, so I don't want to change them. Commented Nov 28, 2011 at 9:13
  • If you are using an .xsd to define the schema for JAXB2 you can define default values direct in the .xsd. Commented Nov 28, 2011 at 9:32
  • Unfortunately the schema is not under my control.. But I agree that it would be better to solve the problem there. Commented Nov 28, 2011 at 9:54

5 Answers 5

7
public static void setEmpty(Object object) throws IllegalArgumentException, IllegalAccessException {
    Class<?> clazz = object.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (String.class.equals(field.getType())) {
            field.setAccessible(true);
            if (field.get(object) == null) {
                field.set(object, "");
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use reflection to list all fields of an object and then check and change it. You might have to modify the access level, if they are private. You can find a lot of tutorials on that when searching Google, e.g. this one.

Comments

1

Try to use AspectJ:

@Aspect
public class StringHandler {
    @Around("execution(String com....YourClass.*(*))")
    public Object handle(ProceedingJoinPoint thisJoinPoint) throws Throwable {   
        String s = (String) thisJoinPoint.proceed();
        if (s == null){
           return "";
        }
        return s;
    }
}

This will be faster in run-time because this aspect will be compiled to byte-code, otherwise reflection will be used at run-time and slows your app.

Comments

0

Another way is to write an utility which generates code which sets empty strings to members of particular objects.

Comments

0

In favor of reusable design you should consider using a default value for null values just like Apache's common defaultString API, something like this:

public String getValue(String value){
   return StringUtils.defaultString(value);
}

You can also consider using defaultString(String str,String defaultStr) so that you have the option of changing the default value to anything should there be any reason of doing so.

StringUtils documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.