I am trying to come up with a design for a method that takes another method as a parameter and retrieves the parameter values of the method passed. How can this be done? I've tried using java.lang.reflect.* but can't seem to find an API that supports this.
You can't really get the values passed as parameters like this.
You can make your own Proxy and from there capture parameters before calling the right method. Or with aspect you could get the parameters value directly when the method is called.
Method.getParameterTypes
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html#getParameterTypes%28%29
Related
I have a json template as below
{
"Account_Number" : "${accNo}"
}
I want to use a user-defined directive which is basically a Java code to check if accNo is greater than 0. If not, I want to set the value as 0000.
I was reading here ( https://freemarker.apache.org/docs/pgui_datamodel_directive.html) that it is possible to write Java code by implementing the TemplateDirectiveModel interface. However, I was unable to retrieve the value of accNo.
Is it possible to achieve the above using user-defined directive? If yes, how?
You want something like <#accNoJson value=accNo />, then you can get it from params parameter of TemplateDirectiveModel.execute. If you want something like <#accNoJson/> (which is a bit odd), then you can get it with env.getVariable("accNo"), where env is the 1st parameter of TemplateDirectiveModel.execute.
The most typical approach would be ${accNoJson(accNo)}, in which case you should implement TemplateMethodModelEx.
I've been using Amazon Glacier via the Amazon Java SDK.
I'm struck that parameters are passed around via an object, rather than as individual parameters.
For example, to retrieve the output of a job, where the parameters are Vault, JobId,range, the following technique is used:
client.getJobOutput(new GetJobOutputRequest(Vault, JobId, range));
Instead of:
client.getJobOutput(Vault, JobId, range);
What are the pros and cons of the two approaches?
Pros:
If your method takes many parameters, using a parameter object makes the method signature sane.
If you want to take additional parameters for the method later, using a parameter object means that you just have to add another field in the param object and the method signature need not change.
If you want a batch version of the method, just pass a list of param objects.
Cons:
Extra verbosity
Another level of indirection
My General Question is: Is it inefficient/bad practice to call preparedStatement.executeBatch() if there's only one query in the batch?
I'm writing a generic method for the Java Helper Library to execute a query. There's a javabean called HelperQuery which holds a list of arrays another javabean called QueryParameter which holds a type (like STRING, BLOB, INT, etc.) and a value. The QueryParameter is used to fill the HelperQuery's PreparedStatement. In many cases, there will be only one array of QueryParameters.
My Specific Question is: Should I handle things differently if there's only one array of QueryParameters or would it be ok to handle things exactly the same regardless of how many QueryParameters there are?
executeBatch is a "super" method from the PreparedStatement's parent Statement which returns an int[] which indicates the success/failure of the executed queries and executeQuery returns a ResultSet. Therefore, it would be a good idea to have the two be totally different method calls so the developer can handle them differently. I would recommend:
An executeQuery(HelperQuery helperQuery) method which will return the associated ResultSet and will only get the first QueryParameters from the HelperQuery (for convenience) and another method which the developer can specify which QueryParameter set to use (either have them specify a number of the QueryParameter list or just pass in the QueryParameters explicitly (I recommend the second of the two)).
An executeBatch(HelperQuery helperQuery method which will return the int[] and the developer can handle that as they wish.
It's always good to give the user (developer in this case) power to do what they want (but also provide for a simple solution for them to perform common tasks).
i had a situation where i had to use this setDataVector function. I was puzzled to see there was an extra second argument(Vector columnIdentifiers) in the function. I'm just resetting the data. Why do i need to send the column identifiers?? And it doesn't take the old column identifiers by default if i don't pass the second argument. Irritating to add initialize a vector with column identifiers only for this purpose. Any idea why it's been done like that?
From the actual code, it looks to me like the method could have been better named. Something like setDataAndColumns() makes more sense. The internal code looks like this:
this.dataVector = nonNullVector(dataVector);
this.columnIdentifiers = nonNullVector(columnIdentifiers);
Passing in null for columnIdentifiers will simply remove all the columns in the table. I guess your controller class needs to keep a copy of the columnIdentifiers to pass in as required.
The setDataVector(...) method is invoked by all the constructor methods which require you to include both parameters.
I have a form with (at the moment) two fields and submit the following:
capture.id = 213
capture.description = DescriptionText
The target object 'capture' is immutable and I would like to provide a type converter to take both values and call the constructor. What I cannot seem to do is get by TypeConverter to be invoked.
If the input is simply:
capture = foo
Then the type converter is called, but obviously this isn't much use, is there away to make a ognl delegate the rest of the rest of the type conversation to me, perhaps passing in a Map of the parameters?
Any ideas? Is this even possible in struts2
versions: struts 2.0.14 & ognl 2.6.11
EDIT: I've done a bit of reading on this and my next attempt seemed to me to be a good plan. My theory was that using the Map syntax would make Ognl convert the values to a map and then call my converter with that map to convert it to my value.
capture[id] = 213
capture[description] = DescriptionText
Nope that doesn't seem make any difference at all.
The way I did this was to have the following in the JSP:
<s:textfield name="capture" value="capture.id" />
<s:textfield name="capture" value="capture.description" />
In the type converter, the String[] values parameter of the convertFromString method will contain both values needed to construct a new immutable capture. Provided that you are consistent with the text field ordering (or better yet, encapsulate it in a tag file), you can use the indexes of the values array to reliably get the appropriate field of the capture object.
The one weird part about this approach is that the convertToString method doesn't really do anything for you. You can return either id or description (or concatenate them together), but since you are using the values attribute in the JSP, it doesn't matter.
It seems the that the answer is no you can't do that with struts2.
I've posted this question on the struts2 mailing list and it seems that it just isn't possible to have multiple fields be presented to a TypeConverter.
The alternative solution suggested is to have mutable object with setters and then have some form of 'petify' method to prevent any future changes.
For my project I've actually implemented another struts Interceptor to implement my custom parameter binding behaviour.