
cn.taketoday.bytecode.proxy.DispatcherGenerator Maven / Gradle / Ivy
/*
* Original Author -> Harry Yang ([email protected]) https://taketoday.cn
* Copyright © TODAY & 2017 - 2022 All Rights Reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/]
*/
package cn.taketoday.bytecode.proxy;
import java.lang.reflect.Modifier;
import java.util.List;
import cn.taketoday.bytecode.Type;
import cn.taketoday.bytecode.core.ClassEmitter;
import cn.taketoday.bytecode.core.CodeEmitter;
import cn.taketoday.bytecode.core.MethodInfo;
import cn.taketoday.bytecode.commons.MethodSignature;
/**
* @author TODAY
* 2018-11-08 15:09
*/
final class DispatcherGenerator implements CallbackGenerator {
public static final DispatcherGenerator INSTANCE = new DispatcherGenerator(false);
public static final DispatcherGenerator PROXY_REF_INSTANCE = new DispatcherGenerator(true);
private static final Type DISPATCHER = Type.fromClass(Dispatcher.class);
private static final Type PROXY_REF_DISPATCHER = Type.fromClass(ProxyRefDispatcher.class);
private static final MethodSignature LOAD_OBJECT = MethodSignature.from("Object loadObject()");
private static final MethodSignature PROXY_REF_LOAD_OBJECT = MethodSignature.from("Object loadObject(Object)");
private final boolean proxyRef;
private DispatcherGenerator(boolean proxyRef) {
this.proxyRef = proxyRef;
}
@Override
public void generate(ClassEmitter ce, Context context, List methods) {
for (final MethodInfo method : methods) {
if (!Modifier.isProtected(method.getModifiers())) {
final CodeEmitter e = context.beginMethod(ce, method);
context.emitCallback(e, context.getIndex(method));
if (proxyRef) {
e.loadThis();
e.invokeInterface(PROXY_REF_DISPATCHER, PROXY_REF_LOAD_OBJECT);
}
else {
e.invokeInterface(DISPATCHER, LOAD_OBJECT);
}
e.checkCast(method.getClassInfo().getType());
e.loadArgs();
e.invoke(method);
e.returnValue();
e.end_method();
}
}
}
@Override
public void generateStatic(CodeEmitter e, Context context, List methods) throws Exception {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy