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:
java.lang.NullPointerException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.internal.runners.ClassRoadie.runAfters(ClassRoadie.java:72)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:47)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
 at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
 at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
 at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236)
 at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134)
 at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
 at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
 at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
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:
 @AfterClass
 public void resetConf() throws Exception{
  setTestConfiguration();
 }
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;
use
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 http://gnuwin32.sourceforge.net 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 2.1.1.4) (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(DefaultErrorHandler.java:40)
 at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
 at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
 at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1119)
 at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:846)
 at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1530)
 at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
 at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
 at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:865)
 ...
Fortunately we have not only found the problem but also a cure. Assuming web.xml for server is located in C:\Tools\WASC 2.1.1.4\var\catalina\conf, make sure it contains following entries:
  
        jsp
        org.apache.jasper.servlet.JspServlet
 
        modificationTestInterval
        0
    
        
            development
            true
        
        
            fork
            false
        
        
            xpoweredBy
            false
        
        
            engineOptionsClass
            org.apache.geronimo.jasper.JspServletOptions
    
    
        scratchdir
        c:/Tools/WASC 2.1.1.4/var/catalina/worker1/

    
    
        keepgenerated
        true
    
        3
    
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.