use of controlP5.Label in project bgpcep by opendaylight.
the class Type1LabelParser method serializeLabel.
@Override
public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
Preconditions.checkArgument(subobject instanceof Type1LabelCase, "Unknown Label Subobject instance. Passed {}. Needed Type1LabelCase.", subobject.getClass());
final ByteBuf body = Unpooled.buffer(LABEL_LENGTH);
final Type1Label type1Label = ((Type1LabelCase) subobject).getType1Label();
Preconditions.checkArgument(type1Label != null, "Type1Label is mandatory.");
writeUnsignedInt(type1Label.getType1Label(), body);
LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
}
use of controlP5.Label in project component-runtime by Talend.
the class ProxyGenerator method delegateMethod.
private void delegateMethod(final ClassWriter cw, final Method method, final String proxyClassFileName, final int methodIndex) {
final Class<?> returnType = method.getReturnType();
final Class<?>[] parameterTypes = method.getParameterTypes();
final Class<?>[] exceptionTypes = method.getExceptionTypes();
final int modifiers = method.getModifiers();
if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
throw new IllegalStateException("It's not possible to proxy a final or static method: " + method.getDeclaringClass().getName() + " " + method.getName());
}
// push the method definition
int modifier = modifiers & (ACC_PUBLIC | ACC_PROTECTED | ACC_VARARGS);
MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null);
mv.visitCode();
// push try/catch block, to catch declared exceptions, and to catch java.lang.Throwable
final Label l0 = new Label();
final Label l1 = new Label();
final Label l2 = new Label();
if (exceptionTypes.length > 0) {
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
}
// push try code
mv.visitLabel(l0);
final String classNameToOverride = method.getDeclaringClass().getName().replace('.', '/');
mv.visitLdcInsn(Type.getType("L" + classNameToOverride + ";"));
// the following code generates the bytecode for this line of Java:
// Method method = <proxy>.class.getMethod("add", new Class[] { <array of function argument classes> });
// get the method name to invoke, and push to stack
mv.visitLdcInsn(method.getName());
// create the Class[]
createArrayDefinition(mv, parameterTypes.length, Class.class);
int length = 1;
// push parameters into array
for (int i = 0; i < parameterTypes.length; i++) {
// keep copy of array on stack
mv.visitInsn(DUP);
Class<?> parameterType = parameterTypes[i];
// push number onto stack
pushIntOntoStack(mv, i);
if (parameterType.isPrimitive()) {
String wrapperType = getWrapperType(parameterType);
mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
} else {
mv.visitLdcInsn(Type.getType(parameterType));
}
mv.visitInsn(AASTORE);
if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
length += 2;
} else {
length++;
}
}
// the following code generates bytecode equivalent to:
// return ((<returntype>) invocationHandler.invoke(this, {methodIndex}, new Object[] { <function arguments
// }))[.<primitive>Value()];
final Label l4 = new Label();
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, 0);
// get the invocationHandler field from this class
mv.visitFieldInsn(GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
// add the Method from the static array as first parameter
mv.visitFieldInsn(GETSTATIC, proxyClassFileName, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class));
// push the methodIndex of the current method
if (methodIndex < 128) {
mv.visitIntInsn(BIPUSH, methodIndex);
} else if (methodIndex < 32267) {
// for methods > 127 we need to push a short number as index
mv.visitIntInsn(SIPUSH, methodIndex);
} else {
throw new IllegalStateException("Sorry, we only support Classes with 2^15 methods...");
}
// and now load the Method from the array
mv.visitInsn(AALOAD);
// prepare the parameter array as Object[] and store it on the stack
pushMethodParameterArray(mv, parameterTypes);
// invoke the invocationHandler
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke", "(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true);
// cast the result
mv.visitTypeInsn(CHECKCAST, getCastType(returnType));
if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType))) {
// get the primitive value
mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + Type.getDescriptor(returnType), false);
}
// push return
mv.visitLabel(l1);
if (!Void.TYPE.equals(returnType)) {
mv.visitInsn(getReturnInsn(returnType));
} else {
mv.visitInsn(POP);
mv.visitInsn(RETURN);
}
// catch InvocationTargetException
if (exceptionTypes.length > 0) {
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, length);
Label l5 = new Label();
mv.visitLabel(l5);
for (int i = 0; i < exceptionTypes.length; i++) {
Class<?> exceptionType = exceptionTypes[i];
mv.visitLdcInsn(Type.getType("L" + exceptionType.getCanonicalName().replace('.', '/') + ";"));
mv.visitVarInsn(ALOAD, length);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
final Label l6 = new Label();
mv.visitJumpInsn(IFEQ, l6);
final Label l7 = new Label();
mv.visitLabel(l7);
mv.visitVarInsn(ALOAD, length);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitTypeInsn(CHECKCAST, getCastType(exceptionType));
mv.visitInsn(ATHROW);
mv.visitLabel(l6);
if (i == (exceptionTypes.length - 1)) {
mv.visitTypeInsn(NEW, "java/lang/reflect/UndeclaredThrowableException");
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, length);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>", "(Ljava/lang/Throwable;)V", false);
mv.visitInsn(ATHROW);
}
}
}
// finish this method
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of controlP5.Label in project jodd by oblac.
the class ProxyAspectData method readAdviceData.
// ---------------------------------------------------------------- read
/**
* Parse advice class to gather some advice data. Should be called before any advice use.
* Must be called only *once* per advice.
*/
private void readAdviceData() {
if (ready) {
return;
}
adviceClassReader.accept(new EmptyClassVisitor() {
/**
* Stores advice reference.
*/
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
adviceReference = name;
super.visit(version, access, name, signature, superName, interfaces);
}
/**
* Prevents advice to have inner classes.
*/
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
if (outerName.equals(adviceReference)) {
throw new ProxettaException("Proxetta doesn't allow inner classes in/for advice: " + advice.getName());
}
super.visitInnerClass(name, outerName, innerName, access);
}
/**
* Clones advices fields to destination.
*/
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
// [A5]
wd.dest.visitField(access, adviceFieldName(name, aspectIndex), desc, signature, value);
return super.visitField(access, name, desc, signature, value);
}
/**
* Copies advices methods to destination.
*/
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals(CLINIT)) {
// [A6]
if (!desc.equals(DESC_VOID)) {
throw new ProxettaException("Invalid static initialization block description for advice: " + advice.getName());
}
name = clinitMethodName + methodDivider + aspectIndex;
access |= AsmUtil.ACC_PRIVATE | AsmUtil.ACC_FINAL;
wd.addAdviceClinitMethod(name);
return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
}
@Override
public void visitLineNumber(int line, Label start) {
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
if (opcode == INVOKESTATIC) {
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceMethodName(name, aspectIndex);
}
}
super.visitMethodInsn(opcode, owner, name, desc, isInterface);
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
// [F6]
if (owner.equals(adviceReference)) {
// [F5]
owner = wd.thisReference;
name = adviceFieldName(name, aspectIndex);
}
super.visitFieldInsn(opcode, owner, name, desc);
}
};
} else if (name.equals(INIT)) {
// [A7]
if (!desc.equals(DESC_VOID)) {
throw new ProxettaException("Advices can have only default constructors. Invalid advice: " + advice.getName());
}
name = initMethodName + methodDivider + aspectIndex;
access = ProxettaAsmUtil.makePrivateFinalAccess(access);
wd.addAdviceInitMethod(name);
return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
}
@Override
public void visitLineNumber(int line, Label start) {
}
// used to detect and to ignore the first super call()
int state;
@Override
public void visitVarInsn(int opcode, int var) {
// [F7]
if ((state == 0) && (opcode == ALOAD) && (var == 0)) {
state++;
return;
}
super.visitVarInsn(opcode, var);
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
if ((state == 1) && (opcode == INVOKESPECIAL)) {
state++;
return;
}
if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE)) {
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceMethodName(name, aspectIndex);
}
} else if (opcode == INVOKESTATIC) {
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceMethodName(name, aspectIndex);
}
}
super.visitMethodInsn(opcode, owner, name, desc, isInterface);
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
// [F7]
if (owner.equals(adviceReference)) {
// [F5]
owner = wd.thisReference;
name = adviceFieldName(name, aspectIndex);
}
super.visitFieldInsn(opcode, owner, name, desc);
}
};
} else // other methods
if (!name.equals(executeMethodName)) {
name = adviceMethodName(name, aspectIndex);
return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
}
@Override
public void visitLineNumber(int line, Label start) {
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE)) {
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceMethodName(name, aspectIndex);
}
} else if (opcode == INVOKESTATIC || opcode == INVOKESPECIAL) {
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceMethodName(name, aspectIndex);
}
}
super.visitMethodInsn(opcode, owner, name, desc, isInterface);
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
// replace field references
if (owner.equals(adviceReference)) {
owner = wd.thisReference;
name = adviceFieldName(name, aspectIndex);
}
super.visitFieldInsn(opcode, owner, name, desc);
}
};
}
//return new MethodAdapter(new EmptyMethodVisitor()) { // toask may we replace this with the following code?
return new EmptyMethodVisitor() {
@Override
public void visitVarInsn(int opcode, int var) {
if (isStoreOpcode(opcode)) {
if (var > maxLocalVarOffset) {
// find max local var offset
maxLocalVarOffset = var;
}
}
super.visitVarInsn(opcode, var);
}
};
// return super.visitMethod(access, name, desc, signature, exceptions);
}
}, 0);
// increment offset by 2 because var on last index may be a dword value
maxLocalVarOffset += 2;
ready = true;
}
use of controlP5.Label in project intellij-community by JetBrains.
the class AdvancedEnhancer method emitMethods.
private void emitMethods(final ClassEmitter ce, Map<Method, MethodInfo> methodMap, final Map<Method, Method> covariantMethods) {
CallbackGenerator[] generators = CallbackInfo.getGenerators(callbackTypes);
Map<MethodInfo, MethodInfo> covariantInfoMap = new HashMap<>();
for (Method method : methodMap.keySet()) {
final Method delegate = covariantMethods.get(method);
if (delegate != null) {
covariantInfoMap.put(methodMap.get(method), ReflectUtils.getMethodInfo(delegate, delegate.getModifiers()));
}
}
BridgeMethodGenerator bridgeMethodGenerator = new BridgeMethodGenerator(covariantInfoMap);
Map<CallbackGenerator, List<MethodInfo>> groups = new HashMap<>();
final Map<MethodInfo, Integer> indexes = new HashMap<>();
final Map<MethodInfo, Integer> originalModifiers = new HashMap<>();
final Map positions = CollectionUtils.getIndexMap(new ArrayList<>(methodMap.values()));
for (Method actualMethod : methodMap.keySet()) {
MethodInfo method = methodMap.get(actualMethod);
int index = filter.accept(actualMethod);
if (index >= callbackTypes.length) {
throw new IllegalArgumentException("Callback filter returned an index that is too large: " + index);
}
originalModifiers.put(method, (actualMethod != null) ? actualMethod.getModifiers() : method.getModifiers());
indexes.put(method, index);
final CallbackGenerator generator = covariantMethods.containsKey(actualMethod) ? bridgeMethodGenerator : generators[index];
List<MethodInfo> group = groups.get(generator);
if (group == null) {
groups.put(generator, group = new ArrayList<>(methodMap.size()));
}
group.add(method);
}
CodeEmitter se = ce.getStaticHook();
se.new_instance(THREAD_LOCAL);
se.dup();
se.invoke_constructor(THREAD_LOCAL, CSTRUCT_NULL);
se.putfield(THREAD_CALLBACKS_FIELD);
CallbackGenerator.Context context = new CallbackGenerator.Context() {
public ClassLoader getClassLoader() {
return AdvancedEnhancer.this.getClassLoader();
}
public int getOriginalModifiers(MethodInfo method) {
return originalModifiers.get(method);
}
public int getIndex(MethodInfo method) {
return indexes.get(method);
}
public void emitCallback(CodeEmitter e, int index) {
emitCurrentCallback(e, index);
}
public Signature getImplSignature(MethodInfo method) {
return rename(method.getSignature(), (Integer) positions.get(method));
}
@Override
public void emitInvoke(CodeEmitter codeEmitter, MethodInfo methodInfo) {
codeEmitter.super_invoke(methodInfo.getSignature());
}
public CodeEmitter beginMethod(ClassEmitter ce, MethodInfo method) {
CodeEmitter e = EmitUtils.begin_method(ce, method);
if (!interceptDuringConstruction && !TypeUtils.isAbstract(method.getModifiers())) {
$Label constructed = e.make_label();
e.load_this();
e.getfield(CONSTRUCTED_FIELD);
e.if_jump(e.NE, constructed);
e.load_this();
e.load_args();
e.super_invoke();
e.return_value();
e.mark(constructed);
}
return e;
}
};
Set<CallbackGenerator> seenGen = new HashSet<>();
for (int i = 0; i < callbackTypes.length + 1; i++) {
CallbackGenerator gen = i == callbackTypes.length ? bridgeMethodGenerator : generators[i];
if (!seenGen.contains(gen)) {
seenGen.add(gen);
final List<MethodInfo> fmethods = groups.get(gen);
if (fmethods != null) {
try {
gen.generate(ce, context, fmethods);
gen.generateStatic(se, context, fmethods);
} catch (RuntimeException x) {
throw x;
} catch (Exception x) {
throw new CodeGenerationException(x);
}
}
}
}
se.return_value();
se.end_method();
}
use of controlP5.Label in project intellij-community by JetBrains.
the class AdvancedEnhancer method emitCurrentCallback.
private static void emitCurrentCallback(CodeEmitter e, int index) {
e.load_this();
e.getfield(getCallbackField(index));
e.dup();
$Label end = e.make_label();
e.ifnonnull(end);
// stack height
e.pop();
e.load_this();
e.invoke_static_this(BIND_CALLBACKS);
e.load_this();
e.getfield(getCallbackField(index));
e.mark(end);
}
Aggregations