[TxMt] useful java snippets to share

Lang Riley lang at langriley.com
Fri Dec 31 20:55:08 UTC 2004


Below are some useful java snippets and their explanations that I 
created.  I will try to take the time later to add them to the wiki:

Snippet result: Create a class in java with auto-generated package 
name, author, date, class name.
Usage:  For the autogeneration of the package you should create your 
class file in its proper location, e.g., org.foo.bar.Batz.java would be 
created in the directory bar which is a subdirectory of foo and org 
directories.  The autogeneration of package name and classname also 
assumes that your class package directory structure has as its parent 
directory a directory named 'src'.  You can of course change that in 
the third call to sed for both the package name and the class name.
Trigger: jcl (or whatever you want to trigger it with)
Snippet:

${1:package `echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*src.//' | sed 's/.[A-Za-z]*.java//g'`};




/**
  * ${2:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*src.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`}
  *
  * @author ${9:`niutil -readprop / /users/$USER realname`}
  * @since `date +%D`
  */
public class ${2:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | 
sed 's/.*src.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`} ${3:extends} 
$4 ${5:implements} $6${7:,}$8 {

    $0

}


Snippet result:  Create a private scoped variable.  Not that useful, 
but heh, it is here if you want it.
Usage: No explanation necessary.
Trigger: jpv


private ${1:String} $2;

$0


Snippet result: Create  a public scoped method
Usage: No special comments.  Pretty straight forward.
Trigger: jpum
Snippet:

/**
  * $2
  *
  * @param $4 $5
  * ${6:@return} $7
  */
public ${1:void} $2(${3:String} $4) {

    $0

}



Snippet result: Create an if statement with guard comment
Usage:
Trigger: jif
Snippet:

if ($1) { // $2
    $0
}


Snippet result: Create a privately scoped method.
Usage:
Trigger: jpm
Snippet:

/**
  * $2
  *
  * @param $4 $5
  * ${6:@return} $7
  */
private ${1:void} $2(${3:String} $4) {

    $0

}


Snippet result: Create a while statement with guard comment
Usage:
Trigger: jwh
Snippet:

while ($1) { // $2

    $0

}


Snippet result: Create a try/catch/finally block
Usage:
Trigger: jtc
Snippet:

try {

    $2

} catch ($1 e) {

    $3

} finally {

    $0

}


Snippet result: Create a class variable/constant.
Usage:
Trigger: jps
Snippet:


private static final ${1:String} $2 = "$0";



Snippet result: Create an if/else statement with guard comments
Usage:
Trigger: jelse
Snippet:


if ($1) { // $2

    $0

} else { // $3



}



Snippet result: Create the package imports and instantiation code for 
using jakarta commons logging package.
Usage: I would suggest executing the snippet in the import area of you 
class definition and then cut/pasting the instantiation code to the 
private member variable declaration area.
Trigger: jlog
Snippet:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** Logger for this class and subclasses */
  protected final Log logger = LogFactory.getLog(getClass());



Snippet result: Create a JUnit TestCase subclass for unit testing.  
Auto-generates the package name and the proper imports for the class 
that will be tested.
Usage:  Assumes JUnit unit test class naming  convention is followed 
whereby if a class named UserDao is being unit tested, the unit test 
class would be named UserDaoTests.java and would be located in a 
parallel directory structure analogous to its package name.  Also 
assumes, as is standard convention, that unit test packages  have as 
their parent directory 'test' and the classes which they test have the 
parent directory of 'src'.  For example, UserDao.java would be located, 
let's say, in src/org/foo/bar/dao/ and its unit test class, 
UserDaoTests.java, would be in test/org/foo/bar/dao/.  Of course, if 
you don't work that way, you can message the regular expressions to 
suite your environment.
Trigger: jtest
Snippet:

package `echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/.[A-Za-z]*.java//g'`;


import junit.framework.TestCase;
import `echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/Tests.java//'`;


/**
  * ${2:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`}
  *
  * @author ${4:`niutil -readprop / /users/$USER realname`}
  * @since `date +%D`
  */
public class ${2:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | 
sed 's/.*test.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`} extends 
TestCase {

    private ${5:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | 
sed 's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/'`}  ${6:`echo $TM_FILEPATH |  sed 's/\//./g' 
| sed 's/^.//' | sed 's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/' | perl -e 'print lcfirst(<>)'`};

    public ${5:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/Tests.java//' | sed 's/.*\.\([A-Za-z]*\)$/\1/'`} 
get${5:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/'`}() { return this.${6:`echo $TM_FILEPATH |  
sed 's/\//./g' | sed 's/^.//' | sed 's/.*test.//' | sed 
's/Tests.java//' | sed 's/.*\.\([A-Za-z]*\)$/\1/' | perl -e 'print 
lcfirst(<>)'`}; }
    public void set${5:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 
's/^.//' | sed 's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/'`}(${5:`echo $TM_FILEPATH |  sed 's/\//./g' | 
sed 's/^.//' | sed 's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/'`} ${6:`echo $TM_FILEPATH |  sed 's/\//./g' | 
sed 's/^.//' | sed 's/.*test.//' | sed 's/Tests.java//' | sed 
's/.*\.\([A-Za-z]*\)$/\1/' | perl -e 'print lcfirst(<>)'`}) { 
this.${6:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*test.//' | sed 's/Tests.java//' | sed 's/.*\.\([A-Za-z]*\)$/\1/' | 
perl -e 'print lcfirst(<>)'`} = ${6:`echo $TM_FILEPATH |  sed 
's/\//./g' | sed 's/^.//' | sed 's/.*test.//' | sed 's/Tests.java//' | 
sed 's/.*\.\([A-Za-z]*\)$/\1/' | perl -e 'print lcfirst(<>)'`}; }

    public void test$8() {
       $0
    }
}



Snippet result: Create public getter/setter pair for a private instance 
variable.  This really comes in handy if you do a lot of dependency 
injection style development, for bean properties.
Usage:
Trigger: jprops
  Snippet:

public $1 get$2() { return this.$3; }
public void set$2($1 $3) { this.$3 = $3; }


Snippet result: Create a JSTL output statement for jsp code.
Usage:
Trigger: jcout
Snippet:


<c:out value="\${$1}"/>$0


Snippet result: Create an interface definition with autogenerated 
package name, interface name, author name, and date.
Usage: Same as the usage for the java class snippet.
Trigger: jid
Snippet:

${1:package `echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*src.//' | sed 's/.[A-Za-z]*.java//g'`};

/**
  * ${2:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 's/^.//' | sed 
's/.*src.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`}
  *
  * @author ${3:`niutil -readprop / /users/$USER realname`}
  * @since `date +%D`
  */
public interface ${4:`echo $TM_FILEPATH |  sed 's/\//./g' | sed 
's/^.//' | sed 's/.*src.//' | sed 's/.*\.\([A-Za-z]*\).java/\1/g'`} {

    $0

}


Hope it helps someone, it sure has saved me a ton of typing.

Regards,

Lang Riley




More information about the textmate mailing list