
cc.concurrent.mango.util.reflect.Reflection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
mango is a dao framework.
/*
* Copyright 2014 mango.concurrent.cc
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package cc.concurrent.mango.util.reflect;
import cc.concurrent.mango.exception.BeanInstantiationException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* @author ash
*/
public class Reflection {
public static T instantiate(Class clazz) throws BeanInstantiationException {
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "specified class is an interface");
}
try {
return clazz.newInstance();
} catch (InstantiationException ex) {
throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
} catch (IllegalAccessException ex) {
throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
}
}
public static T newProxy(
Class interfaceType, InvocationHandler handler) {
if (!interfaceType.isInterface()) {
throw new IllegalArgumentException("expected an interface to proxy, but " + interfaceType);
}
Object object = Proxy.newProxyInstance(
interfaceType.getClassLoader(),
new Class>[]{interfaceType},
handler);
return interfaceType.cast(object);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy