10/28/2014

Javadoc closing tags in IntelliJ

When writing javadocs, IntelliJ automatically adds a closing tag for html elements. For instance, after typing <lt>, it automaticaly adds </lt>, or after typing <p>, it adds </p>. It can be annoying since simple html elements like those used in javadocs don't really need ending tags.
To disable javadoc automatic closing tags in IntelliJ, simply go to IntelliJ Preferences -> Editor -> Smart Keys, then on the right panel, uncheck Automatically insert closing tag.

Intellij 14 screenshot:



 Intellij 15 screenshot:

A related note, JDK 8 has tightened javadoc syntax check, and as a result self-closing elements like <p/>, or <br/> are deemed invalid and will cause failures. See JDK-8020619. However, this checking can be disabled by passing nonstandard option -Xdoclint:none to javadoc tool. For official javadoc guidelines, see How to Write Doc Comments for the Javadoc Tool .

9/24/2014

Diff tools: diff, vimdiff, opendiff & twdiff

4 diff tools that can be executed from command line:

  1. diff (/usr/bin/diff): pure CLI with text output, available in any terminals:
    /tmp > diff Hello.java Hello2.java
    
    1c1
    < public class Hello {
    ---
    > public class Hello2 {
    3c3
    <         System.out.println("Hello from " + Hello.class);
    ---
    >         System.out.println("Hello2 from " + Hello2.class);
    

    use -wub option to view contextual diff:
    /tmp > diff -wub Hello.java Hello2.java
    
    --- Hello.java 2014-09-24 13:16:55.000000000 -0400
    +++ Hello2.java 2014-09-24 13:18:01.000000000 -0400
    @@ -1,5 +1,5 @@
    -public class Hello {
    +public class Hello2 {
         public static void main(String[] args) {
    -        System.out.println("Hello from " + Hello.class);
    +        System.out.println("Hello2 from " + Hello2.class);
         }
     }
    

  2. vimdiff (/usr/bin/vimdiff): part of vim, available in any terminals, pure CLI but blocks the current terminal:
    vimdiff Hello.java Hello2.java
    

  3. opendiff (/usr/bin/opendiff): part of Mac OS xcode tools, launches a FileMerge window for diff and merge:  
    opendiff Hello.java Hello2.java
    



  4. twdiff (/usr/local/bin/twdiff): command line tool of TextWrangler, and launches 3 separate TextWrangler windows (left, right and bottom) for diff and merge.
    twdiff Hello.java Hello2.java
    


7/24/2014

java.util.Properties and character encoding

java.util.Properties class (see Java SE 7 Javadoc) by default assumes ISO 8859-1 character encoding in reading and writing. So when a properties file is in other character encoding, you will see strange characters and behaviors.

Properties class has no method or constructor that takes encoding or locale parameter. Fortunately, in Java SE 6, two new methods were added to allow for reading from java.io.Reader and writing to java.io.Writer:

public synchronized void load(Reader reader) throws IOException;

public void list(PrintWriter out)
So you can create Reader or Writer instances with appropriate encoding, and pass them to Properties load or list methods. For example,
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.junit.Test;

public class MyTest {

    @Test
    public void loadPropertiesTest() throws Exception {
        final File jndiPropertiesFile = new File(System.getProperty("user.home"), "tmp/jndi.properties");
        final FileInputStream fileInputStream = new FileInputStream(jndiPropertiesFile);

        // FileReader does not have any constructor that takes encoding, so use InputStreamReader instead
        final InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");

        final Properties jndiProperties = new Properties();
        jndiProperties.load(inputStreamReader);

        //now dump the properties to verify
        jndiProperties.list(System.out);
    }
}
Output from running loadPropertiesTest test:
-- listing properties --
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces