Wednesday, October 30, 2013

JUnit + Eclipse + Maven = fun

Given:

/src/test/java/com/helloworld/a.xml with body <src_test_java/>
/src/test/resources/com/helloworld/a.xml with body <src_test_resources/>
/src/main/java/com/helloworld/a.xml with body <src_main_java/>
/src/main/resources/com/helloworld/a.xml with body <src_main_resources/>

Run the following JUnit(NOT under /src/test/java/com/helloworld/) in Eclipse configured by Maven with Eclipse plugin (mvn eclipse:eclipse):

A: this.getClass().getResource("a.xml”);
B: this.getClass().getResource("/com/helloworld/a.xml");
C: this.getClass().getResourceAsStream("a.xml");
D: this.getClass().getResourceAsStream("/com/helloworld/a.xml");
E: this.getClass().getClassLoader().getResource("a.xml");
F: this.getClass().getClassLoader().getResource("/com/helloworld/a.xml");
G: this.getClass().getClassLoader().getResourceAsStream("a.xml");
H: this.getClass().getClassLoader().getResourceAsStream("/com/helloworld/a.xml");

What should the result look like?

Answer:
A: empty string
B: <src_test_resources/>
C: empty string
D: <src_test_resources/>
E: null
F: null
G: null
H: null

Tuesday, February 12, 2013

java.lang.Long.intValue() or casting long to int

Problem:

public static void main(String args[]) {
Long l = Long.MAX_VALUE;
System.out.println(l.longValue());
System.out.println(l.intValue());
System.out.println("---------------\n");
Long l2 = Long.valueOf(Integer.MAX_VALUE);
System.out.println(l2.longValue());
System.out.println(l2.intValue());
System.out.println("---------------\n");
Long l3 = Long.valueOf(Integer.MAX_VALUE) + 1;
System.out.println(l3.longValue());
System.out.println(l3.intValue());
System.out.println("---------------\n");
Long l4 = Long.valueOf(Integer.MAX_VALUE) + 2;
System.out.println(l4.longValue());
System.out.println(l4.intValue());
System.out.println("---------------\n");
Long l5 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE;
System.out.println(l5.longValue());
System.out.println(l5.intValue());
System.out.println("---------------\n");
Long l6 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE + 1;
System.out.println(l6.longValue());
System.out.println(l6.intValue());
System.out.println("---------------\n");
Long l7 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE + 2;
System.out.println(l7.longValue());
System.out.println(l7.intValue());
System.out.println("---------------\n");
}




9223372036854775807
-1
---------------

2147483647
2147483647
---------------

2147483648
-2147483648
---------------

2147483649
-2147483647
---------------

4294967294
-2
---------------

4294967295
-1
---------------

4294967296
0
---------------




Solution:

use one of the suggested methods mentioned at this thread:

http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java

Thursday, February 7, 2013

Use autoboxing/unboxing cautiously

Problem:


public static void main(String args[]) {
Long long1 = Long.valueOf(123L);
Long long2 = new Long("123");
Long long3 = Long.parseLong("123");
long long4 = Long.valueOf(123L);
long long5 = new Long("123");
long long6 = Long.parseLong("123");

System.out.println("long1==long2:"+ (long1==long2));
System.out.println("long1==long3:"+ (long1==long3));
System.out.println("long1==long4:"+ (long1==long4));
System.out.println("long1==long5:"+ (long1==long5));
System.out.println("long1==long6:"+ (long1==long6));
System.out.println("long2==long3:"+ (long2==long3));
System.out.println("long2==long4:"+ (long2==long4));
System.out.println("long2==long5:"+ (long2==long5));
System.out.println("long2==long6:"+ (long2==long6));
System.out.println("long3==long4:"+ (long3==long4));
System.out.println("long3==long5:"+ (long3==long5));
System.out.println("long3==long6:"+ (long3==long6));
System.out.println("long4==long5:"+ (long4==long5));
System.out.println("long4==long6:"+ (long4==long6));

System.out.println("long5==long6:"+ (long5==long6));
}

long1==long2:false
long1==long3:true
long1==long4:true
long1==long5:true
long1==long6:true
long2==long3:false
long2==long4:true
long2==long5:true
long2==long6:true
long3==long4:true
long3==long5:true
long3==long6:true
long4==long5:true
long4==long6:true
long5==long6:true

Solution: If you don't know the origin of the Long object, you may want to check if it is null AND longObject.longValue() for value comparison.

Tuesday, September 4, 2012

Invoking parent private method by reflection


public class SuperClass {
boolean superField = true;
private void superMethod(boolean superField) {
System.out.println("superMethod() called - param:" + superField);
}
}






import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SubClass extends SuperClass {
public static void main(String args[]) {
SubClass subClassInstance = new SubClass();
try {
Method declaredMethod = SuperClass.class.getDeclaredMethod("superMethod", boolean.class);
declaredMethod.setAccessible(true);
declaredMethod.invoke((SuperClass)subClassInstance, new Object[] {false}); //I remember that there was a situation this explicit cast is required
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}



Friday, March 30, 2012

MapQuest Android MapView vs Google Android MapView

Problem: Cannot align the MapView properly in MapQuest.


Solution: Do your own bit-wise OR!!!


Google Android MapView LayoutParams:


int a = MapView.LayoutParams.BOTTOM_CENTER; //81

int b = MapView.LayoutParams.BOTTOM; //80

int c = MapView.LayoutParams.CENTER_HORIZONTAL; //1

int d = MapView.LayoutParams.BOTTOM | MapView.LayoutParams.CENTER_HORIZONTAL; // 81

it makes sense, doesn't it?



How about MapQuest Android MapView LayoutParams?

int a = MapView.LayoutParams.BOTTOM_CENTER; //35

int b = MapView.LayoutParams.BOTTOM; //32

int c = MapView.LayoutParams.CENTER_HORIZONTAL; //1

int d = MapView.LayoutParams.BOTTOM | MapView.LayoutParams.CENTER_HORIZONTAL; // 33

hmmm...something doesn't seem right here...