2

i have a class like this

public class Student
{

    private String reference;
    private String aPlaceAt;
    //methods generate by Netbeans Eclipse generates the same methods
    public String getaPlaceAt(){return aPlaceAt;}
    public void setaPlaceAt(String aPlaceAt){this.aPlaceAt = aPlaceAt;}    
    public String getReference(){return reference;}
    public void setReference(String reference){this.reference = reference;}       
}

later i need to find the setters and getters by each property by reflection i am using the following code

public Method findSetterFor(final Class clazz,final String propertyName) throws Exception
{
    return new PropertyDescriptor(propertyName,clazz).getWriteMethod();
}

for(final Field field:clazz.testClazz.getDeclaredFields())
System.out.println(field.getName()+" "+clazz.findSetterFor(clazz.testClazz,field.getName()));        

they return the correct setter for reference but for the property aPlaceAt

throws

Exception in thread "main" java.beans.IntrospectionException: Method not found: isAPlaceAt

why isAPlaceAt? not should be

public void setaPlaceAt(String aPlaceAt){this.aPlaceAt = aPlaceAt;}?

or why i am doing wrong? how can accomplish it thanks?

i think the setters and getters are generate correctly according to other post at SO and this link

Link

the methods are generate by Netbeans and Eclipse and intellij

UPDATE here is another hot forum about it this same problem please check it out

Another SO post

Community
  • 1
  • 1
chiperortiz
  • 4,751
  • 9
  • 45
  • 79

1 Answers1

5

Your getter/setter names don't follow the Java bean naming conventions (or maybe the do). For PropertyDescriptor, according to the javadoc

Note that the property name should start with a lower case character, which will be capitalized in the method names.

they should be

public String getAPlaceAt() {
    return aPlaceAt;
}

public void setAPlaceAt(String aPlaceAt) {
    this.aPlaceAt = aPlaceAt;
}

The error message is kind of misleading. The introspector will try to find getAPlaceAt and then isAPlaceAt. It will report the last thing it tried but failed.

You can avoid such mistakes by generating setters/getters through the IDE.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • they are generate by netbeans – chiperortiz Apr 28 '14 at 14:44
  • @chiperortiz Hmm. Netbeans isn't following bean naming conventions then. – Sotirios Delimanolis Apr 28 '14 at 14:44
  • @chiperortiz You need to also change the getter as shown in my answer. – Sotirios Delimanolis Apr 28 '14 at 22:10
  • seems to works now.. but i have 2 questions.. why eclipse and netbeans and intellij creates the setters in that way.. and hibernate using projections searchs the same methods signature only this `PropertyDescription` use this way..... – chiperortiz Apr 29 '14 at 12:03
  • hey @Sotirios Delimanolis please check my edited question thanks a lot. – chiperortiz Apr 29 '14 at 12:27
  • @chiperortiz Yeah, that link has some contradictory information. Within a JSP, the proper name would be `getaPlaceAt()`, but with `PropertyDescriptor`, the javadoc states `Note that the property name should start with a lower case character, which will be capitalized in the method names.` Is the specification wrong or is the implementation wrong? I don't know. – Sotirios Delimanolis Apr 29 '14 at 14:44