本文整理了Java中com.google.common.reflect.Reflection
类的一些代码示例,展示了Reflection
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reflection
类的具体详情如下:
包路径:com.google.common.reflect.Reflection
类名称:Reflection
[英]Static utilities relating to Java reflection.
[中]与Java反射相关的静态实用程序。
代码示例来源:origin: google/guava
private <T> T newProxy(final Class<T> interfaceType) {
return Reflection.newProxy(interfaceType, new FreshInvocationHandler(interfaceType));
}
代码示例来源:origin: google/guava
/**
* Returns the package name of the class, without attempting to load the class.
*
* <p>Behaves identically to {@link Package#getName()} but does not require the class (or
* package) to be loaded.
*/
public String getPackageName() {
return Reflection.getPackageName(className);
}
代码示例来源:origin: google/guava
public void testInitialize() {
assertEquals("This test can't be included twice in the same suite.", 0, classesInitialized);
Reflection.initialize(A.class);
assertEquals(1, classesInitialized);
Reflection.initialize(
A.class, // Already initialized (above)
B.class, C.class);
assertEquals(3, classesInitialized);
}
代码示例来源:origin: google/guava
void testInteraction(Function<? super T, ? extends T> wrapperFunction) {
T proxy = Reflection.newProxy(interfaceType, this);
T wrapper = wrapperFunction.apply(proxy);
boolean isPossibleChainingCall = interfaceType.isAssignableFrom(method.getReturnType());
try {
Object actualReturnValue = method.invoke(wrapper, passedArgs);
// If we think this might be a 'chaining' call then we allow the return value to either
// be the wrapper or the returnValue.
if (!isPossibleChainingCall || wrapper != actualReturnValue) {
assertEquals(
"Return value of " + method + " not forwarded", returnValue, actualReturnValue);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw Throwables.propagate(e.getCause());
}
assertEquals("Failed to forward to " + method, 1, called.get());
}
代码示例来源:origin: google/guava
/**
* Returns the package name of {@code clazz} according to the Java Language Specification (section
* 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
* attempting to define the {@link Package} and hence load files.
*/
public static String getPackageName(Class<?> clazz) {
return getPackageName(clazz.getName());
}
代码示例来源:origin: glowroot/glowroot
Reflection.initialize(Container.class);
代码示例来源:origin: google/guava
private static <T> void testExceptionPropagation(
Class<T> interfaceType, Method method, Function<? super T, ? extends T> wrapperFunction) {
final RuntimeException exception = new RuntimeException();
T proxy =
Reflection.newProxy(
interfaceType,
new AbstractInvocationHandler() {
@Override
protected Object handleInvocation(Object p, Method m, Object[] args)
throws Throwable {
throw exception;
}
});
T wrapper = wrapperFunction.apply(proxy);
try {
method.invoke(wrapper, getParameterValues(method));
fail(method + " failed to throw exception as is.");
} catch (InvocationTargetException e) {
if (exception != e.getCause()) {
throw new RuntimeException(e);
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
代码示例来源:origin: google/guava
private FactoryMethodReturnValueTester(
Class<?> declaringClass,
ImmutableList<Invokable<?, ?>> factories,
String factoryMethodsDescription) {
this.declaringClass = declaringClass;
this.factories = factories;
this.factoryMethodsDescription = factoryMethodsDescription;
packagesToTest.add(Reflection.getPackageName(declaringClass));
}
代码示例来源:origin: com.google.guava/guava-tests
public void testInitialize() {
assertEquals("This test can't be included twice in the same suite.", 0, classesInitialized);
Reflection.initialize(A.class);
assertEquals(1, classesInitialized);
Reflection.initialize(
A.class, // Already initialized (above)
B.class,
C.class);
assertEquals(3, classesInitialized);
}
代码示例来源:origin: prestodb/presto
@Override
public void configure(Binder binder)
{
// Install no-op session supplier on workers, since only coordinators create sessions.
binder.bind(SessionSupplier.class).to(NoOpSessionSupplier.class).in(Scopes.SINGLETON);
// Install no-op resource group manager on workers, since only coordinators manage resource groups.
binder.bind(ResourceGroupManager.class).to(NoOpResourceGroupManager.class).in(Scopes.SINGLETON);
// Install no-op transaction manager on workers, since only coordinators manage transactions.
binder.bind(TransactionManager.class).to(NoOpTransactionManager.class).in(Scopes.SINGLETON);
// Install no-op failure detector on workers, since only coordinators need global node selection.
binder.bind(FailureDetector.class).to(NoOpFailureDetector.class).in(Scopes.SINGLETON);
// HACK: this binding is needed by SystemConnectorModule, but will only be used on the coordinator
binder.bind(QueryManager.class).toInstance(newProxy(QueryManager.class, (proxy, method, args) -> {
throw new UnsupportedOperationException();
}));
}
代码示例来源:origin: google/j2objc
/**
* Returns the package name of the class, without attempting to load the class.
*
* <p>Behaves identically to {@link Package#getName()} but does not require the class (or
* package) to be loaded.
*/
public String getPackageName() {
return Reflection.getPackageName(className);
}
代码示例来源:origin: jooby-project/jooby
private static <T> T empty(final Class<T> type) {
return Reflection.newProxy(type, (proxy, method, args) -> {
throw new UnsupportedOperationException(method.toString());
});
}
代码示例来源:origin: wildfly/wildfly
/**
* Returns the package name of the class, without attempting to load the class.
*
* <p>Behaves identically to {@link Package#getName()} but does not require the class (or
* package) to be loaded.
*/
public String getPackageName() {
return Reflection.getPackageName(className);
}
代码示例来源:origin: jooby-project/jooby
@SuppressWarnings("unchecked")
private static <T> AnnotatedType<T> createAnnotatedType(final Class<T> type) {
return Reflection.newProxy(AnnotatedType.class, (proxy, method, args) -> {
if (method.getName().equals("getJavaClass")) {
return type;
}
throw new UnsupportedOperationException(method.toString());
});
}
}
代码示例来源:origin: google/j2objc
/**
* Returns the package name of {@code clazz} according to the Java Language Specification (section
* 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
* attempting to define the {@link Package} and hence load files.
*/
public static String getPackageName(Class<?> clazz) {
return getPackageName(clazz.getName());
}
代码示例来源:origin: google/guava
private static <D extends GenericDeclaration> TypeVariable<D> newTypeVariableImpl(
D genericDeclaration, String name, Type[] bounds) {
TypeVariableImpl<D> typeVariableImpl =
new TypeVariableImpl<D>(genericDeclaration, name, bounds);
@SuppressWarnings("unchecked")
TypeVariable<D> typeVariable =
Reflection.newProxy(
TypeVariable.class, new TypeVariableInvocationHandler(typeVariableImpl));
return typeVariable;
}
代码示例来源:origin: wildfly/wildfly
/**
* Returns the package name of {@code clazz} according to the Java Language Specification (section
* 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
* attempting to define the {@link Package} and hence load files.
*/
public static String getPackageName(Class<?> clazz) {
return getPackageName(clazz.getName());
}
代码示例来源:origin: jooby-project/jooby
@SuppressWarnings("unchecked")
private static <T> CreationalContext<T> createCreationalContext() {
return Reflection.newProxy(CreationalContext.class, (proxy, method, args) -> {
if (method.getName().equals("release")) {
return null;
}
throw new UnsupportedOperationException(method.toString());
});
}
代码示例来源:origin: google/guava
private ImmutableList<Method> getVisibleMethods(Class<?> cls) {
// Don't use cls.getPackage() because it does nasty things like reading
// a file.
String visiblePackage = Reflection.getPackageName(cls);
ImmutableList.Builder<Method> builder = ImmutableList.builder();
for (Class<?> type : TypeToken.of(cls).getTypes().rawTypes()) {
if (!Reflection.getPackageName(type).equals(visiblePackage)) {
break;
}
for (Method method : type.getDeclaredMethods()) {
if (!method.isSynthetic() && isVisible(method)) {
builder.add(method);
}
}
}
return builder.build();
}
}
代码示例来源:origin: jooby-project/jooby
private Object newBeanInterface(final Request req, final Response rsp, final Route.Chain chain,
final Class<?> beanType) {
return Reflection.newProxy(beanType, (proxy, method, args) -> {
StringBuilder name = new StringBuilder(method.getName()
.replace("get", "")
.replace("is", ""));
name.setCharAt(0, Character.toLowerCase(name.charAt(0)));
return value(new RequestParam(method, name.toString(), method.getGenericReturnType()), req,
rsp, chain);
});
}
内容来源于网络,如有侵权,请联系作者删除!