Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

March/2024
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
242526272829 30
31      

[ Archives News
for 'Java Technology' ]

home > news > java technology > stringtemplate part 4:generating a builder class using stringtemplate

StringTemplate Part 4:Generating a Builder Class using StringTemplate

Parts 1, 2 and 3 introduced StringTemplate through the use of simple SQL, HTML and plain text examples. This article switches gears and attempts to use StringTemplate to generate a more complicated text output: Java source code. In “Effective Java: Second Edition” (go read it, no really go read it) Mr. Block puts forth the idea of using a “Builder” class to help construct complicated objects. His builder uses a “fluid interface” that also has the extra side effect of looking like named parameters. Here is how you might use a builder class to create Planet objects. Planet mercury = new PlanetBuilder().name("Mercury").mass(0.06f).build(); System.out.println(mercury); Planet mars = new PlanetBuilder().name("mars").diameter(0.532f) .mass(0.11f).orbitalRadius(1.52f).orbitalPeriod(1.88f) .inclinationToSunsEquator(5.65f).orbitalEccentricity(0.093f) .rotationPeriod(1.03f).moons("Phobos","Deimos") .rings(false).atmosphere("CO2,N2").build(); System.out.println(mars); The only issue I have with this type of API is that it requires a lot of maintenance. Every time a property is added to the target class another method must be added to the builder and that data needs to be factored into the build method. I believe that with StringTemplate and a dash of reflection we can generate a builder for any (Bean) class. Lets take a look at the Planet class to see what we are up against. ...


Date: June, 13 2010
Url: http://www.java.net/blog/aberrant/archive/2010/06/13/stringtemplate-part-4generating-builder-class-using-stringtemplate


Others News

©2002-2019Java-Virtual-Machine.net https://mobirise.com/ | html website maker | free web editor | best website design software | free bootstrap templates