2011-08-16

Faster coding with content assist in eclipse

Sometimes writing common Java-Statements is a bit boring. I couldn't find a list with common statements, so I decided to create my own.


NameExample
arrayadd
/* add element to an array */
String[] myStringArray = 
    new String[5];
   
/* type 'arrayadd' and press ctrl + space 
* the new myStringArray2 will have length + 1
*/    
String[] myStringArray2 = 
    new String[myStringArray.length + 1];
System.arraycopy(
  myStringArray, 
  0, 
  myStringArray2, 
  0, 
  myStringArray.length);
myStringArray2[myStringArray.length] = 
  new String("newElement");
arraymerge
/* merge two arrays into one */
String[] myStringArray = new String[5];
String[] arrayToAdd = new String[5];
  
/* type 'arraymerge' and press ctrl + space */
String[] myStringArray2 = 
  new String[myStringArray.length
             + arrayToAdd.length];
System.arraycopy(
  myStringArray, 
  0, 
  myStringArray2, 
  0,
  myStringArray.length);
System.arraycopy(
  arrayToAdd, 
  0, 
  myStringArray2, 
  myStringArray.length,
  arrayToAdd.length);
cast
/* cast an object
* type 'cast' and press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
  type new_name = (type) name;
catch
/* create a catch block
* type 'catch' and press ctrl + space 
*/ 
  catch (Exception e) {
  // TODO: handle exception
}
do
/* create a do-while loop
* type 'do' and press ctrl + space 
*/ 
  do {

  } while (condition);
else
/* create an else block
* type 'else' and press ctrl + space 
*/ 
  else {
  
  }
elseif
/* create an else-if block
* type 'elseif' and press ctrl + space 
*/ 
  else if (condition) {
   
  }
for - iterate over array
/* iterate over an array */
String string[] = new String[5];
  
/* type 'for', press ctrl + space and 
* select 'for - iterate over array'
*/ 
for (int i = 0; i < string.length; i++) {

}
for - iterate over array with temporary variable
/* iterate over array using a temporary variable */

String string[] = new String[5];
/* type 'for', press ctrl + space and 
* select 'for - iterate over array 
* with temporary variable'
*/ 
for (int i = 0; i < string.length; i++) {
  String string2 = string[i];
}
for - iterate over collection
/* a for-loop iterating over collection */

Set s = new HashSet();
  
/* type 'for', press ctrl + space and 
* select 'for iterate over collection'
*/  
for (Iterator iterator = s.iterator(); 
     iterator.hasNext();) {
  String string = (String) iterator.next();   
}
foreach
/* iterate over an array or Iterable */

Set s = new HashSet();
  
/* type 'foreach', press ctrl + space and 
* select 'for iterate over an array or
* Iterable
*/   
for (String string : s) {
 
}
if
/* if block
* type 'if', press ctrl + space  
*/ 
if (condition) {

}
ifelse
/* dynamic type test and cast */
String string = new String();
  
/* type 'instanceof', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
  
if (string instanceof type) {
  type new_name = (type) string;
}
lazy
/* init a new object */
String string;

/* type 'lazy', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
if (string == null) {
  string = new type(arguments);
}
return string;
main
/* create a main-method
* type 'main', press ctrl + space  
*/ 
public static void main(String[] args) {
 
}
new
/* create a new object
* type 'new', press ctrl + space  
*/ 
type name = new type(arguments);
private_method
/* create a private method
* type 'private_method', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
private return_type name() {
  
}
private_static_method
/* create a private static method
* type 'private_static...', press ctrl + space  
* same as before, only static 
*/
protected_method
/* create a protected method
* type 'protected', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
protected return_type name() {
  
}
public_method
/* create a public method
* type 'public_method', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
public return_type name() {
  
}
runnable
/* create a new runnable 
* type 'runnable', press ctrl + space 
*/   
new Runnable() {
  public void run() {
    
  }
}
static_final
/* create a new constant
* type 'static_final', press ctrl + space 
*/
public static final String NAME;
switch
/* create a switch block
* type 'switch', press ctrl + space 
*/
switch (key) {
case value:
 
  break;

default:
  break;
}
synchronized
/* create a synchronized block
* type 'synchronized', press ctrl + space 
*/
synchronized (args) {
   
}
syserr
/* print to standard error
* type 'syserr', press ctrl + space 
*/
System.err.println();
sysout
/* print to standard out
* type 'sysout', press ctrl + space 
*/
System.out.println();
systrace
/* print current method to standard out
* type 'systrace', press ctrl + space 
*/
System.out.println("MyClass.main()");
test
/* test method
* type 'test', press ctrl + space 
*/
public void testname() throws Exception {
  
}
toarray
/* convert collection to array */
Set hashSet = new HashSet();
  
/* type 'toarray', press ctrl + space */
(String[]) hashSet.toArray(
                   new String[hashSet.size()])
try
/* create try block
*/ type 'try', press ctrl + space 
*/
try {
   
} catch (Exception e) {
  // TODO: handle exception
}
while - iterate with enumeration
/* create a while loop(enumeration)
* type 'while', press ctrl + space
*/
while (en.hasMoreElements()) {
  type type = (type) en.nextElement();
   
}
while - iterate with iterator
/* create a while loop(iterator) */
Collection stringSet = new HashSet();
Iterator iterator = stringSet.iterator();

*/ type 'while', press ctrl + space */
while (iterator.hasNext()) {
 String string = (String) iterator.next();
  
}
while - while loop with condition
/* create a while loop(condition)
* type 'while', press ctrl + space
*/
while (condition) {
   
}
There are a lot more possibilities, take a closer look at Eclipse for additional information.

Keine Kommentare:

Kommentar veröffentlichen