下面列出了java.lang.reflect.InvocationHandler#invoke ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
private static <T> T getProxyStatic(
Proxy proxyFactory,
ClassLoader loader,
Class<?> interfaces,
InvocationHandler handler,
T instance,
Function1<MethodInfo, Boolean> filter)
{
InvocationHandler proxyHandler = filter != null ?
(InvocationHandler & Serializable) (proxy, method, args) -> {
if (filter.apply(MethodInfo.of(method))) {
return handler.invoke(proxy, method, args);
}
else {
return method.invoke(instance, args);
}
}
: handler;
return proxyFactory.getProxy(loader, proxyHandler, interfaces);
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
// Note that the declaring class in method is the interface
// in which the method was defined, not the proxy class.
Class cls = method.getDeclaringClass() ;
InvocationHandler handler =
(InvocationHandler)classToInvocationHandler.get( cls ) ;
if (handler == null) {
if (defaultHandler != null)
handler = defaultHandler ;
else {
ORBUtilSystemException wrapper = ORBUtilSystemException.get(
CORBALogDomains.UTIL ) ;
throw wrapper.noInvocationHandler( "\"" + method.toString() +
"\"" ) ;
}
}
// handler should never be null here.
return handler.invoke( proxy, method, args ) ;
}
private ApiScope getScope(Annotation currentMethod) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod);
Annotation[] extensions = (Annotation[]) methodHandler.invoke(currentMethod,
apiOperation.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null);
if (extensions != null) {
methodHandler = Proxy.getInvocationHandler(extensions[0]);
Annotation[] properties = (Annotation[]) methodHandler.invoke(extensions[0], extensionClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES, null), null);
String scopeKey;
String propertyName;
for (Annotation property : properties) {
methodHandler = Proxy.getInvocationHandler(property);
propertyName = (String) methodHandler.invoke(property, extensionPropertyClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null);
if (ANNOTATIONS_SCOPE.equals(propertyName)) {
scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null), null);
if (scopeKey.isEmpty()) {
return null;
}
return apiScopes.get(scopeKey);
}
}
}
return null;
}
/**
* Finds a registered mock that can handle the method invocation and delegate to it. If there is no such mock throws
* IllegalStateException.
*
* @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])
*/
@Override
public Object invoke( final Object proxy, final Method method, final Object[] args )
throws Throwable
{
System.out.println( "Play mock for " + method );
for( MockResolver mockResolver : mockRepository.getAll() )
{
InvocationHandler handler = mockResolver.getInvocationHandler( proxy, method, args );
if( handler != null )
{
return handler.invoke( mockResolver, method, args );
}
}
throw new IllegalStateException( "There is no mock registered that can handle " + method );
}
public boolean isUseServerPrepare(Statement statement) {
if(statement instanceof PGStatement) {
PGStatement pgStatement = (PGStatement) statement;
return pgStatement.isUseServerPrepare();
} else {
InvocationHandler handler = Proxy.getInvocationHandler(statement);
try {
return (boolean) handler.invoke(statement, PGStatement.class.getMethod("isUseServerPrepare"), null);
} catch (Throwable e) {
throw new IllegalArgumentException(e);
}
}
}
private void invokeSideEffects( Object proxy,
Method method,
Object[] params,
Object result,
Throwable originalThrowable
)
throws Throwable
{
proxyHandler.setProxy( proxy );
resultInvocationHandler.setResult( result, originalThrowable );
try
{
for( InvocationHandler sideEffect : sideEffects )
{
try
{
sideEffect.invoke( proxy, method, params );
}
catch( Throwable throwable )
{
if( throwable != originalThrowable )
{
throwable.printStackTrace();
}
}
}
}
finally
{
proxyHandler.clearProxy();
resultInvocationHandler.setResult( null, null );
}
}
/**
* When an annotation and method is passed, this method invokes that executes said method against the annotation
*
* @param method
* @param annotation
* @param returnType
* @return
* @throws Throwable
*/
private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
switch (returnType) {
case STRING:
return (String) methodHandler.invoke(annotation, method, null);
case STRING_ARR:
return ((String[]) methodHandler.invoke(annotation, method, null))[0];
default:
return null;
}
}
public void setPrepareThreshold(Statement statement, int threshold) throws SQLException {
if(statement instanceof PGStatement) {
PGStatement pgStatement = (PGStatement) statement;
pgStatement.setPrepareThreshold(threshold);
} else {
InvocationHandler handler = Proxy.getInvocationHandler(statement);
try {
handler.invoke(statement, PGStatement.class.getMethod("setPrepareThreshold", int.class), new Object[]{threshold});
} catch (Throwable throwable) {
throw new IllegalArgumentException(throwable);
}
}
}
private Map<String,Scope> processAPIScopes(Annotation annotation) throws Throwable {
Map<String, Scope> scopes = new HashMap<>();
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
Annotation[] annotatedScopes = (Annotation[]) methodHandler.invoke(annotation, scopesClass
.getMethod(ANNOTATIONS_SCOPES, null), null);
Scope scope;
String permissions[];
StringBuilder aggregatedPermissions;
for(int i=0; i<annotatedScopes.length; i++){
aggregatedPermissions = new StringBuilder();
methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
scope = new Scope();
scope.setName(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME), annotatedScopes[i], STRING));
scope.setDescription(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_DESCRIPTION), annotatedScopes[i], STRING));
scope.setKey(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_KEY), annotatedScopes[i], STRING));
permissions = (String[])methodHandler.invoke(annotatedScopes[i], scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS, null),null);
for (String permission : permissions) {
aggregatedPermissions.append(PERMISSION_PREFIX);
aggregatedPermissions.append(permission);
aggregatedPermissions.append(" ");
}
scope.setRoles(aggregatedPermissions.toString());
scopes.put(scope.getKey(), scope);
}
return scopes;
}
private Map<String,ApiScope> processAPIScopes(Annotation annotation) throws Throwable {
Map<String, ApiScope> scopes = new HashMap<>();
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
Annotation[] annotatedScopes = (Annotation[]) methodHandler.invoke(annotation, scopesClass
.getMethod(ANNOTATIONS_SCOPES, null), null);
ApiScope scope;
String permissions[];
StringBuilder aggregatedPermissions;
for(int i=0; i<annotatedScopes.length; i++){
aggregatedPermissions = new StringBuilder();
methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
scope = new ApiScope();
scope.setName(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME), annotatedScopes[i], STRING));
scope.setDescription(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_DESCRIPTION), annotatedScopes[i], STRING));
scope.setKey(invokeMethod(scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_KEY), annotatedScopes[i], STRING));
permissions = (String[])methodHandler.invoke(annotatedScopes[i], scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS, null),null);
for (String permission : permissions) {
aggregatedPermissions.append(PERMISSION_PREFIX);
aggregatedPermissions.append(permission);
aggregatedPermissions.append(" ");
}
scope.setRoles(aggregatedPermissions.toString().trim());
scopes.put(scope.getKey(), scope);
}
return scopes;
}
/**
* When an annotation and method is passed, this method invokes that executes said method against the annotation
*
* @param method
* @param annotation
* @param returnType
* @return
* @throws Throwable
*/
private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
switch (returnType) {
case STRING:
return (String) methodHandler.invoke(annotation, method, null);
case STRING_ARR:
return ((String[]) methodHandler.invoke(annotation, method, null))[0];
default:
return null;
}
}
/**
* Iterate API annotation and build API Configuration
*
* @param annotation reading @SwaggerDefinition annotation
* @return APIResourceConfiguration which compose with an API information which has its name, context,version,and tags
* @throws Throwable
*/
private APIResourceConfiguration processAPIAnnotation(Annotation annotation) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
Annotation info = (Annotation) methodHandler.invoke(annotation, apiClazz
.getMethod(SWAGGER_ANNOTATIONS_INFO,null),null);
Annotation[] tags = (Annotation[]) methodHandler.invoke(annotation, apiClazz
.getMethod(SWAGGER_ANNOTATIONS_TAGS,null),null);
String[] tagNames = new String[tags.length];
for(int i=0; i<tags.length; i++){
methodHandler = Proxy.getInvocationHandler(tags[i]);
tagNames[i]=(String)methodHandler.invoke(tags[i], tagClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null),null);
}
methodHandler = Proxy.getInvocationHandler(info);
String version = (String)methodHandler.invoke(info, infoClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VERSION,null),null);
if("".equals(version))return null;
Annotation[] apiInfo = (Annotation[])methodHandler.invoke(info, infoClass
.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS,null),null);
methodHandler = Proxy.getInvocationHandler(apiInfo[0]);
Annotation[] properties = (Annotation[])methodHandler.invoke(apiInfo[0], extensionClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES,null), null);
APIResourceConfiguration apiResourceConfig = new APIResourceConfiguration();
for (Annotation property : properties) {
methodHandler = Proxy.getInvocationHandler(property);
String key = (String) methodHandler.invoke(property, extensionPropertyClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null),
null);
String value = (String) methodHandler.invoke(property, extensionPropertyClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null),null);
if ("".equals(key)) return null;
switch (key) {
case SWAGGER_ANNOTATIONS_PROPERTIES_NAME:
if ("".equals(value)) return null;
apiResourceConfig.setName(value);
break;
case SWAGGER_ANNOTATIONS_PROPERTIES_CONTEXT:
if ("".equals(value)) return null;
apiResourceConfig.setContext(value);
break;
default:
break;
}
}
apiResourceConfig.setVersion(version);
apiResourceConfig.setTags(tagNames);
return apiResourceConfig;
}