parsing geojson using jackson - java

I have some GeoJSON which corresponds to a PostGIS Point object.
Here is an example:
{"type":"Point","coordinates":[-397408.355686851020437,7575590.819041009992361]}
I tried to parse this into a postgis point object in Java by doing:
ObjectMapper mapper = new ObjectMapper();
Point coors = null;
try {
coors = mapper.readValue(str, Point.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I get the following JsonMappingException:
org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:346)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:188)
at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2820)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2719)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at dao.impl.PostGisDaoImpl.getOsmPoiNodes(PostGisDaoImpl.java:53)
at service.OsmDBService.main(OsmDBService.java:24)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
at org.codehaus.jackson.map.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:194)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:1065)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:654)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:583)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:432)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:341)
... 8 more
Does anyone know what is wrong here?
Thanks
EDIT:
New Exception:
org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:346)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:596)
at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:438)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:383)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:188)
at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2820)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2719)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at dao.impl.PostGisDaoImpl.getOsmPoiNodes(PostGisDaoImpl.java:53)
at service.OsmDBService.main(OsmDBService.java:24)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
at org.codehaus.jackson.map.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:194)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:1065)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:654)
at org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:583)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:432)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:341)
... 14 more

The reason for the issue is that it sees both the definition of the variable an the setter method for the variable so double x; and setX(double x) as it being defined twice. You need to tell it to pick one or another by adding the #XmlAccessorType at the top of your class and setting it to XmlAccessType.FIELD or whichever you prefer it to use. For example:
#XmlAccessorType(XmlAccessType.FIELD)
public class Point {
...
}
If you are using Point from here it is different than what I expected. It is because it actually has two set methods: void setX(double x) and void setX(int x)
So in this case you want field access. Still looking to see how to set it programmatically instead of annotation.
UPDATE: I think in this case your best bet is to create a wrapper for the Point class that you can then pull the point back out of. So something like:
public class MyPoint {
private String type;
#JsonIgnore
public Point point;
public MyPoint(){
this.point = new Point();
}
public double[] getCoordinates(){
return new double[] {this.point.getX(), this.point.getY()};
}
public void setCoordinates(double[] x){
if (x.length == 2){
this.point.setX(x[0]);
this.point.setY(x[1]);
}
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
And then change your code to:
ObjectMapper mapper = new ObjectMapper();
Point coors = null;
try {
MyPoint mine = mapper.readValue(str, MyPoint.class);
coors = mine.point;
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe there is a better way, but this should work.
EDIT:
This is the test I ran to see if this worked:
public static void main(String[] args) {
String str = "{\"type\":\"Point\",\"coordinates\":[-397408.355686851020437,7575590.819041009992361]}";
ObjectMapper mapper = new ObjectMapper();
Point coors = null;
try {
MyPoint mine = mapper.readValue(str, MyPoint.class);
coors = mine.point;
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(coors);
}
And the output that I get:
POINT(-397408.355686851 7575590.81904101)

Exception points out the problem, which you can see from the Javadoc you linked to. There are two alternate setters:
void setX(double x)
void setX(int x)
and Jackson doesn't want to guess which one it should try to use. Having a field and setter is NOT problematic; methods (setter) have precedence over fields.
Since you can't modify Point, you will probably want to use mix-in annotations.
Or custom deerializer if you prefer.

Related

How to check if an object has at least one field that is not null?

I have a set of beans that are generated by a third-party library.
How can I check if each bean has at least one field that is not null?
The problem is easily solved using reflection. Just add this method to your bean:
public boolean hasAtLeastOneNonEmpty() {
Class<? extends QueryBean> class1 = this.getClass();
Field[] fields = class1.getDeclaredFields();
for (Field field : fields) {
try {
if (field.get(this) != null) {
return true;
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}

Mismatch of return datatype

i am facing a problem regrading specifying the return data type. I have the FOComp class which implements callabale, the call() method of the 'FOComp' returns data type List<ArrayList<Mat>> as shown in the code of 'FOComp' class below.
and the method 'getResults()' returns data of type ArrayList<Mat> as shown in the code below. and currently, at run time, when I execute the code, I receive the folowing error:
Multiple markers at this line
The return type is incompatible with Callable<ArrayList<Mat>>.call()
The return type is incompatible with Callable<List<Mat>>.call()
kindly please let me know how to fix it.
'FOComp' class:
static class FOComp implements Callable<List<Mat>> {//should return list contains 4 mats(0,45,90,135)
private ArrayList<Mat> gaussianMatList = null;
private List<ArrayList<Mat>> results_4OrientAngles_List = null;
public FOComp(ArrayList<Mat> gaussianMatList) {
// TODO Auto-generated constructor stub
this.gaussianMatList = gaussianMatList;
this.results_4OrientAngles_List = new ArrayList<ArrayList<Mat>>();
}
public List<ArrayList<Mat>> call() throws Exception {
// TODO Auto-generated method stub
try {
featOrient = new FeatOrientation(this.gaussianMatList);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
featOrient.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.results_4OrientAngles_List.add(featOrient.getResults());
return results_4OrientAngles_List;
}
}
'getResults':
public ArrayList<Mat> getResults() {
if (this.crossAddOrientMapsList != null) {
if (!this.crossAddOrientMapsList.isEmpty()) {
if (this.crossAddOrientMapsList.size() == 4) {
double[] theta = new double[4];
theta[0] = 0;
theta[1] = 45;
theta[2] = 90;
theta[3] = 135;
for (int i = 0; i < this.crossAddOrientMapsList.size(); i++) {
MatFactory.writeMat(FilePathUtils.newOutputPath("FinalCrossAdd_" + theta[i]+"_degs"), this.crossAddOrientMapsList.get(i));
//ImageUtils.showMat(this.crossAddOrientMapsList.get(i), "OrientMap_" + theta[i] + " degs");
}
return this.crossAddOrientMapsList;
} else {
Log.WTF(TAG, "getResults", "crossAddOrientMapsList != 4 !!");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is empty.");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is null");
return null;
}
}
class FOComp implements Callable<List<Mat>>
and
public List<ArrayList<Mat>> call()
aren't really compatible... Your call() method should be
#Override public List<Mat> call()
Also, it is good practice to avoid implementation classes in method signatures, use the interfaces instead (in this case, use List rather than ArrayList). That will also fix your problem with one of the "multiple markers" :-)
Cheers,
You class declaration says that you are going to return a List of Mat (FOComp implements Callable<List<Mat>>), but your call method signature says you are going to return a List of ArrayList of Mat (List<ArrayList<Mat>>).
You will need to make them consistent.

How can I ignore specifically annotated classes under TestNG?

Goal: Ignore test classes on runtime which have custom annotation set.
What I tried:
public void onStart(ITestContext context) {
if (context instanceof TestRunner) {
Map<Class<?>, ITestClass> notSkippedCl = new HashMap<Class<?>, ITestClass>();
TestRunner tRunner = (TestRunner) context;
Collection<ITestClass> testClasses = tRunner.getTestClasses();
for (Iterator<ITestClass> iterator = testClasses.iterator(); iterator.hasNext();) {
ITestClass rr = iterator.next();
Class<?> realClass = rr.getRealClass();
if (chechAnnotation(realClass))
{
notSkippedCl.put(realClass,rr);
}
}
try {
Field field = TestRunner.class.getDeclaredField("m_classMap");
field.setAccessible(true);
Map<Class<?>, ITestClass> mapClass = (Map<Class<?>, ITestClass>) field.get(tRunner);
mapClass.clear();
mapClass.putAll(notSkippedCl);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
onStart method is called before all test classes in package, So I get TestRunner here, which contains map of all test classes. I iterate throw each one, checking it Annotation and if I find one I add to new map. Then I override map of TestRunner. I was thinking that this will help me ignore classes without annotation, but I was wrong.
Maybe someone knows right solution, to Ignore test classes depending on custom annotation?
(parameter of the method cannot be changed)
P.S. setting #Test(enabled=false) annotation is not a solution in my situation
--EDIT_FAUND_SOLUTION--
I managed to create solution, not sure if there was easier way, but this works:
#Override
public void onStart(ITestContext context) {
if (context instanceof TestRunner) {
Set<ITestNGMethod> methodstodo = new HashSet<ITestNGMethod>();
TestRunner tRunner = (TestRunner) context;
ITestNGMethod[] allTestMethods = tRunner.getAllTestMethods();
SupportedBrowser currentBrowser = HelperMethod.getCurrentBrowser();
for(ITestNGMethod testMethod : allTestMethods)
{
Class<?> realClass = testMethod.getTestClass().getRealClass();
Set<SupportedBrowser> classBrowsers = getBrowsers(realClass);
if (classBrowsers.contains(currentBrowser)) {
methodstodo.add(testMethod);
}
}
try {
Field field = TestRunner.class.getDeclaredField("m_allTestMethods");
field.setAccessible(true);
field.set(tRunner, methodstodo.toArray(new ITestNGMethod[methodstodo.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I'd recommend to create org.testng.IMethodInterceptor as a Listener. TestNG calls intercept method just before test suite starts. You get list of all methods as a parameter and have to return modified/new/etc. list with methods you want to run. See documentation http://testng.org/doc/documentation-main.html#methodinterceptors for more details and examples.

NoSuchMethodException loading Build.getRadioVersion() using reflection

I'm trying to load the radio version of the Android device using reflection. I need to do this because my SDK supports back to API 7, but Build.RADIO was added in API 8, and Build.getRadioVersion() was added in API 14.
// This line executes fine, but is deprecated in API 14
String radioVersion = Build.RADIO;
// This line executes fine, but is deprecated in API 14
String radioVersion = (String) Build.class.getField("RADIO").get(null);
// This line executes fine.
String radioVersion = Build.getRadioVersion();
// This line throws a MethodNotFoundException.
Method method = Build.class.getMethod("getRadioVersion", String.class);
// The rest of the attempt to call getRadioVersion().
String radioVersion = method.invoke(null).toString();
I'm probably doing something wrong here. Any ideas?
Try this:
try {
Method getRadioVersion = Build.class.getMethod("getRadioVersion");
if (getRadioVersion != null) {
try {
String version = (String) getRadioVersion.invoke(Build.class);
// Add your implementation here
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.wtf(TAG, "getMethod returned null");
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What Build.getRadioVersion() actually does is return the value of gsm.version.baseband system property. Check Build and TelephonyProperties sources:
static final String PROPERTY_BASEBAND_VERSION = "gsm.version.baseband";
public static String getRadioVersion() {
return SystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION, null);
}
According to AndroidXref this property is available even in API 4. Thus you may get it on any version of Android through SystemProperties using the reflection:
public static String getRadioVersion() {
return getSystemProperty("gsm.version.baseband");
}
// reflection helper methods
static String getSystemProperty(String propName) {
Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties");
Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
return tryInvoke(mtdGet, null, propName);
}
static Class<?> tryClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
static Method tryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) {
try {
return cls.getDeclaredMethod(name, parameterTypes);
} catch (Exception e) {
return null;
}
}
static <T> T tryInvoke(Method m, Object object, Object... args) {
try {
return (T) m.invoke(object, args);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());
} catch (Exception e) {
return null;
}
}

In Java Invoking a method using Reflection is not able to find method, when method has baseclass as parameter

Below is the code snippet, I am trying to invoke the usingClass method using REFLECTION. Calling the usingClass() method directly(w/o reflection) works when I pass an object of type Child, though when I try to achieve the same thing using Reflection it throws NoSuchMethodFoundException. Would like to understand if I am missing something or is there any logic behind this? Please help
package Reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethodInvocation {
/**
* #param args
*/
public static void main(String[] args) {
TestMethodInvocation test = new TestMethodInvocation();
Child child = new Child();
Parent parent = (Parent)child;
Class<? extends Parent> argClassType = parent.getClass();
Class<? extends TestMethodInvocation> thisClassType = test.getClass();
test.usingClass(child);
Method methodToCall;
try {
methodToCall = thisClassType.getDeclaredMethod("usingClass", argClassType);
methodToCall.invoke(test, parent);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void usingClass(Parent p){
System.out.println("UsingClass: " + p.getClass());
}
}
Output is as below.
UsingClass: class Reflection.Child
java.lang.NoSuchMethodException: Reflection.TestMethodInvocation.usingClass(Reflection.Child)
at java.lang.Class.getDeclaredMethod(Unknown Source)
at Reflection.TestMethodInvocation.main(TestMethodInvocation.java:20)
The reason your code does not work is that getClass() is dynamically bound. Casting to Parent does not affect the runtime type of your object and so the variables child and parent contain the same class object.
Unless you explicitly query your instance for its parent class via getGenericSuperclass() or something similar, you will have to use the static way mentioned by dystroy.
You should use
methodToCall = thisClassType.getDeclaredMethod("usingClass", Parent.class);
because the precise exact class of parent (which is Child), is used at runtime and the type of the variable holding it changes nothing.
Another (too heavy) way to solve it would be :
Class<? extends Parent> argClassType2 = (new Parent()).getClass();
...
methodToCall = thisClassType.getDeclaredMethod("usingClass", argClassType2);

Categories

Resources