下面列出了java.beans.Beans#getInstanceOf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setValue(T val)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (setter == null) {
throw new IllegalAccessException();
}
Object valideInstance = Beans.getInstanceOf(instance, setter.getDeclaringClass());
try {
setter.invoke(valideInstance, val);
} catch (IllegalAccessException ex) {
try {
setter.setAccessible(true);
setter.invoke(valideInstance, val);
} finally {
setter.setAccessible(false);
}
}
}
public void setValue(T val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!canWrite()) {
throw new IllegalAccessException();
}
Object validInstance = Beans.getInstanceOf(instance, setter.getDeclaringClass());
Object value = val;
if (
(val != null) && (setter.getParameterTypes()[0].getComponentType().isPrimitive()) &&
(!val.getClass().getComponentType().isPrimitive())
) {
value = Utilities.toPrimitiveArray((Object[]) val);
}
setter.invoke(validInstance, value);
}
public T getValue() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (getter == null) {
throw new IllegalAccessException();
}
Object valideInstance = Beans.getInstanceOf(instance, getter.getDeclaringClass());
try {
try {
return cast(getValueType(), getter.invoke(valideInstance));
} catch (IllegalAccessException ex) {
try {
getter.setAccessible(true);
return cast(getValueType(), getter.invoke(valideInstance));
} finally {
getter.setAccessible(false);
}
}
} catch (IllegalArgumentException iae) {
//Provide a better message for debugging
StringBuffer sb = new StringBuffer("Attempted to invoke method ");
sb.append(getter.getName());
sb.append(" from class ");
sb.append(getter.getDeclaringClass().getName());
sb.append(" on an instance of ");
sb.append(valideInstance.getClass().getName());
sb.append(" Problem:");
sb.append(iae.getMessage());
throw (IllegalArgumentException) new IllegalArgumentException(sb.toString()).initCause(iae);
}
}
/** Detaches all listeners from the bean and destroys it.
* @throws IOException if there was a problem
*/
@Override
public void destroy() throws IOException {
if (removePCLMethod != null) {
try {
Object o = Beans.getInstanceOf(bean, removePCLMethod.getDeclaringClass());
removePCLMethod.invoke(o, new Object[] { propertyChangeListener });
} catch (Exception e) {
NodeOp.exception(e);
}
}
super.destroy();
}
public T getValue() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!canRead()) {
throw new IllegalAccessException();
}
Object validInstance = Beans.getInstanceOf(instance, getter.getDeclaringClass());
return PropertySupport.cast(getValueType(), getter.invoke(validInstance));
}
public E getIndexedValue(int index)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!canIndexedRead()) {
throw new IllegalAccessException();
}
Object validInstance = Beans.getInstanceOf(instance, indexedGetter.getDeclaringClass());
return PropertySupport.cast(getElementType(), indexedGetter.invoke(validInstance, index));
}
public void setIndexedValue(int index, E val)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!canIndexedWrite()) {
throw new IllegalAccessException();
}
Object validInstance = Beans.getInstanceOf(instance, indexedSetter.getDeclaringClass());
indexedSetter.invoke(validInstance, new Object[] { new Integer(index), val });
}
/** Performs initialization of the node
*/
private void initialization(boolean hasLookup) throws IntrospectionException {
setIconBaseWithExtension(ICON_BASE);
setSynchronizeName(true);
// Find the first public superclass of the actual class.
// Should not introspect on a private class, because then the method objects
// used for the property descriptors will not be callable without an
// IllegalAccessException, even if overriding a public method from a public superclass.
Class<?> clazz = bean.getClass();
while (!Modifier.isPublic(clazz.getModifiers()) && !hasExplicitBeanInfo(clazz)) {
clazz = clazz.getSuperclass();
if (clazz == null) {
clazz = Object.class; // in case it was an interface
}
}
beanInfo = Utilities.getBeanInfo(clazz);
// resolving the name of this bean
registerName();
setNameSilently(getNameForBean());
BeanDescriptor descriptor = beanInfo.getBeanDescriptor();
String sd = descriptor.getShortDescription();
if (!Utilities.compareObjects(sd, descriptor.getDisplayName())) {
setShortDescription(sd);
}
// add propertyChangeListener
EventSetDescriptor[] eventSetDescriptors = beanInfo.getEventSetDescriptors();
int i;
int k = eventSetDescriptors.length;
Method method = null;
for (i = 0; i < k; i++) {
method = eventSetDescriptors[i].getAddListenerMethod();
if (
(method != null) && method.getName().equals("addPropertyChangeListener") && // NOI18N
Modifier.isPublic(method.getModifiers())
) {
break;
}
}
if (i != k) {
try {
Object o = Beans.getInstanceOf(bean, method.getDeclaringClass());
propertyChangeListener = new PropL();
method.invoke(o, new Object[] { WeakListeners.propertyChange(propertyChangeListener, o) });
removePCLMethod = eventSetDescriptors[i].getRemoveListenerMethod();
} catch (Exception e) {
// Warning, not info: likely to call e.g. getters or other things used
// during startup of the bean, so it is not good to swallow errors here
// (e.g. SharedClassObject.initialize throws RuntimeException -> it is
// caught here and probably someone wants to know).
Exceptions.attachMessage(e,
"Trying to invoke " + method +
" where introspected class is " +
clazz.getName()); // NOI18N
NodeOp.warning(e);
}
}
createProperties(bean, beanInfo);
for (Enumeration e = beanInfo.getBeanDescriptor().attributeNames(); e.hasMoreElements();) {
String aname = (String) e.nextElement();
setValue(aname, beanInfo.getBeanDescriptor().getValue(aname));
}
if (!hasLookup) {
Node.Cookie instanceCookie = TMUtil.createInstanceCookie(bean);
if (instanceCookie != null) {
getCookieSet().add(instanceCookie);
}
}
}