Boolean Getter and Setter using eclipse - java

I am wondering why eclipse produces the following getter and setter if i used the following boolean:
boolean isLifeTimeMember
The getter should be isIsLifeTimeMember() and not isLifeTimeMember()
I think it affected calling the variable isLifeTimeMember in JSP. because it will look at JSP and map it to isIsLifeTimeMember() getter method.
Error will result because there is no isIsLifeTimeMember() method but the getter generated by eclipse is isLifeTimeMember()
Thank you.

Eclipse name generation rules are that boolean getters should start with is. If the variable name already starts with is, then it thinks that no additional prefix is necessary.
Eclipse has a setting that controls the use of is for generated boolean getters. Open up Preferences and navigate to Java > Code Style. There you can uncheck the option "Use 'is' prefix for getters that return boolean". Eclipse-generated boolean getters will then start with "get", just like all the others.
Java has no problem, by the way, in having a field and a method with the same name.
However, having property names that start with "is" will probably cause problems with jsp. As described in this thread, it's better to avoid property names that read like questions (isLifeTimeMember) and instead just use the property itself as the property name (lifeTimeMember).
Code Example:
boolean lifeTimeMember;
public boolean isLifeTimeMember() {
return lifeTimeMember;
}
public void setLifeTimeMember(boolean lifeTimeMember) {
this.lifeTimeMember = lifeTimeMember;
}
And in JSP if you need to use this variable simply use variable name "lifeTimeMember".

In case of boolean variable, eclipse prepends is to the variable name to form the getter name. I.e. If variable is boolean present; then the gemerated getter would be named isPresent();
Its not advisable to have an is in the variable name.
If the variable name is ispresent , on jsp you will lookup by variable name ispresent which in turn looks up for its getter, its an boolean so it assumes getter would be isispresemt(); which was not there as the getter setter generator in eclipse does not add an is in case that already existe in variable name.
thus an exception could not find the field ispresent is expected to be thrown
having an is in field name , can cause problems, avoid using them

Related

Way of invoking setter methods using reflection based on getters of another Object

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!

JavaBeanBooleanPropertyBuilder for "Beans" without setters

I found this post for connecting a Java Bean as property binding with an existing JavaFX property. The binding should target a boolean property:
class MyClass {
private boolean loaded;
public boolean isLoaded() {
return loaded;
}
// Value changed internally
}
For real beans, meaning beans with setters the following works like a charm. But I've the problem that there's no setter for the loaded property, just because it's set internally and shouldn't be modifyable for external classes.
BooleanProperty loadedProeprty = new JavaBeanBooleanPropertyBuilder()
.bean(bean)
.name("loaded")
.getter("isLoaded")
.build();
Is there any way to create still a property for such "beans" without a setter? For now I just get a NoSuchMethodException for the expected setter MyClass.setLoaded(boolean).
Use ReadOnlyJavaBeanBooleanPropertyBuilder instead.
Normal properties in JavaFX are always read/write and thus require a setter. The read only variant creates a read only property and thus does not require a setter.

Calling private instance variable from other applcation whose getter setter not avaliable

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

Spring expression parsing using evaluation context for boolean field

I am using spring-expression for parsing values in a class(present in jar). After i read this value i set it in the target class [a typical use case of spring-expression]. However, all the field's value from the class in jar can be parsed except, boolean value. In the source class, it is declared like this:
boolean isVerified;
//getter
public isVerified() {
return isVerfied;
}
Spring-expression code to read this value:
Expression sourceExp = parser.parseExpression(<source field string>);
sourceExp.getValue(sourceContext);
and this fails. The message is
Couldn't find property isVerified
My question is it because spring is looking for isIsVerified method rather than isVerified method? If not this what could be the reason for failure?
You don't show your expression but SpEL uses JavaBean semantics when accessing bean properties. It knows nothing about the internals of the referenced bean. When it encounters a property request...
"x.foo"
it tries to find the getter getFoo() (any return type) and if that's not found, it looks for isFoo() if it returns boolean.
I suspect you are trying to use x.isVerified. There is no such getter; you need to use x.verified, or you can invoke the method itself x.isVerified().

IReport and getter between classes

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"

Categories

Resources