Java Reference
In-Depth Information
How do I use these classes ( Geocoder and Location ) from Java? Just instantiate them
and call methods as usual. In the previous section I started accumulating JUnit 4 tests into
a test class. Here's another test to add to that set:
@Test
public void testGeocoder() {
Location loc = new Location();
loc.setState("1600 Pennsylvania Avenue");
loc.setCity("Washington");
loc.setState("DC");
Geocoder geocoder = new Geocoder();
geocoder.fillInLatLong(loc);
assertEquals(38.895,loc.getLatitude(),0.001);
assertEquals(-77.037,loc.getLongitude(),0.001);
}
It doesn't get much easier than that. I don't need to instantiate a script engine or worry
aboutGroovyshellsorclassloaders.Justinstantiate andpopulate a Location ,instantiate
a Geocoder , and invoke the desired method.
From now on all of the examples I show will do integration the easy way. Again, this isn't
a value judgment against all the techniques demonstrated earlier in the chapter. If you want
to call an existing Groovy script from Java, or you're required to keep Java and Groovy
code separate in your application, the previous mechanisms all work. Freely intermixing
classes the way this script does, however, is very easy.
One last issue remains before I start looking at how Groovy might help Java. So far in this
chapterthegoalwasalwaystocallGroovyfromJava.Whatabouttheotherdirection?How
do you call Java from Groovy?
3.2.5. Calling Java from Groovy
Actually, this is so easy it hardly deserves a section at all. I've already shown it more than
once. Remember the earlier example using the Google V2 geocoder (reproduced here for
convenience)?
Search WWH ::




Custom Search