Does Java codemodel support GenericEntity - java

Does Java codemodel support GenericEntity ?
I am trying to generate a code like below using jcodemodel:
Object obj = new GenericEntity<List<java.lang.String>>(listStr){}
But I am not able do that. I used below code :
JType jObjType = ((JClass) jcodemodel._ref(GenericEntity.class)).narrow(jcodemodel.ref(List.class).narrow(
jcodemodel.ref(String.class)));
JVar jvobj = jMethodResource.body().decl(jcm.ref(Object.class), "obj", JExpr._new(jObjType).arg(.....listStr reference...));
The code obtained using this is as follows :
Object obj = new GenericEntity>(listStr)
But "{}" is missing.
Can anyone help me here? How do a code so that I am obtained with {} :
Object obj = new GenericEntity<List<java.lang.String>>(listStr){}

Use this method (https://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JCodeModel.html#anonymousClass(com.sun.codemodel.JClass)). Something like that:
JClass listOfEmplType = jcodemodel.ref(List.class).narrow(jcodemodel.ref(
Emplyee.class.getName()));
JVar listOfEmpl = jMethodResource.body().decl(listOfEmplType, "listStr", JExpr._null());
JClass jObjType = ((JClass) jcodemodel._ref(GenericEntity.class)).narrow(listOfEmplType);
JVar jvobj = jMethodResource.body().decl(jcodemodel.ref(Object.class), "obj",
JExpr._new(jcodemodel.anonymousClass(jObjType)).arg(listOfEmpl));

Related

How to map Json (from procedure) to Java object

I have the following SP (SQL server) that return a Json output.
BEGIN
SET #jsonOutput = (
SELECT
Program.Name AS ProgramName,
ProgramOwner.FirstName AS OwnerFirstName,
FROM ProgramOwner, Program
WHERE Program.Id = ProgramOwner.ProgramOwner2Program
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER)
I would like to map the return Json output to a List of ProgramDto via modelMapper. Not sure hot to do that since the return values from call.execute is an Object.
Something like this:
SimpleJdbcCall call = new
SimpleJdbcCall(jdbcTemplate).withProcedureName(programProc).declareParameters(
new SqlOutParameter("jsonOutput", Types.VARCHAR));
Map<String,Object>out = call.execute(new MapSqlParameterSource());
if(out.size()>0) {
// Only to show what I am trying to do
Type rootType = new TypeToken<List<ProgramDto>>() {}.getType();
modelMapper.map(out.get("jsonOutput"),rootType );
}
Thank you
As I understood you are trying to get a list of object from
You can use Jackson api
Like this
say for example your json is in variable named jsonData, then you can get the object you need like below.
ObjectMapper mapper = new ObjectMapper();
List<Type> myList = Arrays.asList(mapper.readValue(jsonData, Type[].class));
You can also find more examples here

How to convert Scala play.api.mvc.Result to Java play.mvc.Result in Play Framework

In Play framework (Java), is there a way to convert an object of type play.api.mvc.Result to an object of type play.mvc.Result?
In Play version 2.5.x you should check out play.core.j.JavaHelpers. I had to do exactly that conversion i.e. from a Scala play.api.mvc.Result to Java play.mvc.Result:
It goes like this e.g.
import play.core.j.JavaHelpers
def doLogin = Action { implicit request =>
val jContext = JavaHelpers.createJavaContext(request)
// ...
val sResult : play.api.mvc.Result = ...
val jResult : play.mvc.Result = JavaHelpers.createResult(jContext, sResult)
// now you can use jResult
}

Pass an array collection in JSON

I am trying to pass an array collection from flex page to my backend java.Here is the code,
private function getItems():void{
myObj = new Object();
myObj['dId']= dId.value.toString();
myObj['itmList']=JSON.encode(itmList);// trying to pass like this..
var url:String = URLManager.baseURL;
url = url+"myController/ReportController?do=getItems";
url = url+"&parameter="+ escape(JSON.encode(myObj))
var urlRequest:URLRequest = new URLRequest(url);
navigateToURL(urlRequest,"_blank");
}
My itmList is an array collection, how can I pass it from JSon to Java controller? And how to get that in Java?
JSON.encode the itmList source array instead. (i.e. itmList.source is an array)
Then use HTTPService instead:
HTTPService AsyncToken and AsyncResponder example
Another option is to use JSON.stringify instead (Flash has had native JSON support since FP 11). Just make sure you remove the import com.adobe.serialization.json.JSON; from the top of your file.
myObj['itmList']=JSON.stringify(itmList);
Or, since you're encoding your whole data object,
myObj['itmList']=itmList.source;
var url:String = URLManager.baseURL;
url = url+"myController/ReportController?do=getItems";
url = url+"&parameter="+ escape(JSON.stringify(myObj))
var urlRequest:URLRequest = new URLRequest(url);
navigateToURL(urlRequest,"_blank");

Change SIM PIN on Android using reflection

I would like to change SIM card PIN number using java reflection. Final app will be installed in system/app.
The code I'm using is:
String ICCCARD_CLASS = "com.android.internal.telephony.IccCard";
String PHONEBASE_CLASS = "com.android.internal.telephony.PhoneBase";
Object phoneBaseObject = Class.forName(PHONEBASE_CLASS).getConstructor();
Object iccCardObject = Class.forName(ICCCARD_CLASS).newInstance();
Method iccCardMethod = Class.forName(ICCCARD_CLASS).getMethod("changeIccLockPassword", String.class, String.class, Message.class);
//Method arguments are...
Object arglist1[] = new Object[3];
arglist1[0] = "1111"; //oldPass
arglist1[1] = "2222"; //newPass
arglist1[2] = new Message(); //message handler (not needed)
iccCardMethod.invoke(iccCardObject, arglist1);
But, I'm getting a lot of exceptions like "no such method", "instantiation exception"...
In my Android project packages for IccCard and PhoneBase are not created.
TNX Hackers!
It seems that compiling indeed requires android.jar to be rebuilt with modified classes.dex.

java code corresponding to Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings()) in .net?

I have a code in .net that serializes a request to the json format ... The code is something like this.
var ops = new Newtonsoft.Json.JsonSerializerSettings();
ops.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
ops.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
ops.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
ops.Converters.Add(new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter());
String strSO = Newtonsoft.Json.JsonConvert.SerializeObject(source,
bIndent ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None,
ops);
I tried the java code corresponding to this portion but it doesn't work.
From my understanding, the Newtonsoft serializer takes an object with member variables and outputs a json string that represents that object.
So you can do something like:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
And you'll get an output string like:
{"Name": "Apple",
"Expiry": "\/Date(1230375600000+1300)\/",
"Price": 3.99,
"Sizes": ["Small", "Medium", "Large"]
}
Now the bad news is that the BlackBerry library that you're using doesn't use reflection to examine the structure of objects it serialises. It is a formatter rather than a serializer.
The good news is that it is pretty easy to use. The documentation is here:
http://www.blackberry.com/developers/docs/6.0.0api/org/json/me/package-summary.html
In short, to write an object such as the one above, you would do something like:
myString = new JSONStringer()
.object()
.key("Name")
.value("Apple")
.key("Expiry")
.value("Date("+myDate.getTime()+")")
.endObject()
.toString();
..and so on. Note that you are constructing the JSON structure element by element, rather than having the JSON library assume that your object is the exact structure of the data you wish to output.
Hopefully this will give you some idea of how to proceed.
If your question is "Does anyone know of a Java equivalent to Newtonsoft.Json for .NET for serializing in JSON format?"
Check the bottom of http://json.org

Categories