Java Code Examples for java.lang.reflect.Method.getDefaultValue()

The following are Jave code examples for showing how to use getDefaultValue() of the java.lang.reflect.Method class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenJSharp   File: AnnotationAttributeBase.java   View Source Code Vote up 5 votes
private static void setDefaults(HashMap map, Class annotationClass)
{
    for (Method m : annotationClass.getDeclaredMethods())
    {
        Object defaultValue = m.getDefaultValue();
        // TODO throw exception if default is missing for method that doesn't yet have a value
        if (defaultValue != null && !map.containsKey(m.getName()))
        {
            map.put(m.getName(), defaultValue);
        }
    }
}
 
Example 2
Project: businessworks   File: Annotations.java   View Source Code Vote up 5 votes
public static boolean isAllDefaultMethods(Class<? extends Annotation> annotationType) {
  boolean hasMethods = false;
  for (Method m : annotationType.getDeclaredMethods()) {
    hasMethods = true;
    if (m.getDefaultValue() == null) {
      return false;
    }
  }
  return hasMethods;
}
 
Example 3
Project: openjdk-jdk10   File: TypedAnnotationWriter.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    if(method.getDeclaringClass()==JAnnotationWriter.class) {
        try {
            return method.invoke(this,args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }

    String name = method.getName();
    Object arg=null;
    if(args!=null && args.length>0)
        arg = args[0];

    // check how it's defined on the annotation
    Method m = annotation.getDeclaredMethod(name);
    Class<?> rt = m.getReturnType();

    // array value
    if(rt.isArray()) {
        return addArrayValue(proxy,name,rt.getComponentType(),method.getReturnType(),arg);
    }

    // sub annotation
    if(Annotation.class.isAssignableFrom(rt)) {
        Class<? extends Annotation> r = (Class<? extends Annotation>)rt;
        return new TypedAnnotationWriter(
            r,method.getReturnType(),use.annotationParam(name,r)).createProxy();
    }

    // scalar value

    if(arg instanceof JType) {
        JType targ = (JType) arg;
        checkType(Class.class,rt);
        if(m.getDefaultValue()!=null) {
            // check the default
            if(targ.equals(targ.owner().ref((Class)m.getDefaultValue())))
                return proxy;   // defaulted
        }
        use.param(name,targ);
        return proxy;
    }

    // other Java built-in types
    checkType(arg.getClass(),rt);
    if(m.getDefaultValue()!=null && m.getDefaultValue().equals(arg))
        // defaulted. no need to write out.
        return proxy;

    if(arg instanceof String) {
        use.param(name,(String)arg);
        return proxy;
    }
    if(arg instanceof Boolean) {
        use.param(name,(Boolean)arg);
        return proxy;
    }
    if(arg instanceof Integer) {
        use.param(name,(Integer)arg);
        return proxy;
    }
    if(arg instanceof Class) {
        use.param(name,(Class)arg);
        return proxy;
    }
    if(arg instanceof Enum) {
        use.param(name,(Enum)arg);
        return proxy;
    }

    throw new IllegalArgumentException("Unable to handle this method call "+method.toString());
}
 
Example 4
Project: OpenJSharp   File: TypedAnnotationWriter.java   View Source Code Vote up 4 votes
@SuppressWarnings("unchecked")
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    if(method.getDeclaringClass()==JAnnotationWriter.class) {
        try {
            return method.invoke(this,args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }

    String name = method.getName();
    Object arg=null;
    if(args!=null && args.length>0)
        arg = args[0];

    // check how it's defined on the annotation
    Method m = annotation.getDeclaredMethod(name);
    Class<?> rt = m.getReturnType();

    // array value
    if(rt.isArray()) {
        return addArrayValue(proxy,name,rt.getComponentType(),method.getReturnType(),arg);
    }

    // sub annotation
    if(Annotation.class.isAssignableFrom(rt)) {
        Class<? extends Annotation> r = (Class<? extends Annotation>)rt;
        return new TypedAnnotationWriter(
            r,method.getReturnType(),use.annotationParam(name,r)).createProxy();
    }

    // scalar value

    if(arg instanceof JType) {
        JType targ = (JType) arg;
        checkType(Class.class,rt);
        if(m.getDefaultValue()!=null) {
            // check the default
            if(targ.equals(targ.owner().ref((Class)m.getDefaultValue())))
                return proxy;   // defaulted
        }
        use.param(name,targ);
        return proxy;
    }

    // other Java built-in types
    checkType(arg.getClass(),rt);
    if(m.getDefaultValue()!=null && m.getDefaultValue().equals(arg))
        // defaulted. no need to write out.
        return proxy;

    if(arg instanceof String) {
        use.param(name,(String)arg);
        return proxy;
    }
    if(arg instanceof Boolean) {
        use.param(name,(Boolean)arg);
        return proxy;
    }
    if(arg instanceof Integer) {
        use.param(name,(Integer)arg);
        return proxy;
    }
    if(arg instanceof Class) {
        use.param(name,(Class)arg);
        return proxy;
    }
    if(arg instanceof Enum) {
        use.param(name,(Enum)arg);
        return proxy;
    }

    throw new IllegalArgumentException("Unable to handle this method call "+method.toString());
}
 
Example 5
Project: junit-extensions   File: AssertionUtil.java   View Source Code Vote up 4 votes
public static int getDefaultSizeOfRandom() throws Exception {
  Class<?> clazz = Random.class;
  Method method = clazz.getDeclaredMethod("size");
  return (Integer) method.getDefaultValue();
}