mandag den 17. september 2012

Getting concrete java generic class used in sub classes

It is funny that you need to do wacky stuff like this in order to get the class of T used in subclasses when you make generics classes like <T extends SomClass>:

 private Class getGenericClass() 
 { 
  ParameterizedType pt = (ParameterizedType) getClass().getGenericSuperclass();
  Class clazz = (Class) pt.getActualTypeArguments()[0]; 
  return clazz;
 }

What is even more funny is that the information is clearly stored in the classes at runtime, so why the wacky code to get hold of the concrete class used?

I know this information is not used to do rumtime type check and so on. But some time you need to get access the the type (class) declared in the generic sub classes. For instance if you want to load some data using JPA getEntityManager().find(someEntityClass, anId). Using T.class or some thing like that would be lovely instead of the wacky code above.

mandag den 6. august 2012

The fantastic Grep Console eclipse plugin

One of the most usefull plugin I have found for Eclipse is the Grep Console eclipse plugin.

When i run JBoss in JBDS (a fantastic IDE from Jboss based on Eclipse), the console window get spammed when I get an exception, and its hard to find the code I'm interested in.

But then I found Grep Console and now I can paint in my console using simple regex patterns. It even have a nice config view that helps build the regex pattern. It makes locating packages from my code MUCH faster.


Image from Grep Console user guide displaying some of the default pattern and coloring.

torsdag den 2. august 2012

Classes in Seam 2 hot folder makes page load real slow

Seam 2 hot deploy is cool. It make sure that all seam beans in the hot folder is reloaded if changed. All Seam beans in the src/hot folder get reloaded runtime by seam, unless they are annoteted with @BypassInterceptors. This is cool compared to restarting the server.

But as you add classes to the hot folder it gets slower and slower to reload a page, and by the time you reach 400+ it added about 4-10 seconds to every page call. Of cause this only apply to the development build of seam, because in the live configuration all classes end up in the same normal non hot deploy folder and causes no problem.

The solution for me was to move all classes from hot to the main folder, and then have JRebel take care of the class reloading. This have the added value JRebel can reload all classes, even seam beans annotated with @BypassInterceptors.

It works like a charm, and the new version 5 is dead easy to set up in eclipse.

mandag den 30. juli 2012

Code syntax highlighter in blogger blogspot

Syntax highlighting is working!

There are a lot of blogs on this, but this is what I needed to do to get it to work:

Like every one else I use the fantastic SyntaxHighlighter by Alex Gorbatchev. To get it to work for me I had to host the scripts from SyntaxHighlighter my self. Loading it as described at http://alexgorbatchev.com/SyntaxHighlighter/hosting.html did not work for me. It resulted in a redirect that the browser couldn't handle. Any way, with that in place, I could configure the theme to load the files. This was done by Template > Edit HTML in the blogger admin page. This yield a warning that you have to be careful and know what you are doing. So I was careful while I added:
<link href='http://A_HOST/syntaxhighlighter/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://A_HOST/syntaxhighlighter/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://A_HOST/syntaxhighlighter/scripts/shCore.js' type='text/javascript'/>  
<script src='http://A_HOST/syntaxhighlighter/scripts/shBrushCss.js' type='text/javascript'/>  
<script src='http://A_HOST/syntaxhighlighter/scripts/shBrushJava.js' type='text/javascript'/>  
<script src='http://A_HOST/syntaxhighlighter/scripts/shBrushJScript.js' type='text/javascript'/>  
<script src='http://A_HOST/syntaxhighlighter/scripts/shBrushSql.js' type='text/javascript'/>  
<script src='http://A_HOST/syntaxhighlighter/scripts/shBrushXml.js' type='text/javascript'/>  

<script type='text/javascript'>
//<![CDATA[  
     SyntaxHighlighter.all();
//]]>  
</script>
just below the <title> line and before the <b:skin> tag, and hit the [Save Template] button.

Now all I have to do in order to pretty code is wrap the code in a pre tag that defines how to highlight like:

<pre class="brush: html" name="code">

Thats it.