sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / options / binding / BeanValueAccessor.java
blob3814c890df3b00911fa6d8c22358b8ac02da19bf
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.options.binding;
18 import java.beans.BeanInfo;
19 import java.beans.IntrospectionException;
20 import java.beans.Introspector;
21 import java.beans.PropertyDescriptor;
22 import java.lang.reflect.Field;
24 /**
25 * @author Dmitry Avdeev
27 public abstract class BeanValueAccessor extends ValueAccessor {
29 public static BeanValueAccessor createAccessor(Object bean, final String propertyName) {
30 Field[] fields = bean.getClass().getFields();
31 for (final Field field : fields) {
32 if (field.getName().equals(propertyName)) {
33 return new BeanValueAccessor(bean, propertyName) {
34 protected Object doGetValue() throws IllegalAccessException {
35 return field.get(myBean);
38 protected void doSetValue(Object value) throws IllegalAccessException {
39 field.set(myBean, value);
42 @Override
43 public Class getType() {
44 return field.getType();
49 try {
50 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
51 for (final PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
52 if (descriptor.getName().equals(propertyName)) {
53 return new BeanValueAccessor(bean, propertyName) {
54 @Override
55 protected Object doGetValue() throws Exception {
56 return descriptor.getReadMethod().invoke(myBean);
59 @Override
60 protected void doSetValue(Object value) throws Exception {
61 descriptor.getWriteMethod().invoke(myBean, value);
64 @Override
65 public Class getType() {
66 return descriptor.getPropertyType();
71 throw new IllegalArgumentException("Property " + propertyName + " not found in " + bean.getClass());
73 catch (IntrospectionException e) {
74 throw new RuntimeException(e);
78 protected final Object myBean;
79 protected final String myPropertyName;
81 public BeanValueAccessor(Object bean, String propertyName) {
82 myBean = bean;
83 myPropertyName = propertyName;
86 public Object getValue() {
87 try {
88 return doGetValue();
90 catch (Exception e) {
91 throw new RuntimeException("Cannot access property " + myPropertyName, e);
95 public void setValue(Object value) {
96 try {
97 doSetValue(value);
99 catch (Exception e) {
100 throw new RuntimeException("Cannot access property " + myPropertyName, e);
104 protected abstract Object doGetValue() throws Exception;
106 protected abstract void doSetValue(Object value) throws Exception;