Wednesday, January 22, 2014

can your language talk json?

Recently I discovered Python. It's really amazing that the language is that flexible so it can treat json literals as it's usual code. Think about constructing all objects in Java with any JSON library. Is this flexibility specific to dynamic languages? Btw I have found an example that well known and loved ;> C++ also is flexible enough to handle JSON:
std::string json_cxx = { 
  key: "value", 
  number: 53     
This will compile. But... don't be fooled by compiler letting pass that code! The result is not what javascript developer could expect :/

Thursday, June 13, 2013

compiler could see that...

I recently have met one of those not straightforward exceptions in tests. Something I first classified as 'perhaps it's config or classpath issue'. Here comes the stacktrace:
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at org.junit.internal.runners.ClassRoadie.runAfters(
 at org.junit.internal.runners.ClassRoadie.runProtected(
 at org.apache.maven.surefire.junit4.JUnit4Provider.execute(
 at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(
 at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(
 at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(
 at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(
 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(
 at org.apache.maven.surefire.booter.ForkedBooter.main(
When I looked at the tests I was able to see all test methods are successful, but after there is an error. I was able to figure out that NPE is thrown during invocation of method within clean up method that should be run after test. Here is the code:
 public void resetConf() throws Exception{
Can you see that?
In debugger I have found that first argument of invoke() is null.
But it's quite ok. If it's null it's just static invocation and that's what I would expect during @AfterClass call...
Yes. Fine.
But resetConf() must be static then, you stupid reflection!

Friday, May 17, 2013

one more null check with Optional

Instead of
    if (tipDocument.isPresent()) {
      return tipDocument.get();
    return null;
return tipDocument.orNull();

Monday, April 22, 2013

How not to check for null

There are some clever tricks how to deal with null values. Usage of guava's Optional is one of them. But this is not one of them:
    final Optional<MilestoneDocument> documentOfVersion = findDocumentOfVersion(documentType, documentVersion);
    if (documentOfVersion == null) {
      throw new IllegalStateException();
    if (documentOfVersion.isPresent()) {
      return documentOfVersion.get();

Tuesday, February 19, 2013

user friendly search on windows

Windows with its cmd.exe seems not to be user friendly environment for developer. But this may be changed easily, you need only to install few programs we miss from normal systems. See and download coreutils, findutils, sed, awk, grep etc. and put them to PATH. As find.exe confilicts with windows file.exe I always rename it to gfind.exe . You have to be careful with quoting - only double quote is welcomed. Also paths may be tricki, remember to replace / with \. Eg. xargs needs extra backslash to interpret one properly.

Let's find all inserts in sql files.

find . -name '*.sql' | xargs grep insert 
It is the win32 version:
gfind . -name "*.sql" | sed s!/!\\\\!g | xargs grep insert

Summary: having in mind few simple rules you may make your system much easier to talk to.

Monday, February 11, 2013

picking up python

Recently I dived into jython, which is now scripting layer of application I' maintaining. Coming from Java/Scala I have some notices:
  • indentation matters
  • it actually does not matter that much, as I indent my code :>
  • method's first argument is always self
  • this point may have some exceptions, but for me a thumb rule is :%s/()/(self)/ in my vim :>
  • there is no null, but None
  • even if it's still jvm
  • None starts with capital letter, just like False, True and some others...
  • this one really surprised me
  • you do not write new to create new object
  • just call constructor, but
  • you have to return explicitly
  • which hurts when you're used to scala's brevity

Sunday, October 7, 2012

strange exception under JSF+JSP

Since we combined JSP with JSF 1.2 on websphere community edition (wasc (same for apache geronimo/apache tomcat), i often see mysteriously looking exception: A literal value was specified for attribute actionListener (see below for stack). This looks like we cannot use #{...} expressions used by faces but only ${...} used by JSP. This is the exception itself:

Caused by: org.apache.jasper.JasperException: /portal/activity/ActivityResultInfo.jsp(377,8) A literal value was specified for attribute actionListener that is defined as a deferred method with a return type of void. JSP.2.3.4 does not permit literal values in this case
 at org.apache.jasper.compiler.DefaultErrorHandler.jspError(
 at org.apache.jasper.compiler.ErrorDispatcher.dispatch(
 at org.apache.jasper.compiler.ErrorDispatcher.jspError(
 at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(
 at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(
 at org.apache.jasper.compiler.Node$CustomTag.accept(
 at org.apache.jasper.compiler.Node$Nodes.visit(
 at org.apache.jasper.compiler.Node$Visitor.visitBody(
 at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(
Fortunately we have not only found the problem but also a cure. Assuming web.xml for server is located in C:\Tools\WASC\var\catalina\conf, make sure it contains following entries:

keepgenerated = true is very important. Now, you can get rid of problem undeploying app, stopping your app server, cleaning scratch dir and redeploying app back. Then faces should run normally.