开发者

In JSP/Java how can I replace getter/setter by s.th. more generic?

I am getting tired by adding tons of getters/setters all the time in my beans. Is there a simple way to use annotations to get rid of this stupid work? or any other way? The 2nd example is the short version, which I would like to run, since there is no need to encapsulte my members (though in another context it might be neccessary). In my real world I have to access about 15 classes with about 10 data members in each class which would produce 300 useless setters/getters.

Example TestPerson.java (works):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

Example TestPerson.java (does NOT work):

public class TestPerson {
  public String firstName;
  public String lastName;
  public TestPerson() {
    firstName = "Hugo";
  }
}

Example test.jsp

<jsp:useBean id="testperson" class="test.TestPerson" scope="request" />
<html>
<head><title>Test</title></head>
<body>
<h2>Results</h2>${testperson.firstName}<br>
</body&开发者_如何学Cgt;
</html>


Project Lombok solves this (and much more), and it has support for both Eclipse and Netbeans.


Just have your IDE to autogenerate them. In Eclipse for example, define some properties, rightclick source, choose Source and then Generate Getters and Setters.

In JSP/Java how can I replace getter/setter by s.th. more generic?


Have you considered using the Groovy scripting language? It is based on Java and generates Java Bytecode.

See this link: http://groovy.codehaus.org/Groovy+Beans the getters and the setters are implicit.


There's also scala as well:

class foo {
   @BeanProperty
   var s1 : String,
   @BeanProperty
   var i1 : int
}

You could use the scala compiler to generate java byte code, which would create this class, along with the appropriate private fields, and getter and setter methods.

However, assuming you want to stick with plain java, i doubt java will see first class property support any time soon. Your best bet is some kind of code generation. It would be possible to generate these via annotations and annotation handlers, though this might give eclipse fits. EDIT: This is what Lombok (as mentioned above) appears to be doing. It's generating source code for you at compile time based on the annotations.


Try write-it-once. Template based code generator. You write custom template using Groovy, and generate file depending on java reflections. It's the simplest way to generate any file. You can make getters/settest/toString by generating AspectJ or java files, SQL based on JPA annotations, inserts / updates based on enums and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜