Pencils Down

This weblog is about my experiences in software development

Browsing Posts tagged Java

I ran across this ‘error’ this week. After poking around the internet for a while it looked like this is due to a bug in an older version of the SAX parser in the xerces library.

Well, we have quite a bit of code in production, so can’t just update the jar.

My original code was simply using the class loader to read in an xsd and use the Source of the xsd for a Schema that would be used to validate some xml entered by the user.

I toyed with this some and thought the exception sounded like it thought there was extra cruff after the closing tag of the xsd. So, I read in the xsd to a byte array, as before. I kept track of the byte count of read bytes, looped over the byte array (for that count), added each byte to a StringBuffer then converted the StringBuffer to a String. Voila!

Was getting this error trying to run a hibernate query in a thread during startup.

Need to add @Transactional to the class. This tells hibernate to add the session to the thread.

A new company, another set of random values for serialVersionUID.

Not sure why most programmers have the urge to put in some huge, ‘random’ number here. The objective is to match versions during serialization. Can’t imagine using a random number is a good idea.  Even better, the numbers entered are not random.

Sorry, this is just a pet peeve. There is much written up on using this correctly if you are truly serializing objects. I think I have seen this at every company I ever worked.

We have a case where people can submit multiple changes to their database records. Each change is handled separately with code like:
– from table where attribute=value
– object.setChangedValue(updatedValue)
– hibernate.flush()

However, if the user happened to submit two changes to the same record the initial query will return the initially obtained object – NOT the updated value.

We fixed this by performing a refresh() once we get the object back from the hql query.

FindBugs is a program which uses static analysis to look for bugs in Java code.

It is offered as an Eclipse plugin. So, just like any other plugin:

  • right click on the item(s) you want to check,
  • click on “FindBugs” and
  • off you go.

Some of the issues that have come up for me:

  • use of static SimpleDateFormat – it’s not thread safe – who knew?
  • new coding did not have a setter for a dao – hopefully testing would have shown the error
  • compares on different object types – the code looked correct, but oh oh…

I love things that just work

Just saw that Java 8 fixes the date problem – http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

I have run across an issue several times with Maven in Eclipse. If a dependent project is updated my project is toast for the rest of the day. I have tried shutting down Eclipse, rebuilding all, updating all dependencies: you name it.

After being fed up with this today I tried shutting down my machine and starting anew. Guess what? The build worked! No changes whatsoever to any of my code.

Just got this (while trying to create history records of a set)

The error occurs because an attempt was made to store the exact same Collection object more than once (possibly in multiple objects).

In my case I just attempted to copy the collection out of the real object into the history object.

I think this is enforcement of the not allowing multiple inheritance in Java. Also, not sure how this would work at the database level

Another developer and I started seeing this running an app locally.

While it sounds really interesting with hints of transaction state it was really just noise cascading up from a missing property in the local properties file.

Yes, it did not occur on server. But again, from the wording it sounds like some filter error or the like and we use different authorizations running locally.

VerbalExpressions

No comments

VerbalExpressions is a library available on GITHUB for Java,C++,PHP…

It allows you to build an expression using a VerbalExpressions object.  The example from this site is below (showing Java but very similar in other languages).

VerbalExpression testRegex = new VerbalExpression ()
                                 .startOfLine()
                                 .then("http")
                                 .maybe("s")
                                 .then("://")
                                 .maybe("www.")
                                 .anythingBut(" ")
                                 .endOfLine();

// Create an example URL
String url = "https://www.google.com";

// Use VerbalExpression's testExact() method to test if the entire string matches
// the regex
testRegex.testExact(url); //True

testRegex.toString(); // Ouputs the regex used:
                      // ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$

VerbalExpression testRegex = new VerbalExpression ()
                                 .startOfLine()
                                 .then("abc")
                                 .or("def");

String testString = "defzzz";

//Use VerbalExpression's test() method to test if parts if the string match the regex
testRegex.test(testString); //true
testRegex.testExact(testString); //false

Turn off this setting: Project->Properties->Java Compiler->Enable Project Specific Settings

Likely there is something in your specific settings that is off (again likely wrong JDK)