I've generated classes from my XML .xsd and am trying to set a field EndpointID within in the class MeterSessionInputRF. The problem I'm having is that the setEndpointID method only accepts JAXBElement<Byte> as it's parameter.
I'm currently query a database to get the input for the setEndpointID method. This input can be a string, char, whatever I want it to be.
How do I create a JAXBElement<Byte>? I've tried using the ObjectFactory class but when I try and use it, I don't have the option of creating such an object.
Here's the code I already have to give some perspective.
if(moduleResults.next()){
MeterSessionInputRF msiRF = new MeterSessionInputRF();
msiRF.setRFFrequency(moduleResults.getFloat("id_amr_module"));
JAXBElement<Byte> endpointType;
byte epT = moduleResults.getByte("cd_module_typ");
endpointType.setValue(epT);
msiRF.setEndpointType(endpointType);
}
I keep getting the error that endpointType may have not been initialized. Is there a correct way to create the JAXBElement<Byte>?
The ObjectFactory class generated by XJC should have a method to do that for you. I know you said it wasn't there, but check again, there should be some method that returns an object of that type.
Related
I implemented an object builder which takes a class name and tries to create a new instance object. The builder tries to inject services if any required. The class name is passed as a string in the current implementation. Here is an example
$className = '\Example\Application\ServiceClass';
$service = CiContext::newInstance($className);
However, this is not compatible with my IDE (eclipse) and the refactoring process does not find the class name (in the string form).
Is there any way to get the class name like java does?
Class classInstance = ServerClass.class;
In this case, the refactoring process finds the class reference and changes the class name.
Well, PHP7 class constant is supported on a class name. For example, the following code is correct in PHP 7.4:
$service = CiContext::newInstance(\Example\Application\ServiceClass::class);
This will solve my problem and the IDE find the class usage.
On the other hand, the class literal is going to support for objects too. For more information see class literal on object
I am working on jira-plugin to create issues in JIRA. I am using the method - create(user,createValidationResult) to create issue.
I need to give values of mandatory fields while creating.
I want to give the default values of fields while creating. (The default values are the ones which are configured during the creation of these in JIRA)
I have found the below methods
method populateDefaults
method getDefaultValue
But both the methods require Issue- parameter which is not yet created as I need to create the issue after setting default values
Please let me know how to set the values for these fields. These fields are added using method addCustomFieldValue in the class IssueInputParameters
I found the solution myself : (This works perfectly)
Use the below method :
IssueInputParameters issueInputParameters =
issueService.newIssueInputParameters();
issueInputParameters.setApplyDefaultValuesWhenParameterNotProvided(true);
IssueService.CreateValidationResult createValidationResult =
issueService.validateCreate(user, issueInputParameters);
issueService.create(user,createValidationResult);
Note : In the above code 'issueService' is an object of IssueService.
I am using Play with Excel module 1.2.3. In a controller, I get a list of Students by calling a method defined in the model - Student:
List<Student> students= Student.findStudents();
findStudents() is defined as:
public static List<Student> findStudents() {
List<Student> list = Student.find("colA != colB").fetch();
return list;
}
then I render the excel file by:
renderExcel("student_report");
Inside the excel template, I have used JXLS. For example:
<jx:forEach items="${students}" var="stu">
${stu.address.name} ${stu.name}
</jx:forEach>
Now, the weird thing happens. stu.name always get displayed fine. However, stu.address.name only get displayed when I have done something like System.out.println(student.address.name) in the code. Otherwise, the cell in the Excel report is blank.
Can anyone explain this?
N.B. Student lazily ref to address
Jxls uses Apache Jexl to process property expressions like stu.address.name. Jexl uses reflection to calculate the object property values.
But reflection and lazy loading do not go along because you work not with a real object but with a proxy object.
When you do System.out.println(student.address.name) the real object is instantiated and the reflection works fine.
Possible solution to the issue is described in this answer Converting Hibernate proxy to real object. Or you just should do eager fetching whenever you need to pass the object to Jxls.
I need to create a method that takes in argument any attribute of any class. But i dont want it to be of type String, to avoid refactoring problems while renaming an attribute and to get the errors in Markers Tab of eclipse, and not while running my application.
Having a class Person :
public class Person {
private String name;
// other attributes...
// getters and setters...
}
Now the needed method :
void getAnAttributeOfAClass( <which_type_or_class_here?> attr_as_arg){
// Now I need to get the name of attribute that would be of class Strin...
}
Is there a function or a method, by which we can specify an attribute?
For example :
Person.class.name
Would it be of class Property ?
EDIT
More exactly (#Smallhacker answer helped me), I need to verify at compile time if the argument is really an attribute of the specified class.
Person.class.name // no compile time error
Person.class.nameXXX // compile time error
The closest to what you want is Reflection API's Field or JavaBeans Introspector API's PropertyDescriptor.
But usually things like that are not needed in Java projects because there are libraries which handle these concerns.
You could pass a Class object along with a String name, then let your method use Introspector internally to read that property.
Not sure I understand you well, but there is a class java.lang.reflect.Field, that has a method getName() that would give your the name of the field.
In your example, to get field name, you would do: Person.class.getDeclaredField("name").
EDIT: to get the value of a field in an object, you would do: field.get(obj);
OK, let's say You have the following variables:
Person person = ...; // initialized with some Person
Field nameField = Person.class.getDeclaredField("name");
Now to get the name of person, you would do:
String personName = (String)nameField.get(person);
Actually, this would throw an exception because name is a private field. You can however bypass the protection by doing:
nameField.setAccessible(true);
Unfortunately, Java lacks an ability to reference member variables in a way that can be analyzed at compile time.
There may be some kind of library to simplify this somewhat, but it wouldn't provide a full solution due to limitations in the language itself.
Maybe java generics can help you with this.
You can do something like:
class YourClass<E> {
void getAnAttributeOfAClass(E attr_as_arg){
// some code
}
}
someVariable = new YourClass<Person>();
someVariable.getAnAtributeOfAClass(someObject); //this will not compile if someObject is not an instance of Person
But I still don't know what you want to do exactly inside the method.
I have created test class that created DOM object ,currently I created some attributes
Hard coded ,for example I have create element name structure and for the structure we have attributes,I have created different class that handle the attribute with constructor .
These is the code from the main method
Properties properties = new Properties(document);
Element Property = properties.getProperty();
Properties.setProperty(Property, "product_id","10", "Pro ID");
Type.appendChild(Property);
Properties properties1 = new Properties(document);
Element Property1 = properties1.getProperty();
Properties.setProperty(Property1, "curr","5", "Curr Code");
Type.appendChild(Property1);
The code in the constructor is
public Properties(Document document) {
Property = document.createElement(PROPERTY);
}
As you can see for create new property I have created element property and property1 etc hard coded,which is problematic since what will happen
If I will have table that with list of properties with there data ,how should I handle it?
I am not sure if the constructor is the right solution and my question is how to do that better ?
Thanks!!!
It depends on what you're trying to do.
If you want to create a DOM so that you can test your classes that build DOM's, then simply hard-coding calls will work just fine. You just have to make sure you use enough combinations of calls to thoroughly test you API.
If, on the other hand, you need to create a DOM so that you can then proceed to test your API's that require some sort of DOM input, you might want to consider simply creating those DOM's in the form of XML documents and then using the org.w3c.dom API's to create the DOM from the XML.