Example usage for java.lang.reflect Method getDefaultValue

List of usage examples for java.lang.reflect Method getDefaultValue

Introduction

In this page you can find the example usage for java.lang.reflect Method getDefaultValue.

Prototype

public Object getDefaultValue() 

Source Link

Document

Returns the default value for the annotation member represented by this Method instance.

Usage

From source file:org.neo4j.ogm.metadata.AnnotationInfo.java

public AnnotationInfo(Annotation annotation) {

    this.annotationName = annotation.annotationType().getName();
    this.annotation = annotation;
    this.elements = new HashMap<>();

    final Method[] declaredElements = annotation.annotationType().getDeclaredMethods();
    for (Method element : declaredElements) {
        Object value;/*from  w  ww  .j a v  a2s.c  o m*/
        value = getAttributeValue(annotation, element);
        elements.put(element.getName(),
                value != null ? convert(element, value) : element.getDefaultValue().toString());
    }

    for (Method element : declaredElements) {
        ValueFor valueFor = element.getAnnotation(ValueFor.class);
        if (valueFor != null) {
            Object value = getAttributeValue(annotation, element);

            if (value != null && (!(value instanceof String) || StringUtils.isNotBlank((String) value))) {
                elements.put(valueFor.value(), convert(element, value));
            }
        }
    }
}

From source file:org.jboss.seam.spring.utils.AnnotationInvocationHandler.java

public AnnotationInvocationHandler withAttributes(Map<String, Object> registeredValues) {
    this.registeredValues = registeredValues;
    for (Method method : annotationType.getDeclaredMethods()) {
        final String currentAttribute = method.getName();
        Object registeredValue = registeredValues.get(currentAttribute);
        if (registeredValue == null && method.getDefaultValue() == null) {
            throw new IllegalArgumentException("Required attribute '" + currentAttribute + "' not set for "
                    + annotationType.getName() + " instance, and no default is specified");
        }/*from w w  w .  jav a2  s .  co m*/
    }
    return this;
}

From source file:org.springframework.core.type.classreading.AbstractRecursiveAnnotationVisitor.java

private void registerDefaultValues(Class<?> annotationClass) {
    // Check declared default values of attributes in the annotation type.
    Method[] annotationAttributes = annotationClass.getMethods();
    for (Method annotationAttribute : annotationAttributes) {
        String attributeName = annotationAttribute.getName();
        Object defaultValue = annotationAttribute.getDefaultValue();
        if (defaultValue != null && !this.attributes.containsKey(attributeName)) {
            if (defaultValue instanceof Annotation) {
                defaultValue = AnnotationAttributes.fromMap(
                        AnnotationUtils.getAnnotationAttributes((Annotation) defaultValue, false, true));
            } else if (defaultValue instanceof Annotation[]) {
                Annotation[] realAnnotations = (Annotation[]) defaultValue;
                AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length];
                for (int i = 0; i < realAnnotations.length; i++) {
                    mappedAnnotations[i] = AnnotationAttributes
                            .fromMap(AnnotationUtils.getAnnotationAttributes(realAnnotations[i], false, true));
                }/* www. j a  va 2  s  .  c  om*/
                defaultValue = mappedAnnotations;
            }
            this.attributes.put(attributeName, defaultValue);
        }
    }
}

From source file:org.unitils.core.engine.AnnotationDefaultInvocationHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (annotations == null || annotations.isEmpty()) {
        return null;
    }//from  w w  w .j ava2  s.  c  o m
    AnnotationDefault annotationDefaultAnnotation = method.getAnnotation(AnnotationDefault.class);
    if (annotationDefaultAnnotation == null) {
        return method.invoke(annotations.get(0), args);
    }

    Object defaultValue = method.getDefaultValue();
    for (A annotation : annotations) {
        Object result = method.invoke(annotation, args);
        if (result != defaultValue && (defaultValue == null || !defaultValue.equals(result))) {
            return result;
        }
    }

    if (configuration == null) {
        return defaultValue;
    }
    String propertyName = annotationDefaultAnnotation.value();
    if (isBlank(propertyName)) {
        return defaultValue;
    }
    return configuration.getValueOfType(method.getReturnType(), propertyName);
}

From source file:ca.oson.json.util.ObjectUtil.java

@SuppressWarnings("unchecked")
public static <E, R> R getMethodValue(E obj, Method method, Object... args) {
    R value = null;/*from  w w  w  . j a  v a  2  s. com*/

    try {
        method.setAccessible(true);

        value = (R) method.invoke(obj, args);
    } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        // e.printStackTrace();
        try {
            if (obj != null) {
                Expression expr = new Expression(obj, method.getName(), args);
                expr.execute();
                value = (R) expr.getValue();
            }

            if (value == null) {
                value = (R) method.getDefaultValue();
            }

        } catch (Exception e1) {
            // e1.printStackTrace();
        }
    }

    return value;
}

From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Hash code./*  w  w  w . j  a v a2 s .  c  o  m*/
 * 
 * @param proxy
 *            the proxy
 * @return the int
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 */
protected int hashCode(Object proxy)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    Method[] methods = targetClass.getDeclaredMethods();
    int hashCode = 0;
    for (Method method : methods) {
        Class<?> type = method.getReturnType();
        Object value = (Object) this.attributesMapping.get(method);
        if (value == null)
            value = method.getDefaultValue();
        // if type is primitive type
        if (type.isPrimitive()) {
            // //////////////////////////////////
            // //////////////////////////////////
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        } else if (type.isArray()) {
            Object array = method.invoke(proxy);
            Class<?> comtype = type.getComponentType();
            if (comtype == byte.class || comtype == Byte.class) {
                byte[] valueArr = new byte[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getByte(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == char.class || comtype == Character.class) {
                char[] valueArr = new char[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getChar(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == double.class || comtype == Double.class) {
                double[] valueArr = new double[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getDouble(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == float.class || comtype == Float.class) {
                float[] valueArr = new float[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getFloat(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == int.class || comtype == Integer.class) {
                int[] valueArr = new int[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getInt(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == long.class || comtype == Long.class) {
                long[] valueArr = new long[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getLong(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == short.class || comtype == Short.class) {
                short[] valueArr = new short[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getShort(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == boolean.class || comtype == Boolean.class) {
                boolean[] valueArr = new boolean[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getBoolean(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else {
                Object[] valueArr = new Object[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.get(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            }
        } else {
            // Object value = method.invoke(proxy);
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        }
    }
    return hashCode;
}

From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Equals./*ww  w.j  av  a2s .  c om*/
 * 
 * @param o
 *            the o
 * @param proxy
 *            the proxy
 * @return true, if successful
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 */
protected boolean equals(Object o, Object proxy)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    if (!ReflectUtils.isCast(o, this.targetClass))
        return false;
    Method[] lst = this.targetClass.getDeclaredMethods();
    for (Method method : lst) {
        Object oResult;
        Object thisResult;
        method.setAccessible(true);
        oResult = method.invoke(o);
        thisResult = (Object) this.attributesMapping.get(method);
        if (thisResult == null)
            thisResult = method.getDefaultValue();
        Class<?> returnType = method.getReturnType();
        if (returnType.isArray()) {
            Object[] oResultArray = new Object[Array.getLength(oResult)];
            for (int i = 0; i < oResultArray.length; i++) {
                oResultArray[i] = Array.get(oResult, i);
            }
            Object[] thisResultArray = new Object[Array.getLength(thisResult)];
            for (int i = 0; i < thisResultArray.length; i++) {
                thisResultArray[i] = Array.get(thisResult, i);
            }
            if (oResultArray.length != thisResultArray.length)
                return false;
            if (!Arrays.equals(oResultArray, thisResultArray))
                return false;
        } else if (!oResult.equals(thisResult)) {
            return false;
        }
    }
    return true;
}

From source file:com.medsphere.fileman.FMRecord.java

private boolean setValue(AnnotatedElement ele, String value) {
    FMAnnotateFieldInfo annote = ele.getAnnotation(FMAnnotateFieldInfo.class);
    FIELDTYPE fieldType = annote.fieldType();
    boolean retVal = true;
    Object obj = null;/*from   ww w .  j  a va  2  s .  c o m*/
    try {
        if (value != null && !value.equals("")) {
            switch (fieldType) {
            case DATE: {
                obj = FMUtil.fmDateToDate(value);
                break;
            }
            case POINTER_TO_FILE: {
                obj = Integer.valueOf(value);
                break;
            }
            case NUMERIC: {
                obj = Double.valueOf(value);
                break;
            }
            default: {
                obj = value;
                break;
            }
            }
        }
        if (ele instanceof Field) {
            Field field = (Field) ele;
            if (valuesChanged(field.get(this), obj)) {
                field.set(this, obj);
            }
        } else if (ele instanceof Method) {
            Method method = (Method) ele;
            if (valuesChanged(method.getDefaultValue(), obj)) {
                invokeSetter(ele, method, obj);
            }
        }
        addModifiedField(annote.number());
    } catch (IllegalAccessException e) {
        // Bad but not overall fatal, so keep going.
        retVal = false;
    } catch (ParseException e) {
        // Bad but not overall fatal, so keep going.
        retVal = false;
    } catch (NumberFormatException e) {
        // Bad but not overall fatal, so keep going.
        retVal = false;
    } finally {
        // Empty block. Don't throw exceptions out of here.
    }
    return retVal;
}

From source file:org.apache.bval.jsr.AnnotationConstraintBuilder.java

@SuppressWarnings("unchecked")
private void buildPayload(final Method method) throws IllegalAccessException, InvocationTargetException {
    if (!TypeUtils.isAssignable(method.getReturnType(), ConstraintAnnotationAttributes.PAYLOAD.getType())) {
        throw new ConstraintDefinitionException("Return type for payload() must be of type "
                + ConstraintAnnotationAttributes.PAYLOAD.getType());
    }/*from www . j  a v  a2 s.  co m*/
    if (Object[].class.cast(method.getDefaultValue()).length > 0) {
        throw new ConstraintDefinitionException("Default value for payload() must be an empty array");
    }

    final Class<? extends Payload>[] payload_raw = (Class<? extends Payload>[]) method
            .invoke(constraintValidation.getAnnotation());

    final Set<Class<? extends Payload>> payloadSet;
    if (payload_raw == null) {
        payloadSet = Collections.<Class<? extends Payload>>emptySet();
    } else {
        payloadSet = new HashSet<Class<? extends Payload>>(payload_raw.length);
        Collections.addAll(payloadSet, payload_raw);
    }
    constraintValidation.setPayload(payloadSet);
}

From source file:org.grouplens.grapht.annotation.AnnotationProxy.java

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (isHashCode(method)) {
        return proxyHashCode(proxy);
    } else if (isEquals(method)) {
        return proxyEquals(proxy, args[0]);
    } else if (isAnnotationType(method)) {
        return proxyAnnotationType();
    } else if (isToString(method)) {
        return proxyToString(proxy);
    } else if (attributes.containsKey(method.getName()) && method.getParameterTypes().length == 0) {
        return copyAnnotationValue(attributes.get(method.getName()));
    } else {//from   www  .j a  va2  s  .c  o  m
        // fall back to the default
        return copyAnnotationValue(method.getDefaultValue());
    }
    // wait() and other Object methods do not get sent to the InvocationHandler
    // so we don't have any other cases
}