[英]why default method not recognized as property(getter/setter)?
界面:
public interface SomeInt{
Integer getX();
void setX(Integer value);
default Integer getY(){
return getX();
}
default void setY(Integer value){
setX(value);
}
}
一個類實現它:
public class A implements SomeInt{
private Integer x;
public Integer getX(){
return x;
}
public void setX(Integer value){
x = value;
}
}
初始化后,我可以調用方法 get Y & set Y ,並獲得正確的回報。 但是我不能在 JSP( EL ) 中使用它,比如${instance_of_class_a.y}
。 並且屬性 Y 未在 IDEA 的變量列表(調試模式)中列出。
如果我確實在 A 類中明確添加了 getY 和 setY,則一切正常。 為什么? 我認為默認方法就像一個編譯器糖。
抱歉我的英語不好和代碼中的錯誤,我已經更正了。
問題寫在這里有點不好,所以可能出了點問題。 特別是為錯別字添加@Override
。
interface SomeInt {
int getX();
void setX(int x);
default int getY() {
return getX();
}
default void setY(int value) {
setX(value);
}
}
static class A implements SomeInt {
private int x;
@Override
public int getX() {
return x;
}
@Override
public void setX(int value) {
x = value;
}
}
System.out.println("Methods:");
for (Method m : A.class.getMethods()) {
System.out.printf("+ %s%n", m.getName());
}
for (Method m : A.class.getDeclaredMethods()) {
System.out.printf("- %s%n", m.getName());
}
一般來說,getter/setter 使用Class.getMethods
。
Methods:
+ setX
+ getX
...
+ setY
+ getY
- setX
- getX
我想我已經有了答案。
BeanELResover 使用 java.beans.Introspector 來 getBeanInfo(Properties)
public static BeanInfo getBeanInfo(Class<?> beanClass)
throws IntrospectionException
{
if (!ReflectUtil.isPackageAccessible(beanClass)) {
return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
}
ThreadGroupContext context = ThreadGroupContext.getContext();
BeanInfo beanInfo;
synchronized (declaredMethodCache) {
beanInfo = context.getBeanInfo(beanClass);
}
if (beanInfo == null) {
beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
synchronized (declaredMethodCache) {
context.putBeanInfo(beanClass, beanInfo);
}
}
return beanInfo;
}
Introspector 的構造函數調用方法“findExplicitBeanInfo”來獲取當前類的 getDeclaredMethods。 然后用它的超類來做,直到 Object 或 stopClass。 此處不會加載接口的方法。
private Introspector(Class<?> beanClass, Class<?> stopClass, int flags)
throws IntrospectionException {
this.beanClass = beanClass;
// Check stopClass is a superClass of startClass.
if (stopClass != null) {
boolean isSuper = false;
for (Class<?> c = beanClass.getSuperclass(); c != null; c = c.getSuperclass()) {
if (c == stopClass) {
isSuper = true;
}
}
if (!isSuper) {
throw new IntrospectionException(stopClass.getName() + " not superclass of " +
beanClass.getName());
}
}
if (flags == USE_ALL_BEANINFO) {
explicitBeanInfo = findExplicitBeanInfo(beanClass);
}
Class<?> superClass = beanClass.getSuperclass();
if (superClass != stopClass) {
int newFlags = flags;
if (newFlags == IGNORE_IMMEDIATE_BEANINFO) {
newFlags = USE_ALL_BEANINFO;
}
superBeanInfo = getBeanInfo(superClass, stopClass, newFlags);
}
if (explicitBeanInfo != null) {
additionalBeanInfo = explicitBeanInfo.getAdditionalBeanInfo();
}
if (additionalBeanInfo == null) {
additionalBeanInfo = new BeanInfo[0];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.