I'm using IReport (JasperStudio plugin for Eclipse) and I'm trying to create a report with a JavaBean as source.
Suppose I have these two classes:
public class MyClass {
private String myClassAttribute;
// getter and setter for myClassAttribute
}
public class AnotherMyClass {
private String anotherMyClassAttribute;
private MyClass myClass;
// getter and setter for anotherMyClassAttribute
// getter and setter for myClass
}
If I choose AnotherMyClass as JavaBeanSource I can set only fields from that class (anotherMyClassAttribute), I didn't find a way to set a text to getMyClass().getmyClassAttribute().
Do JavaBeans stop at level one or is there a way to use attribute from other classes between references?
Thanks.
In report define field $F{myClass} with type MyClass
In text field use expression $F{myClass}.getMyClassAttribute()
No, it doesn't stop at level one, you may go as deep as you want. You may use the attribute like myClass.myClassAttribute. And for setting a value to it, myClass.myClassAttribute = "some value"
Related
I have 2 objects which are related
One of them is of class Attribute, the other one is something like controller of that called AddAttributeActionHandler
The Attribute object has a lot of fields in it which I need to set in AddAttributeActionHandler Both classes have getters and setters some of them are equal.
Basically what I what is to copy every available getter from the class Attribute and invoke according setter method on the class AddAttributeActionHandler
In other words I can execute the following (you can call it "manual" setting)
Attribute attributeObj = getAttributeObj();
String codeFromAttrObj = attributeObj.getCode();
String titleFromAttrObj = attributeObj.getTitle();
AddAttributeAction action = AddAttributeAction.create();
action.setCode(codeFromAttrObj);
action.setTitle(titleFromAttrObj);
The problem is that there are just like 50+ fields. I'd like it to be fully automatic.
So, I've found the following code for getting every available public getter for the object Attribute
for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(attributeObj.getClass()).getPropertyDescriptors())
{
if(propertyDescriptor.getReadMethod().toString() != "null")
{
String getterMethodName = propertyDescriptor.getReadMethod().toString();
String getterMethodValue = propertyDescriptor.getReadMethod().invoke(attributeObj).toString());
}
}
The code above gets every getter available in the object and prints its value.
Now I need to find out whether there's a corresponding setter method in the object of the class AddAttributeActionHandler and set the property which I got from the getter in the Attribute object.
Is it possible?
If so, please provide any clues.
Thanks in advance!
I am reading string xml data from other application in my project using JAXB.
They have provided their appdata.jar. (I dont have control on their classes).
They have MessgeBody. In MessageBody they have defined one private instance variable without getter and setter. And because of this I am not able to obtain instance of perticular class.Remaining fields with getter n setter are accessible.
I tried Java reflection , it is not working.
Here is my code -
Class Test{
public void parseXMLStr(){
String xmlStr="<xml><first></first><second></second><third></third></xml>"
//I am able to get values of first and second as getter and setter defined.
First f =getMessageBody().getFirst();
f.dosomething().. //working
Second s =getMessageBody().getSecond();
s.dosomething()...//working
}
appdata.jar(other application) contains MessageBody as,
class MessageBody{
private First first;//getter setter provided
private Second second;//getter setter provided
private Third third;//getter setter NOT Provided
}
How I can obtain Third class instance and access variables inside it.
I tried Java reflection as below but I am getting null pointer exception(although data is there )
Field f = obj.getClass().getDeclaredField("third");
f.setAccessible(true);
Third t = (Third) f.get(obj);
t.getName();//here getting nullpointer exception
I have a domain model with immutable classes, where most of the time the arguments are required to be non-null:
public class TestClass {
private final String field;
public TestClass(String field) {
this.field = Objects.requireNonNull(field);
}
}
I generate these constructor through right click -> Generate... -> Constructor:
The generated constructor, of course, haven't got the requireNonNull() method call, which forces me to add this for every field that needs it.
Is there any way to configure this code generation to include requireNonNull by default, if so, how?
A google search doesn't lead to much, but the official documentation, which doesn't mention anything about this.
It's not possible to customize the constructor template, vote for the related request.
This might sound stupid to you,
but why do I need to define an empty constructor in my #Entitys?
Every tutorial I saw said : every entity needs an empty constructor.
But Java always give you a default invisible empty constructor (if you don't redefine one).
Let me clarify..
What I understood by "need" was write.
Meaning: always write an empty constructor in your entity.
example:
#Entity
public class MyEntity implements Serializable {
#Id
private String str;
public MyEntity(){}
//here getter and setter
}
But Java always gives you this empty constructor when you don't redefine it (write an other one with parameters).
In this case writing this empty constructor seems useless.
An empty constructor is needed to create a new instance via reflection by your persistence framework. If you don't provide any additional constructors with arguments for the class, you don't need to provide an empty constructor because you get one per default.
You can also use the #PersistenceConstructor annotation which looks like following
#PersistenceConstructor
public Movie(Long id) {
this.id = id;
}
to initialise your entity if Spring Data is present in your project. Thus you can avoid the empty constructor as well.
But java always give you a default invisible empty constructor (if you
don't redefine one).
This statement is true only when you don't provide any constructor in your class. If an argument constructor is provided in your class, then JVM will not add the no-argument constructor.
Explicitly defining a default constructor is not necessary unless you provide another constructor for the entity. If you provide another constructor, aside from one with the default constructor's signature, the default constructor will not be created.
Since JPA implementations rely upon the existence of a default constructor it is then necessary to include the default constructor that will be omitted.
As you specified the "JPA" tag, I assume your question applies to JPA only and not empty constructors in general.
Persitence frameworks often use reflection and more specifically Class<T>.newInstance() to instantiate your objects, then call getters/setters by introspection to set the fields.
That is why you need an empty constructor and getters/setters.
See this StackOverflow question about empty constructors in Hibernate.
Actually you don't need to write it. You have it by default. Sometimes you can create private constructor to prevent users to use default
public class MyClass{
private MyClass(){}
}
For singelton patterns, for example you can block using default constructor.
Sometimes, when you use Gson plugin to convert String Json data to Object, it demands to write default constructor, otherwise it doesn't work
All the answers are fine.
But let's talk with code. Following snippets of code will give you more clarity.
PersonWithImplicitConstructor.java
public class PersonWithImplicitConstructor {
private int id;
private String name;
}
First we have to compile the .java file
javac PersonWithImplicitConstructor.java
Then class file will be generated.
Running the javap on top this class file will give you the following information.
javap PersonWithImplicitConstructor.class
Compiled from "PersonWithImplicitConstructor.java"
public class PersonWithImplicitConstructor {
public PersonWithImplicitConstructor();
}
NOTE: If you want more information, you can use -p flag on javap.
The next java file will have parameterised constructor only.
PersonWithExplicitConstructor.java
public class PersonWithExplicitConstructor {
private int id;
private String name;
public PersonWithExplicitConstructor(int id, String name) {
this.id = id;
this.name = name;
}
}
javac PersonWithExplicitConstructor.java
javap PersonWithExplicitConstructor.class
Compiled from "PersonWithExplicitConstructor.java"
public class PersonWithExplicitConstructor {
public PersonWithExplicitConstructor(int, java.lang.String);
}
PersonWithBothConstructors.java
public class PersonWithBothConstructors {
private int id;
private String name;
public PersonWithBothConstructors() {
}
public PersonWithBothConstructors(int id, String name) {
this.id = id;
this.name = name;
}
}
javac PersonWithBothConstructors.java
javap PersonWithBothConstructors.class
Compiled from "PersonWithBothConstructors.java"
public class PersonWithBothConstructors {
public PersonWithBothConstructors();
public PersonWithBothConstructors(int, java.lang.String);
}
Java not always give you a default invisible empty constructor if your class got argument constructor, you have to define the empty constructor by your own.
From the JPA tag, I suppose that you are working with Java beans. Every bean needs to have the following properties:
Getters and setters for all its main instance variables.
An empty constructor.
All its instance variables should preferably be private.
Thus the statement : "every entity needs an empty constructor".
What are the correct rules to write a JavaBean class?
I'm confused because some books use MUST while other user SHOULD or COULD to describe
the writing rule:
i.e.
a bean class MUST implements Serializable or SHOULD?
the instance variables MUST be private or SHOULD BE?
A JavaBean is defined by its properties (i.e. its getter and setter methods), not it's fields. Although the terms are used interchangably, that is actually not correct. The Introspector mechanism ignores fields completely.
Example
Take this (awfully designed) Javabean:
public class TestBean {
private int baz;
private char[] phleem;
public String getFoo() {
return new String(phleem);
}
public void setFoo(final String foo) {
this.phleem = foo.toCharArray();
}
public long getBar() {
return baz;
}
public void setBar(final long bar) {
this.baz = (int) bar;
}
}
You'd think the properties are:
"baz" (int)
"phleem" (char[])
but now let's inspect it with the Javabeans introspector:
for (PropertyDescriptor descriptor : Introspector
.getBeanInfo(TestBean.class, Object.class)
.getPropertyDescriptors()) {
System.out.println("Name: " + descriptor.getName() +
", type: " + descriptor.getPropertyType());
}
Here's the output:
Name: bar, type: long
Name: foo, type: class java.lang.String
Conclusion:
Getters and setters are what define a Javabeans property. It's a convention that they are backed by fields of the same name and type, but the fields are not actually part of the Javabean properties (although many documentations will suggest otherwise).
On re-reading my answer: it is meant as an addendum to the other answers. If you want a short and simple answer, go with skaffman's.
It is a public class.
It has a public parameterless constructor (though it may have other constructors
as well)
It implements Serializable interface (i.e. it can be made persistent, so its state can
be saved)
It has properties with “getter” and “setter” methods named by following
JavaBeans naming patterns
It has events which follow the standard Java event model with the registration
methods named by following the JavaBeans naming patterns
It may have other methods which do not follow the naming patterns. These
methods are not exposed by a builder tool.
Adding to the previous poster - skaffman. It is always a good practice to override, toString(), hashCode(), equals() and finally write a overloaded constructor that has all the fields (that this class has) as input.
Be sure not to use other references (like List, HashMaps etc) in the toString() and hashCode()'s implementation.
On a side note, eclipse has built-in functionality to generate them for you..
A Java Bean is a Java class that should follow the following conventions:
It should have a no-arg constructor.
It should be Serializable.
It should provide methods to set and get the values of the properties, known as getter and setter methods.
All the above and it should not cross the boundaries of Java API . It means it should not extend or implement any classes or interface,but one relaxation is there it can implement only one serializable interfce why because it is a marker interface