Java Code Examples for sun.reflect.misc.MethodUtil

The following examples show how to use sun.reflect.misc.MethodUtil. These examples are extracted from open source projects. 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
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 2
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 3
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 4
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 5
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 6
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 7
Source Project: Bytecoder   Source File: BasicComboBoxEditor.java    License: Apache License 2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 8
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 9
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 10
/**
 * Returns the result of method execution.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
@Override
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    Object bean = getContextBean();
    Class<?>[] types = getArgumentTypes(args);
    Method method = (type != null)
            ? MethodFinder.findStaticMethod(type, this.name, types)
            : MethodFinder.findMethod(bean.getClass(), this.name, types);

    if (method.isVarArgs()) {
        args = getArguments(args, method.getParameterTypes());
    }
    Object value = MethodUtil.invoke(method, bean, args);
    return method.getReturnType().equals(void.class)
            ? ValueObjectImpl.VOID
            : ValueObjectImpl.create(value);
}
 
Example 11
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 12
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 13
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 14
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 15
Source Project: jdk8u60   Source File: MethodElementHandler.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the result of method execution.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
@Override
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    Object bean = getContextBean();
    Class<?>[] types = getArgumentTypes(args);
    Method method = (type != null)
            ? MethodFinder.findStaticMethod(type, this.name, types)
            : MethodFinder.findMethod(bean.getClass(), this.name, types);

    if (method.isVarArgs()) {
        args = getArguments(args, method.getParameterTypes());
    }
    Object value = MethodUtil.invoke(method, bean, args);
    return method.getReturnType().equals(void.class)
            ? ValueObjectImpl.VOID
            : ValueObjectImpl.create(value);
}
 
Example 16
Source Project: jdk8u60   Source File: BasicComboBoxEditor.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 17
Source Project: JDKSourceCode1.8   Source File: DefaultMXBeanMappingFactory.java    License: MIT License 6 votes vote down vote up
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 18
Source Project: JDKSourceCode1.8   Source File: DefaultMXBeanMappingFactory.java    License: MIT License 6 votes vote down vote up
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 19
Source Project: JDKSourceCode1.8   Source File: BasicComboBoxEditor.java    License: MIT License 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 20
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 21
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 22
/**
 * Returns the result of method execution.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
@Override
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    Object bean = getContextBean();
    Class<?>[] types = getArgumentTypes(args);
    Method method = (type != null)
            ? MethodFinder.findStaticMethod(type, this.name, types)
            : MethodFinder.findMethod(bean.getClass(), this.name, types);

    if (method.isVarArgs()) {
        args = getArguments(args, method.getParameterTypes());
    }
    Object value = MethodUtil.invoke(method, bean, args);
    return method.getReturnType().equals(void.class)
            ? ValueObjectImpl.VOID
            : ValueObjectImpl.create(value);
}
 
Example 23
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 24
@Override
final Object toNonNullOpenValue(Object value)
        throws OpenDataException {
    CompositeType ct = (CompositeType) getOpenType();
    if (value instanceof CompositeDataView)
        return ((CompositeDataView) value).toCompositeData(ct);
    if (value == null)
        return null;

    Object[] values = new Object[getters.length];
    for (int i = 0; i < getters.length; i++) {
        try {
            Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
            values[i] = getterMappings[i].toOpenValue(got);
        } catch (Exception e) {
            throw openDataException("Error calling getter for " +
                                    itemNames[i] + ": " + e, e);
        }
    }
    return new CompositeDataSupport(ct, itemNames, values);
}
 
Example 25
Object fromCompositeData(CompositeData cd,
                         String[] itemNames,
                         MXBeanMapping[] converters)
        throws InvalidObjectException {
    Object o;
    try {
        final Class<?> targetClass = getTargetClass();
        ReflectUtil.checkPackageAccess(targetClass);
        o = targetClass.newInstance();
        for (int i = 0; i < itemNames.length; i++) {
            if (cd.containsKey(itemNames[i])) {
                Object openItem = cd.get(itemNames[i]);
                Object javaItem =
                    converters[i].fromOpenValue(openItem);
                MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
            }
        }
    } catch (Exception e) {
        throw invalidObjectException(e);
    }
    return o;
}
 
Example 26
/**
 * Returns the result of method execution.
 *
 * @param type  the base class
 * @param args  the array of arguments
 * @return the value of this element
 * @throws Exception if calculation is failed
 */
@Override
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
    Object bean = getContextBean();
    Class<?>[] types = getArgumentTypes(args);
    Method method = (type != null)
            ? MethodFinder.findStaticMethod(type, this.name, types)
            : MethodFinder.findMethod(bean.getClass(), this.name, types);

    if (method.isVarArgs()) {
        args = getArguments(args, method.getParameterTypes());
    }
    Object value = MethodUtil.invoke(method, bean, args);
    return method.getReturnType().equals(void.class)
            ? ValueObjectImpl.VOID
            : ValueObjectImpl.create(value);
}
 
Example 27
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 28
Source Project: jdk1.8-source-analysis   Source File: EventHandler.java    License: Apache License 2.0 5 votes vote down vote up
private Object applyGetters(Object target, String getters) {
    if (getters == null || getters.equals("")) {
        return target;
    }
    int firstDot = getters.indexOf('.');
    if (firstDot == -1) {
        firstDot = getters.length();
    }
    String first = getters.substring(0, firstDot);
    String rest = getters.substring(Math.min(firstDot + 1, getters.length()));

    try {
        Method getter = null;
        if (target != null) {
            getter = Statement.getMethod(target.getClass(),
                                  "get" + NameGenerator.capitalize(first),
                                  new Class<?>[]{});
            if (getter == null) {
                getter = Statement.getMethod(target.getClass(),
                               "is" + NameGenerator.capitalize(first),
                               new Class<?>[]{});
            }
            if (getter == null) {
                getter = Statement.getMethod(target.getClass(), first, new Class<?>[]{});
            }
        }
        if (getter == null) {
            throw new RuntimeException("No method called: " + first +
                                       " defined on " + target);
        }
        Object newTarget = MethodUtil.invoke(getter, target, new Object[]{});
        return applyGetters(newTarget, rest);
    }
    catch (Exception e) {
        throw new RuntimeException("Failed to call method: " + first +
                                   " on " + target, e);
    }
}
 
Example 29
final Object fromCompositeData(CompositeData cd,
                               String[] itemNames,
                               MXBeanMapping[] converters)
        throws InvalidObjectException {
    try {
        return MethodUtil.invoke(fromMethod, null, new Object[] {cd});
    } catch (Exception e) {
        final String msg = "Failed to invoke from(CompositeData)";
        throw invalidObjectException(msg, e);
    }
}
 
Example 30
Source Project: Bytecoder   Source File: TransferHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns an object which represents the data to be transferred.  The class
 * of the object returned is defined by the representation class of the flavor.
 *
 * @param flavor the requested flavor for the data
 * @see DataFlavor#getRepresentationClass
 * @exception IOException                if the data is no longer available
 *              in the requested flavor.
 * @exception UnsupportedFlavorException if the requested data flavor is
 *              not supported.
 */
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (! isDataFlavorSupported(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }
    Method reader = property.getReadMethod();
    Object value = null;
    try {
        value = MethodUtil.invoke(reader, component, (Object[])null);
    } catch (Exception ex) {
        throw new IOException("Property read failed: " + property.getName());
    }
    return value;
}