Tuesday, March 3, 2015

Java: Mocking a final static field

This is a way how to mock a final static field in Java:
 

    static void mockFinalStaticField(Field field, Object newValue) throws Exception {
        field.setAccessible(true);

        // remove final modifier from field
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }

Call it with:

mockFinalStaticField(yourClass.getClass().getDeclaredField("FINAL_STATIC_FIELDNAME"), yourMock);

Monday, June 25, 2012

Enter Key on p:inputText

This is a way how you can trigger an ajax event with primefaces if you want to submit the inputText field with an enter key press:

Use jQuery to trigger the change event:
 
<p:inputText id="name" value="#{bean.value}" 
    onkeypress="if (event.keyCode == 13) {jQuery('#name').trigger('change'); return false; }" >
    <p:ajax event="change" update="@form" listener="#{bean.search}" />
</p:inputText>

Wednesday, January 18, 2012

Hibernate: BLOB/TEXT column 'field_id' used in key specification without a key length

Working with Hibernate and JPA I've had a problem generating a schema. The generation would fail with following message:

org.hibernate.tool.hbm2ddl.SchemaUpdate - main   - BLOB/TEXT column 'field_id' 
used in key specification without a key length

The JPA mapping causing the problem looked like following:
 
@Column(name = prefix + "field_id", length = 256, unique=true, nullable=false)
private String fieldId;

Apparently, using hibernate in combination with MySQL causes this failure. The key element on the mapping is the length attribute. I've had to change it to length=255 and everything worked again.
@Column(name = prefix + "field_id", length = 255, unique=true, nullable=false)
private String fieldId;

Give it a try!

Friday, January 13, 2012

Debugging mvn tomcat:run on a Macintosh

I did have a hard time to figure out how remote debugging with eclipse and the tomcat-maven-plugin works on a Mac. On windows I used a script which looks like following:

set MAVEN_OPTS=-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
mvn tomcat:run

To achieve the same thing on a Mac just change the script to following:

export MAVEN_OPTS="-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
mvn tomcat:run

Sunday, December 18, 2011

hbm2java: Table TableIdentifier(db.tablename) excluded by strategy

The other day I was trying to reverse engineer a database with the hbm2java plugin. Unfortunately no code was generated. The log stated following:

DEBUG org.hibernate.cfg.reveng.JDBCReader - 
Table TableIdentifier(db.tablename) excluded by strategy

After trying out a few things, I finally found following solution:
In your hibernate.reveng.xml file, set the exclude property to true (exclude="true" in
):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM
"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
    <table-filter match-schema="*" match-name="*"
        package="your.package.name.model"
        exclude="true" />
</hibernate-reverse-engineering>

Let me know if it worked for you too!

Saturday, November 26, 2011

Android tablet not found in Eclipse

The other day I was having problems to connect a new android device with my eclipse IDE. Installing the drivers alone on my computer was not enough.
After I switched the USB debugging on, my eclipse detected the new device without any problems and I could start developping a new app for my tablet.
To turn on USB debugging, go to:

Settings > Applications > Development > USB debugging


give it a try!

Tuesday, November 22, 2011

OpenGL and AdMob

In one of my projects I needed to integrate AdMob into an GLSurfaceView. This is how I made it:

1. Get the GoogleAdMobAds.jar from Google itself: http://code.google.com/mobile/ads/ and add it to your buildpath.

2. Add a new xml file to your layout folder, let's say "main.xml": This file needs to contain an android.opengl.GLSurfaceView and a com.google.ads.AdView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">
http://schemas.android.com/apk/res/android</a>"
xmlns:ads="<a href="http://schemas.android.com/apk/lib/com.google.ads">
http://schemas.android.com/apk/lib/com.google.ads</a>"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
 
<android.opengl.GLSurfaceView
android:id="@+id/glSurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|left" />
 
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/AD_MOB_PUBLISHER_ID" />
 
</FrameLayout>

3. Add following code to your activity:

private GLSurfaceView surface;
private GlRenderer renderer; // your renderer implementation
private AdView adView;
 
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
 
setContentView (R.layout.main);
 
surface = (GLSurfaceView) this.findViewById (R.id.glSurface);
renderer = new GlRenderer(this); // your renderer implementation
surface.setRenderer(renderer);
 
AdRequest adRequest = new AdRequest();
// adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(adRequest);
}

4. Furthermore you need to define the AdActivity in your AndroidManifest.xml

<activity
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|
screenSize|smallestScreenSize"
android:name="com.google.ads.AdActivity" />

Note: Make sure you're compiling against at least Android v3.2 (set target in default.properties to android-13). Otherwise it won't work!


5.
AdMob requires following permissions to work properly. Add them to your AndroidManfiest.xml too.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Let me know if it worked in your case!