Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I had a need to remove duplicates from a pre-existing Java List. I tried:

ids = new LinkedList<WatchedId>(new LinkedHashSet<WatchedId>(ids));

Seems easy enough, but the list kept returning no elements. I started clicking through the code and found the LinkedList constructor that takes a collection eventually calls

LinkedList.addAll(int size, Collection)

That method has the following line

Object[] a = c.toArray();

The implementation was in AbstractCollection.toArray(). I stepped through the code and I saw that the nodes were not being moved. Maybe that is a Mac JVM bug? I changed the type to an ArrayList and it worked. Maybe it has something to do with going from a double-linked backing object to a double-linked backing object.

ids = new ArrayList<WatchedId>(new LinkedHashSet<WatchedId>(ids));

September 27th, 2012

Posted In: Collections, Java

Tags: , , , , , , , ,

Leave a Comment

If you JVM is showing the wrong time, it may be because it is picking up the wrong timezone. Specify the following to fix it:

-Duser.timezone=”America/Louisville”

September 12th, 2012

Posted In: Java, Joda-Time

Tags: , ,

Leave a Comment

The project finally got so large that the JUnit tests would run out of heap space during the tests. I added space to the IDEA maven runner, but that didn’t work. You have to add it to the maven-surefire plugin.

<plugin>                                            
    <groupId>org.apache.maven.plugins</groupId>     
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.12.3</version>                       
    <configuration>                                 
        <argLine>-Xms512m -Xmx512m</argLine>        
    </configuration>                                
</plugin>                                           

September 11th, 2012

Posted In: Java, JUnit, Maven

Leave a Comment

I needed to create an xml source object so I could force an unmarshaller to return a specific type of object.

Source source = new StreamSource(new StringReader(myString));

September 3rd, 2012

Posted In: Java, JAXB

Leave a Comment

In a previous post, I showed how to use @XmlSeeAlso to inform the JAXB context of inheriting classes. This is great during marshalling. The real pain was on unmarshalling. A requirement we had was that there were two objects that would need to be able to be unmarshalled from the same root element, . This combined with the @XmlSeeAlso caused a problem. When I was unmarshalling an object of A, it consistently returned an object of type B. The only way I found to force it to return a certain object was to specify it via:

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(...This.class or Super.class...);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Source source = new StreamSource(new StringReader(myString));

            JAXBElement<PersonWatchModel> jaxbElement = unmarshaller.unmarshal(source, This.class);
            someClass.setSomeProperty(jaxbElement.getValue());
        } catch (JAXBException e) {
            log.error("JAXBException {}", e);
            throw e;
        }

This forced it to a specific type and I could use the .getValue() property to get the java object that I needed.

September 3rd, 2012

Posted In: Java, JAXB, xml

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com