18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

JAVA APPLICATION PROGRAMMING INTERFACE<br />

The return value is an Object, specified by the javadoc as “variant”. Therefore, the<br />

value can be downcasted to String or Short, depending on their type in the<br />

geodatabase being queried.<br />

Casting<br />

ArcObjects follows an interface-based programming style. Many methods use<br />

interface types as parameters and have interfaces as return values. When the<br />

return value of a method is an interface type, the method returns an object<br />

implementing that interface. When a method takes an interface type as parameter,<br />

it can take in any object implementing that interface. This style of programming<br />

has the advantage that the same method can work with many different object<br />

types, provided they all implement the same interface.<br />

For example, IFeature.getShape() method returns an object implementing IGeometry.<br />

The object returned could potentially be any one of the following classes that<br />

implement IGeometry: BezierCurve, CircularArc, EllipticArc, Envelope,<br />

GeometryBag, Line, MultiPatch, Multipoint, Path, Point, Polygon, Polyline, Ray,<br />

Ring, Sphere, TriangleFan, Triangles, or TriangleStrip.<br />

Casting is used to convert between types. There are three types of potential casts<br />

you, as a developer, may be tempted to use with the Java API:<br />

1. Interface to concrete class casting<br />

2. Interface cross-casting<br />

3. Interface downcasting<br />

It is important to understand that objects returned from methods within<br />

ArcObjects can behave differently than objects implicitly defined in your code<br />

because the object reference is not held in the JVM.<br />

If you have a method, doSomeProcessingOnPolygon(Polygon p), that operates only on<br />

Polygon objects, and you want to pass the object obtained as a result of<br />

IFeature.getShape, you need a way to convert the “type” of the object from<br />

IGeometry to Polygon. In Java, this is done using a class cast operation:<br />

/* incorrect usage: will give ClassCastException */<br />

Polygon poly = (Polygon)geom;<br />

However, if you use the same code with the <strong>ArcGIS</strong> API for Java, you will get a<br />

ClassCastException. The reason for the exception is that the “geom” object reference<br />

is actually a reference to the native ArcObjects component. As a consequence<br />

of the interoperability between Java and the native ArcObjects components,<br />

the logic of casting this object reference to the Polygon object resides in the<br />

constructor of the Polygon object and not in the JVM.<br />

Every class in the <strong>ArcGIS</strong> API for Java has a constructor that takes in a single<br />

object as a parameter. This constructor can create the corresponding object using<br />

the reference to the ArcObjects component. Therefore, to achieve the equivalent<br />

of a class casting when using the <strong>ArcGIS</strong> API for Java, use the “object constructor”<br />

of the class being casted to.<br />

Polygon poly = new Polygon(geom);<br />

The following code illustrates the object constructor being used to cast the geom<br />

object to a Polygon:<br />

Chapter 4 • <strong>Developer</strong> environments • 189

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!