4

I have a small question. I'm trying to create templates for getters for my variables inside of Eclipse. What I want to do in my getter method is to check if the variable is null or not. If it is null I want to assign a value to it. However the problem is I need to cast the return value of the method to the getter return type. I couldn't manage it. Here is the code that I'd like to have:

Integer someInt;
Double someDoub;
Long someLong;

public Integer getSomeInt(){
    if(someInt == null) someInt = (Integer) new Generator().evaluate();
    return someInt;
}

public Double getSomeDoub(){
    if(someDoub == null) someDoub = (Double) new Generator().evaluate();
    return someDoub;
}

This is the code that I want to generate. Here is what I typed as a template:

if( ${field} == null){
    ${field} = ( ${return_type} ) new Generator().evaluate();
}
return ${field};

As soon as I type this. Eclipse says that return_type is unknown. Please help.

Thank you very much for your time.

2 Answers2

1

This will do your job:

 if( ${field} == null){
        ${field} =   ${field}.getClass().cast( new Generator().evaluate());
    }
    return ${field};
Pranalee
  • 3,389
  • 3
  • 22
  • 36
  • 1
    A good idea, but as written this will not work because if the field is null, you can't call `getClass()` on it. You could store the result from `evaluate` to a temporary `Object` variable, then call `getClass()` on that to do the cast. – matts Jun 08 '12 at 17:36
  • 1
    oops.. i completely missed that.. good catch @matts. but if we create temp obj from `evaluate` and do `getClass` on it , it'll return `Object` and not `Integer` – Pranalee Jun 08 '12 at 17:44
  • Oh, shoot, you're right. I was thinking the object's `getClass()` would still return the `Class` object for the right type. But the compiler will still only know that the `cast` method is returning an `Object` even if it's really returning an `Integer` at runtime. I guess that won't work either... – matts Jun 08 '12 at 17:49
1

Eclipse doesn't provide a way to do this in getter/setter code templates (i.e., the ones that the "Generate Getters and Setters" tool uses). The variables on the "Insert Variable" list are the only ones supported.

${return_type} is only available for use in regular templates (i.e., the type you might invoke using code completion hotkeys).

As a possible workaround, you could create a generified static factory method to produce the default objects, avoiding the need for a cast:

public class MyBean {
    Integer someInt;
    Double someDoub;

    public Integer getSomeInt(){
        if (someInt == null) someInt = GeneratorUtil.createAndEvaluate();
        return someInt;
    }

    public Double getSomeDoub(){
        if (someDoub == null) someDoub = GeneratorUtil().createAndEvaluate();
        return someDoub;
    }
}

public class GeneratorUtil {
    @SuppressWarnings("unchecked")
    public static <T> T createAndEvaluate() {
        return (T) new Generator().evaluate();
    }
}

Does your Generator class use some type of reflection to determine what type of object to generate?

matts
  • 6,738
  • 1
  • 33
  • 50
  • No I'm not using reflection. I have a bunch of POJO classes, which are named same as variable names. The code snippet that I gave is actually not what I exactly have, it was just to simplify the question. In my case "Generator" class differs depending on the variable all of which implements an interface that contains a signature for evaluate method. So I cannot have a utility class. Thank you for the idea though. –  Jun 08 '12 at 17:58
  • @Mehmet Yesin : can u post ur Generator class code? That will help us to provide u more suitable answer. i think what you are trying to achieve is difficult to implement by getter template – Pranalee Jun 08 '12 at 18:03
  • @MehmetYesin so did you want the getter template to put in the correct generator class, or were you planning to go through and write in the correct generator for each getter? – matts Jun 08 '12 at 18:06
  • Let me explain it like this. There are a number of classes(a lot) which represents some data. All of these classes are implementing evaluate method and returning a concrete type. Either Integer, Double, Boolean, Long or String. In my template I'm actually saying ${field} = ( RETURN_TYPE ) new ${field}.evaluate(); because my classes are named same as variable names, which is partially encapsulating them. Hope I explained it a little better. Sorry for the confusion, I created. –  Jun 08 '12 at 18:15
  • Is it possible to modify your generator interface to be generified? Something like `interface Generator { E evaluate(); }`, so then each generator class would specify what type of object it generated, like `class SomeIntGenerator implements Generator { public Integer evaluate() { return 0; } }`. – matts Jun 08 '12 at 19:30
  • @matts Thank you for your time. The system is pretty complex and used by other teams as well. I guess, I'll just go ahead and modify those parts manually. –  Jun 08 '12 at 19:47
  • No problem. Sorry we couldn't come up with a better solution for you. You might be able to use the [Fast Code plugin](http://fast-code.sourceforge.net/getter-setter.html) to do it though, but I don't have any experience with it. – matts Jun 08 '12 at 19:53