Java Class New Instance newInstanceWithDefaults(Class annotationType)

Here you can find the source of newInstanceWithDefaults(Class annotationType)

Description

new Instance With Defaults

License

Open Source License

Declaration

public static <T extends Annotation> T newInstanceWithDefaults(Class<T> annotationType) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.lang.annotation.Annotation;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Main {
    public static <T extends Annotation> T newInstanceWithDefaults(Class<T> annotationType) {
        Object proxy = Proxy.newProxyInstance(annotationType.getClassLoader(), new Class<?>[] { annotationType },
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        return method.getDefaultValue();
                    }//from  w  w  w.  ja  va2 s. com
                });
        return annotationType.cast(proxy);
    }
}

Related

  1. newInstanceOf(Class clazz)
  2. newInstanceOf(String className)
  3. newInstanceOrThrow(final Class clazz)
  4. newInstancesViaMetaAnnotation(Class declarator, Class metaAnnotationClass, Class expected)
  5. newInstanceViaAnnotation(Class declarator, Annotation annotation, Class expected, Annotation parameter)
  6. newInstanceWithFill(Class componentType, int length, Object filledValue)
  7. newInstanceWithoutInit(Class clazz)
  8. newInstanceWithParameterTypes(Constructor constructor)