Java Code Examples for java.beans.Beans#getInstanceOf()

The following examples show how to use java.beans.Beans#getInstanceOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: PropertySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 3
Source File: IndexedPropertySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: BeanNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 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();
}
 
Example 5
Source File: IndexedPropertySupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 6
Source File: IndexedPropertySupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 7
Source File: IndexedPropertySupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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 });
}
 
Example 8
Source File: BeanNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** 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);
        }
    }
}