use of com.googlecode.lanterna.gui2.Label in project openwebbeans by apache.
the class InterceptorDecoratorProxyFactory method generateInterceptorHandledMethod.
private void generateInterceptorHandledMethod(ClassWriter cw, Method method, int methodIndex, Class<?> classToProxy, String proxyClassFileName) throws ProxyGenerationException {
if ("<init>".equals(method.getName())) {
return;
}
if (isIgnoredMethod(method)) {
return;
}
Class<?> returnType = method.getReturnType();
Class<?>[] parameterTypes = method.getParameterTypes();
Class<?>[] exceptionTypes = method.getExceptionTypes();
int modifiers = method.getModifiers();
if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
throw new WebBeansConfigurationException("It's not possible to proxy a final or static method: " + classToProxy.getName() + " " + method.getName());
}
// push the method definition
int modifier = modifiers & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.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
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
if (exceptionTypes.length > 0) {
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
}
// push try code
mv.visitLabel(l0);
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(Opcodes.DUP);
Class<?> parameterType = parameterTypes[i];
// push number onto stack
pushIntOntoStack(mv, i);
if (parameterType.isPrimitive()) {
String wrapperType = getWrapperType(parameterType);
mv.visitFieldInsn(Opcodes.GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
} else {
mv.visitLdcInsn(Type.getType(parameterType));
}
mv.visitInsn(Opcodes.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()];
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitVarInsn(Opcodes.ALOAD, 0);
// get the invocationHandler field from this class
mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
// add the Method from the static array as first parameter
mv.visitFieldInsn(Opcodes.GETSTATIC, proxyClassFileName, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class));
// push the methodIndex of the current method
if (methodIndex < 128) {
mv.visitIntInsn(Opcodes.BIPUSH, methodIndex);
} else if (methodIndex < 32267) {
// for methods > 127 we need to push a short number as index
mv.visitIntInsn(Opcodes.SIPUSH, methodIndex);
} else {
throw new ProxyGenerationException("Sorry, we only support Classes with 2^15 methods...");
}
// and now load the Method from the array
mv.visitInsn(Opcodes.AALOAD);
// prepare the parameter array as Object[] and store it on the stack
pushMethodParameterArray(mv, parameterTypes);
// invoke the invocationHandler
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke", "(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true);
// cast the result
mv.visitTypeInsn(Opcodes.CHECKCAST, getCastType(returnType));
if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType))) {
// get the primitive value
mv.visitMethodInsn(Opcodes.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(Opcodes.POP);
mv.visitInsn(Opcodes.RETURN);
}
// catch InvocationTargetException
if (exceptionTypes.length > 0) {
mv.visitLabel(l2);
mv.visitVarInsn(Opcodes.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(Opcodes.ALOAD, length);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
Label l6 = new Label();
mv.visitJumpInsn(Opcodes.IFEQ, l6);
Label l7 = new Label();
mv.visitLabel(l7);
mv.visitVarInsn(Opcodes.ALOAD, length);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitTypeInsn(Opcodes.CHECKCAST, getCastType(exceptionType));
mv.visitInsn(Opcodes.ATHROW);
mv.visitLabel(l6);
if (i == (exceptionTypes.length - 1)) {
mv.visitTypeInsn(Opcodes.NEW, "java/lang/reflect/UndeclaredThrowableException");
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, length);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>", "(Ljava/lang/Throwable;)V", false);
mv.visitInsn(Opcodes.ATHROW);
}
}
}
// finish this method
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of com.googlecode.lanterna.gui2.Label in project jbang-catalog by quintesse.
the class lanterna method main.
public static void main(String[] args) {
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory().setMouseCaptureMode(MouseCaptureMode.CLICK_RELEASE);
try (Screen screen = terminalFactory.createScreen()) {
screen.startScreen();
List<String> timezonesAsStrings = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
final Window window = new BasicWindow("My Root Window");
Panel contentPanel = new Panel().setLayoutManager(new GridLayout(2).setHorizontalSpacing(3)).addComponent(new Label("This is a label that spans two columns").setLayoutData(GridLayout.createLayoutData(// Horizontal alignment in the grid cell if the cell
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell
GridLayout.Alignment.BEGINNING, // Give the component extra horizontal space if available
true, // Give the component extra vertical space if available
false, // Horizontal span
2, // Vertical span
1))).addComponent(new Label("Text Box (aligned)")).addComponent(new TextBox().setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.BEGINNING, GridLayout.Alignment.CENTER))).addComponent(new Label("Password Box (right aligned)")).addComponent(new TextBox().setMask('*').setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER))).addComponent(new Label("Read-only Combo Box (forced size)")).addComponent(new ComboBox<>(timezonesAsStrings).setReadOnly(true).setPreferredSize(new TerminalSize(20, 1))).addComponent(new Label("Editable Combo Box (filled)")).addComponent(new ComboBox<>("Item #1", "Item #2", "Item #3", "Item #4").setReadOnly(false).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(1))).addComponent(new Label("Button (centered)")).addComponent(new Button("Button", () -> MessageDialog.showMessageDialog(textGUI, "MessageBox", "This is a message box", MessageDialogButton.OK)).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER))).addComponent(new EmptySpace().setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2))).addComponent(new Separator(Direction.HORIZONTAL).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2))).addComponent(new Button("Close", window::close).setLayoutData(GridLayout.createHorizontallyEndAlignedLayoutData(2)));
window.setComponent(contentPanel);
window.addWindowListener(new WindowListenerAdapter() {
public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) {
if (keyStroke.getKeyType() == KeyType.Escape) {
window.close();
}
}
});
textGUI.addWindowAndWait(window);
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.googlecode.lanterna.gui2.Label in project skywalking-java by apache.
the class HistogramTest method testTransform.
@Test
public void testTransform() {
final List<Label> labels = Arrays.asList(Label.newBuilder().setName("k1").setValue("v1").build());
// Check histogram message
final Histogram histogram = MeterFactory.histogram("test").steps(Arrays.asList(2d, 5d)).minValue(1d).tag("k1", "v1").build();
histogram.addValue(1);
histogram.addValue(3);
histogram.addValue(3);
histogram.addValue(7);
verifyHistogram("test", labels, Arrays.asList(1d, 2d, 5d), Arrays.asList(1L, 2L, 1L), histogram.transform());
histogram.addValue(9);
verifyHistogram("test", labels, Arrays.asList(1d, 2d, 5d), Arrays.asList(1L, 2L, 2L), histogram.transform());
}
use of com.googlecode.lanterna.gui2.Label in project security-lib by ncsa.
the class GUIDemo method helloWorld.
public static void helloWorld(Screen screen) {
// Create panel to hold components
Panel panel = new Panel();
panel.setLayoutManager(new GridLayout(2));
panel.addComponent(new Label("Forename"));
panel.addComponent(new TextBox());
panel.addComponent(new Label("Surname"));
panel.addComponent(new TextBox());
// Empty space underneath labels
panel.addComponent(new EmptySpace(new TerminalSize(0, 0)));
panel.addComponent(new Button("Submit"));
// Create window to hold the panel
BasicWindow window = new BasicWindow();
window.setComponent(panel);
// Create gui and start gui
MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
gui.addWindowAndWait(window);
}
use of com.googlecode.lanterna.gui2.Label in project jodd by oblac.
the class ProxettaAsmUtil method visitReturn.
// ---------------------------------------------------------------- return
/**
* Visits return opcodes.
*/
public static void visitReturn(MethodVisitor mv, MethodInfo methodInfo, boolean isLast) {
switch(methodInfo.getReturnOpcodeType()) {
case 'V':
if (isLast) {
mv.visitInsn(POP);
}
mv.visitInsn(RETURN);
break;
case 'B':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(label);
AsmUtil.byteValue(mv);
}
mv.visitInsn(IRETURN);
break;
case 'C':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(label);
AsmUtil.charValue(mv);
}
mv.visitInsn(IRETURN);
break;
case 'S':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(label);
AsmUtil.shortValue(mv);
}
mv.visitInsn(IRETURN);
break;
case 'I':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(label);
AsmUtil.intValue(mv);
}
mv.visitInsn(IRETURN);
break;
case 'Z':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(label);
AsmUtil.booleanValue(mv);
}
mv.visitInsn(IRETURN);
break;
case 'J':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(LCONST_0);
mv.visitInsn(LRETURN);
mv.visitLabel(label);
AsmUtil.longValue(mv);
}
mv.visitInsn(LRETURN);
break;
case 'F':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(FCONST_0);
mv.visitInsn(FRETURN);
mv.visitLabel(label);
AsmUtil.floatValue(mv);
}
mv.visitInsn(FRETURN);
break;
case 'D':
if (isLast) {
mv.visitInsn(DUP);
Label label = new Label();
mv.visitJumpInsn(IFNONNULL, label);
mv.visitInsn(POP);
mv.visitInsn(DCONST_0);
mv.visitInsn(DRETURN);
mv.visitLabel(label);
AsmUtil.doubleValue(mv);
}
mv.visitInsn(DRETURN);
break;
default:
mv.visitInsn(ARETURN);
break;
}
}
Aggregations