com.ovea.tajin.support.guice.ExpandHandler.groovy Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Ovea
*
* Licensed 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 com.ovea.tajin.support.guice
import com.google.inject.TypeLiteral
import com.mycila.inject.injector.MethodHandlerSkeleton
import javax.inject.Singleton
import java.lang.reflect.Method
/**
* @author Mathieu Carbou ([email protected])
*/
@Singleton
class ExpandHandler extends MethodHandlerSkeleton {
@Override
void handle(TypeLiteral extends T> type, T instance, Method method, Expand annotation) {
Class> modelType = annotation.value()
String name = annotation.name() ?: method.name
Class>[] paramTypes = method.parameterTypes
MetaMethod mm = instance.metaClass.pickMethod(method.name, paramTypes)
if (paramTypes && paramTypes[0] == modelType) {
modelType.metaClass[name] << {Object[] args ->
Object[] params = new Object[args.length + 1]
params[0] = delegate
if (args.length) {
System.arraycopy(args, 0, params, 1, args.length)
}
def pTypes = mm.parameterTypes
if (params.size() != pTypes.length) {
throw new IllegalArgumentException('Bad argument size')
}
for (int i = 0; i < pTypes.length; i++) {
if (params[i] != null && !pTypes[i].isDirectlyAssignable(params[i])) {
params[i] = params[i].asType(pTypes[i].theClass)
}
}
return mm.doMethodInvoke(instance, params)
}
} else {
modelType.metaClass.static[name] << {Object[] args ->
Object[] params = args ? (Object[]) args[0] : args
def pTypes = mm.parameterTypes
if (params.length != pTypes.length) {
throw new IllegalArgumentException('Bad argument size')
}
for (int i = 0; i < pTypes.length; i++) {
if (params[i] != null && !pTypes[i].isDirectlyAssignable(params[i])) {
params[i] = params[i].asType(pTypes[i].theClass)
}
}
return mm.doMethodInvoke(instance, params)
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy