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 />

Instanceof<br />

The instanceof operator in Java allows a developer to determine if its first operand<br />

is an instance of its second.<br />

operand1 instanceof operand2<br />

You can use instanceof in ArcObjects when the logic behind the type is held in<br />

Java. You cannot use instanceof when the type is held in ArcObjects, as the logic<br />

of determining whether an object is an instance of a specified type resides in the<br />

constructors of that object type and not the JVM.<br />

Point point = new Point();<br />

point.putCoords(10, 10);<br />

if(point instanceof IGeometry){<br />

System.out.println(" point is a IGeometry");<br />

geom = point;<br />

}<br />

if(point instanceof IClone){<br />

System.out.println(" point is a IClone");<br />

}<br />

The above code works since the type information is held in Java for Point.Java.<br />

When you construct a Point object, a proxy class for each implemented interface is<br />

also constructed. This allows you to use instanceof on any of these types. <strong>Developer</strong>s<br />

would have access to any methods on Point implementing the IGeometry or<br />

IClone interfaces.<br />

This is backwards compatible as well:<br />

if(geom instanceof Polyline){<br />

System.out.println(" geom is a Polyline");<br />

}<br />

else if(geom instanceof Point){<br />

System.out.println(" geom is a Point");<br />

pnt = (IPoint)geom; // allowable cast as the type is held in JVM<br />

}<br />

Since a direct cast of the geom object into Point was created, the geom object is of<br />

type Point and instanceof can be used to check this information. However, since<br />

the type information was known before it was checked above, it is not extremely<br />

useful. What would be useful is to apply the above logic on methods that return<br />

objects of superinterfaces.<br />

Consider the IWorkspaceFactory.openFromFile method, which returns an IWorkspace.<br />

Since the object returned is a Java object that implements IWorkspace, you cannot<br />

check if the returned object is of any of the known implementing classes that<br />

implement IWorkspace. In this case, to check for type information, you should call<br />

a method on the returned object that is expected. If the method does not throw<br />

an exception, it is of that type. This occurs because the logic on this object is<br />

declared at runtime and is held inside the underlying ArcObjects component.<br />

RasterWorkspaceFactory rasterWkspFactory = new RasterWorkspaceFactory();<br />

IWorkspace wksp = rasterWkspFactory.openFromFile( aPath, 0 );<br />

if(wksp instanceof RasterWorkspace){<br />

/* Code does not execute as logic is in ArcObjects. */<br />

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

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

Saved successfully!

Ooh no, something went wrong!