Archive

Archive for the ‘Java’ Category

Not tested yet! (WTF)

5 November 2013 1 comment

This is some production code, that fails.

// Do conjunction////NOT TESTED YET???I dont know what will happen : (
startConjunction(customers, application);

Not sure if the author forgot the comment there or it indeed is not tested. But knowing that the code might not work and not fixing it is really WTF!

Categories: Java, WTF

WTF code!

7 October 2013 1 comment

While I was looking at some legacy code I stumbled upon a few wtf! moments. This is code that surprised me and made me laugh. Not that I haven’t written bad code in my life, but this is hilarious.

setPMPLogHistory(new Integer(applicationId).toString());

In the code above, the variable applicationId is already a String! The developer first creates a new Integer instance from a String and then turns this into a String again!

info.getApplicationTypeID().equalsIgnoreCase("1")

Apart from the obvious refactoring (the “1” should be at the beginning of the comparison in order to avoid a npe) you don’t have upper/lower case digits, so the equalsIgnoreCase is unnecessary.

Categories: Java, WTF

There Will Be Code

30 September 2013 Leave a comment

“One might argue that a book about code is somehow behind the times—that code is no
longer the issue; that we should be concerned about models and requirements instead.
Indeed some have suggested that we are close to the end of code. That soon all code will
be generated instead of written. That programmers simply won’t be needed because business people will generate programs from specifications.

Nonsense! We will never be rid of code, because code represents the details of the
requirements. At some level those details cannot be ignored or abstracted; they have to be
specified. And specifying requirements in such detail that a machine can execute them is
programming. Such a specification is code.

I expect that the level of abstraction of our languages will continue to increase. I
also expect that the number of domain-specific languages will continue to grow. This
will be a good thing. But it will not eliminate code. Indeed, all the specifications written
in these higher level and domain-specific language will be code! It will still need to
be rigorous, accurate, and so formal and detailed that a machine can understand and
execute it.

The folks who think that code will one day disappear are like mathematicians who
hope one day to discover a mathematics that does not have to be formal. They are hoping
that one day we will discover a way to create machines that can do what we want rather
than what we say. These machines will have to be able to understand us so well that they
can translate vaguely specified needs into perfectly executing programs that precisely meet
those needs.

This will never happen. Not even humans, with all their intuition and creativity,
have been able to create successful systems from the vague feelings of their customers.
Indeed, if the discipline of requirements specification has taught us anything, it is that
well-specified requirements are as formal as code and can act as executable tests of that
code!

Remember that code is really the language in which we ultimately express the requirements.
We may create languages that are closer to the requirements. We may create tools
that help us parse and assemble those requirements into formal structures. But we will
never eliminate necessary precision—so there will always be code.”

Robert C. Martin – Clean Code. A Handbook of Agile Software Craftmanship.

Well said Robert. This summarises, in a few paragraphs, how important code is in our every day activities.

Categories: Books, Java Tags: ,

How to programmatically add a a4j:commandButton and a4j:actionparam

2 April 2013 Leave a comment

After spending a few hours I finally managed to programmatically add an ajax command button and action parameter to JSF (1.2) components. So the following

<a4j:commandButton value="Click Me"
	styleClass="buttons" rendered="true" immediate="true"
	id="buttonId" actionListener="#{newRequestBean.doSomething}"
	ignoreDupResponses="true">
	<a4j:actionparam name="theVat" id="vatId" value="IComeFromTheBean" assignTo="#{newRequestBean.vat}" noEscape="false"/>
</a4j:commandButton>

translates to

MethodExpression methodExpression = application.getExpressionFactory().createMethodExpression(
                                FacesContext.getCurrentInstance().getELContext(),
                                "#{newRequestBean.doSomething}",
                                null,
                                new Class[0]);
HtmlAjaxCommandButton button = (HtmlAjaxCommandButton) application.createComponent(HtmlAjaxCommandButton.COMPONENT_TYPE);
button.setValue("Click Me");
button.setStyleClass("buttons");
button.setRendered(true);
button.setImmediate(true);
button.setId("buttonId");
button.setIgnoreDupResponses(true);
button.setActionExpression(methodExpression);

ValueBinding vb = context.getApplication().createValueBinding("#{newRequestBean.vat}");
HtmlActionParameter param = (HtmlActionParameter) application.createComponent(HtmlActionParameter.COMPONENT_TYPE);
param.setId("vatId");
param.setName("theVat");
param.setValue("IComeFromTheBean");
param.setAssignToBinding(vb);
param.setNoEscape(false);

button.addActionListener(param);
button.getChildren().add(param);

Things to notice:

1) The last parameter of the method expression should be an empty array (new Class[0]), otherwise you will get the exception “java.lang.IllegalArgumentException: wrong number of arguments“. Your method should have no parameters.
2) You should add the HtmlActionParameter as the action listener of the command button. This is an absolutely essential step in order to make it work (well, this is only if you want to pass parameters to the call). It took me literally hours to figure this out.
3) The context variable is a FacesContext.

Categories: Java, JSF, RichFaces Tags: , ,

How to retain/get only the duplicate elements in a list

4 February 2011 3 comments

Let’s say you have a list of duplicate (and non-duplicate) items, and you want a new collection with only the duplicate items in there. The easiest way is to extend the HashSet.

public class DuplicatesOnlySet<E> extends HashSet<E>
{
    private final Set<E> uniques = new HashSet<E>();

    public  DuplicatesOnlySet(Collection<? extends E> c) 
    {
        super.addAll(c);
    }

    @Override
    public boolean add(E e) 
    {                
        if(!this.uniques.add(e))
             return super.add(e);

    return false;
    }
}

Call it like

List<String> duplicates = new ArrayList<String>(new DuplicatesOnlySet<String>(original)) ;

where original is the Collection with the duplicate items.

Compliments to Markos for his implementation.

Error loading element Identity with component name null and component class null

10 December 2010 2 comments

This error is related to this post, and is solved by the exact same manner.

1) Edit your application.xml and move the jboss-seam.jar module to the top.
2) Edit each MANIFEST.MF file of the rest of your modules defined in the application.xml and remove the jboss-seam.jar entry.
3) Rebuild and redeploy your ear.

Categories: Java, JBoss, Seam, WebLogic

java.lang.ClassCastException: YOUR_REPORT_NAME_1290416522329_455758 cannot be cast to net.sf.jasperreports.engine.fill.JREvaluator

3 December 2010 9 comments

Another classloading issue. While jasper reports work fine on JBoss, on WebLogic they fail with the exception

net.sf.jasperreports.engine.JRException: Error loading expression class : MYREPORT_1290416522329_455758
...
Caused by: java.lang.ClassCastException: MYREPORT_1290416522329_455758 cannot be cast to net.sf.jasperreports.engine.fill.JREvaluator
at net.sf.jasperreports.engine.design.JRAbstractJavaCompiler.loadEvaluator(JRAbstractJavaCompiler.java:101)

This is easily resolved. In my configuration it was just a matter of moving my jasper jar file from the root of ear to the WEB-INF/lib folder of my web app, in order to be picked up by a different classloader. If this does not work try moving the jasper jar file around in different locations, one of them will work.

Categories: Jasper, Java, JBoss, WebLogic Tags: , , ,