Archive

Archive for the ‘java’ Category

Where’s my Java reflection?, Part II

January 30th, 2011

Just to give the final implementation started in “Where’s my Java reflection“, here it goes the implementation of the generator for the method bindErrors (check last post “Where’s my Java reflection“ to get into context):

private void composeBindErrorsMethod(TreeLogger logger,
			SourceWriter sourceWriter, TypeOracle typeOracle) {

		sourceWriter.println("public void bindErrors("
				+ parameterizedType.getQualifiedSourceName()
				+ " object, Map<String, List<String>> errors) {");

		// Get the fields declared in the parameterized type
		JField[] fields = parameterizedType.getFields();
		for (JField field : fields) {

			JClassType classType = field.getType().isClass();
			if (classType != null) {

				JClassType erroableType = typeOracle.findType(Errorable.class
						.getName());

				if (classType.isAssignableTo(erroableType)) {

					sourceWriter.println("if (errors.containsKey(\""
							+ field.getName() + "\")){");
					sourceWriter.println("object." + field.getName()
							+ ".setErrors(errors.get(\"" + field.getName()
							+ "\"));");
					sourceWriter.println("}");

				}
			}

		}
		sourceWriter.println("}");

Again, hope it helps anyone.

GWT, java, programming, WEB 2.0

Where’s my Java reflection?

January 30th, 2011

Some people like it, others don’t. I like it, it’s so cool to make generic code, but as I ‘ve been experimenting with GTW to know it’s capabilities I found that GWT does not support Reflection. It uses instead a mechanism called Deffered Binding to overcome the lack of reflection.

When doing some kind of framework code with GWT, soon you’ll realize that you need reflection, as I did. Bad luck, you’ll have to dig into the underground features of GWT, namely deferred bindings and generators. You can learn the theoretical traits of Deferred Bindings in the documentation: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html

Let me explain my scenario.

Read more…

GWT, java, Javascript, Software, WEB 2.0

Expressive programming in Java

November 20th, 2010

In Ruby On Rails, for example, you can use a construct like this:

raise ActiveRecord::RecordNotFound unless condition

It’s simple to understand and easier to use, though you can also use the more classical format

if !condition
    raise ActiveRecord::RecordNotFound
end

Because of that I like also to use this kind of expressiveness in Java. Instead of thinking like:

if this then do that

I like to think like:

do that if this

Of course, is a matter of felling well while programming :)

Let’s take an example in Java. In this example there’s a simple API to put something in a cache and you will use a “key” to identify your objects in the cache. But because I, as API designer, don’t want you to use the string “key” as key, I will code the cache like this:

package eu.jpereira.exprogramming.cache;
public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    if ( key.equals("key") ) {
      throw new Exception("Hey dude, key cannot be 'key'!!");
    }
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }
}

But I prefer doing something like this:

package eu.jpereira.exprogramming.cache;
import static eu.jpereira.exprogramming.cache.ExceptionRaiser.*;

public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    throwIfTrue(new Exception("Hey dude, key cannot be 'key'!!"),key.equals("key"),);
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }
}

And the throwIfTrue method is coded like this:

package eu.jpereira.exprogramming.cache;

public class ExceptionRaiser {

  public static <T extends Throwable> void throwIfTrue(T exception, boolean condition) throws T {
    if (condition) {
      throw exception;
    }
  }
}

Even If at the first sight it may seems to be a lot of more work, it’ll pay off with my amusement while programming :) Moreover, it’s easier to understand and to program like this.

java, programming, Ruby, Software

One line of code paranoia

November 12th, 2010

If you have some java method like this:

public void doSomething(String oneArg, Object otherArg, Map<String, Object> mapArgs);

You can use your method somewhere in you code like this:

Map<String, Object> params = new HashMap<String, Object>();
params.add("key1", "Value1");
params.add("key1", "Value1");
doSomething("str1", someInstance, params);

Or if you’re paranoid about the number of lines of code you write, you can do something like:

doSomething("str1", someInstance, new HashMap<String, Object>(){{put("key1", "Value1");put("key1", "Value1");}});

Basically, you’re overriding the new anonymous HashMap and adding it a new initialization block…

java, Software

Look for the obvious first…

October 14th, 2010

As I like to experiment cool web development frameworks, I decided to make a quick tour on Play! Framework. It was queued :)

So, here I was trying the persistence from the framework, that is basically JPA 2.0 with Hibernate as the persistence provider. I was also very interested in knowing what the framework could provide me for data validation, you know, that basic stuff that a framework should give us for free before it can be called wed framework.

I started with a very simple JPA entity like this one:

Read more…

java, Software