Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

My First Attempt at ConvertUtils

My first attempt at using ConvertUtils is turning out to be a painful one - most likely due to my own ignorance. Let's see if you can help me out. I have a Hibernate Bag, which is really a java.util.List on my User object. When I run the User object through XDoclet, I create a UserForm (extends ValidatorForm). The form has an ArrayList on for any instances of List or Set on the object (set through a custom struts_form.xdt template). So I created a ListConverter to convert a List object to an ArrayList. Sounds pretty simple right?! Here's my ListConverter.java:

public class ListConverter implements Converter {
    //~ Instance fields ========================================================

    protected Log log = LogFactory.getLog(ListConverter.class);

    //~ Methods ================================================================

    /**
     * Convert a List to an ArrayList
     *
     * @param type the class type to output
     * @param value the object to convert
     */
    public Object convert(Class type, Object value) {
        if (log.isDebugEnabled()) {
            log.debug("entering 'convert' method");
        }

        // for a null value, return null
        if (value == null) {
            return null;
        } else if (value instanceof Set && (type == Set.class)) {
            return new ArrayList((Set) value);
        } else if (value instanceof List && (type == List.class)) {
            return new ArrayList((List) value);
        } else {
            throw new ConversionException("Could not convert " + value
                                          + " to ArrayList!");
        }
    }
}

When I run BeanUtils.copyProperties(userForm, user), I get:

Could not convert cirrus.hibernate.collections.Bag@e2892b to ArrayList!

On another class, where I am trying to convert a List of Longs, I get:

Could not convert [-1, 1, 30129] to ArrayList!

I'm registering my custom converter in a static block of my BaseManager class. My *Manager classes do all the conversions, so this seems logical:

static {
    ConvertUtils.register(new StringConverter(), String.class);
    ConvertUtils.register(new LongConverter(), Long.class);
    ConvertUtils.register(new ListConverter(), ArrayList.class);
	
    if (log.isDebugEnabled()) {
        log.debug("Converters registered...");
    }
}

Since I'm in a major time crunch, I'll try simply making my getter/setters on my UserForm to be List. I'd like to use ConvertUtils though, so hopefully someone has a solution.

This brings me to a RANT and I think it's my first official one. My last three projects have always started small, and the goal has always been a prototype of functionality. A prototype that turns into a production system. All fricken three of them. All were supposed to take about 3 months to develop initially. The last two projects took at least 6 months. This one has a one month deadline, but the scope is a lot smaller than the previous two. But still, it's always the same scenario - the clients want a prototype, but turn it into a production system. Since it's a prototype, I tend to write "workarounds" for design patterns (see above) that I can't figure out. Is this good? It probably doesn't hurt since no one will ever look at my code - right?! When's the last time you looked at a co-workers code? (The more == the better). And the truth is, as long as it works - it's probably good enough. However, I as a developer, get heartburn when I think about maintaining the system that I created under the impression that it was a prototype. Maybe one of these days I'll figure out all the best practices to creating a robust web application, and then I'll know everything - so I won't have to write workarounds for my lack of knowledge. If you see some pigs flying, you can think to yourself - "Wow, Raible must've figured it all out" ;-)

Posted in Java at Jan 11 2003, 04:04:43 PM MST 1 Comment
Comments:

The problem may be these lines:
        } else if (value instanceof Set && (type == Set.class)) {
        } else if (value instanceof List && (type == List.class)) {
Your "value" and its "type" are a subclass of the Set and List interfaces, not an instance of them. I believe that the instanceof operator is OK with this relationship but not the == method comparing their .class attributes. You might try the Class.isAssignableFrom(Class) method to do this test:
        } else if (value instanceof Set && Set.class.isAssignableFrom(type)) {
        } else if (value instanceof List && List.class.isAssignableFrom(type)) {
B

Posted by Barett McGavock on December 22, 2003 at 08:15 PM MST #

Post a Comment:
  • HTML Syntax: Allowed