Welcome to this Blog. I am Software Engineer and work for Zühlke Engineering AG in Bern. This is my private blog, in which I will post mainly about technical stuff like Software Engineering or IT related topics. The views expressed herein do not necessarily represent those of my employer.

 

 
 

 

Generics are very useful. But sometimes, they don’t fullfil all required needs. Today, I had a small problem. Over the parameter of a method, a Class definition was passed. In the method, I had to cast an object of type Object into a concrete type (the concrete type was T, defined by Generics). The easiest way is to put the cast between a try and catch block. This will work, no question. But in my piece of code, a try and catch block wasn’t very nice. For this reason, I tried to check, if the object o is of the type clazz. Unfortunately, instanceof doesn’t work with dynamic defined classes. But there is a simple solution: just use the method isAssignableFrom.

private void test(Class<?> clazz) {
  // the Object o is from somewhere...
 
  if (o instanceof clazz) {
    // does not working
  } 
 
  if (o.getClass().isAssignableFrom(clazz)) {
    // is working
  }
}

 

 

 
 

 

I just found a really good Quick Reference sheet about the Design Patterns from the GoF (Gang of Four). Have a look at this blog.

 

 

 
 

 

Today I stumbled upon a nice solution, to use the Collections.EMPTY_LIST with Generics. Just use the method emptyList with Generics instead of the constant value.

For example:

Collections.<String> emtpyList();

 

 

 
 

 

We use JiBX in our current project and had the problem, that JiBX won’t trim whitespaces automatically. For example, a XML tag like <test> asdf  </test> get’s stored as “ asdf  “ instead of “asdf”.

To solve this problem, we created an user-defined formatter for Strings.

package ch.stefanjaeger.formatter; 
public class StringFormatter { 
  public static String trimString(String untrimmed) { 
    return untrimmed.trim(); 
  } 
}

Now just update the binding file of JiBX and JiBX will trim whitespaces:

<format 
deserializer="ch.stefanjaeger.formatter.StringFormatter.trimString" 
type="java.lang.String" />

 

 

 
 

 

Are you a real men (or women)? Because if you are one, Eclipse offers you the possiblity to “don’t click” ;-) Try to use the simple shortcut CTRL + 3. What happens? Eclipse opens the Quick access window:

image

 

Now, type what you want, for example “new xml file”, and Eclipse searches for the command, which consists of these words:

image

 

Also very interesting is the access to the Preferences window. Try to search for “classpath variables” or “build path”, you get direct access to these settings. Also interesting is “Generate Getters”. If you don’t know a specific shortcut, just use CTRL + 3 from now on!

 

 

 

Older Posts »