Search in sources :

Example 11 with Label

use of org.apache.xbean.asm9.Label in project googleads-java-lib by googleads.

the class DeactivateLabels method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param labelId the ID of the label to deactivate.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long labelId) throws RemoteException {
    // Get the LabelService.
    LabelServiceInterface labelService = adManagerServices.get(session, LabelServiceInterface.class);
    // Create a statement to select a label.
    StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", labelId);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get labels by statement.
        LabelPage page = labelService.getLabelsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (Label label : page.getResults()) {
                System.out.printf("%d) Label with ID %d will be deactivated.%n", i++, label.getId());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of labels to be deactivated: %d%n", totalResultSetSize);
    if (totalResultSetSize > 0) {
        // Remove limit and offset from statement.
        statementBuilder.removeLimitAndOffset();
        // Create action.
        com.google.api.ads.admanager.axis.v202108.DeactivateLabels action = new com.google.api.ads.admanager.axis.v202108.DeactivateLabels();
        // Perform action.
        UpdateResult result = labelService.performLabelAction(action, statementBuilder.toStatement());
        if (result != null && result.getNumChanges() > 0) {
            System.out.printf("Number of labels deactivated: %d%n", result.getNumChanges());
        } else {
            System.out.println("No labels were deactivated.");
        }
    }
}
Also used : Label(com.google.api.ads.admanager.axis.v202108.Label) LabelPage(com.google.api.ads.admanager.axis.v202108.LabelPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) LabelServiceInterface(com.google.api.ads.admanager.axis.v202108.LabelServiceInterface) UpdateResult(com.google.api.ads.admanager.axis.v202108.UpdateResult)

Example 12 with Label

use of org.apache.xbean.asm9.Label in project googleads-java-lib by googleads.

the class GetAllLabels method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    // Get the LabelService.
    LabelServiceInterface labelService = adManagerServices.get(session, LabelServiceInterface.class);
    // Create a statement to select all labels.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get labels by statement.
        LabelPage page = labelService.getLabelsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (Label label : page.getResults()) {
                System.out.printf("%d) Label with ID %d and name '%s' was found.%n", i++, label.getId(), label.getName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : LabelPage(com.google.api.ads.admanager.axis.v202108.LabelPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) LabelServiceInterface(com.google.api.ads.admanager.axis.v202108.LabelServiceInterface) Label(com.google.api.ads.admanager.axis.v202108.Label)

Example 13 with Label

use of org.apache.xbean.asm9.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();
}
Also used : InterceptorHandler(org.talend.sdk.component.api.service.interceptor.InterceptorHandler) Label(org.apache.xbean.asm6.Label) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 14 with Label

use of org.apache.xbean.asm9.Label in project tomee by apache.

the class Cmp2Generator method createOpenEJB_removeCmr.

// private void createPrintln(MethodVisitor mv, String message) {
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn(message);
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
// }
// private void createPrintField(MethodVisitor mv, String fieldName, String descriptor) {
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn(fieldName + "=");
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V");
// 
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitVarInsn(ALOAD, 0);
// mv.visitFieldInsn(GETFIELD, implClassName, fieldName, descriptor);
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V");
// }
/**
 * Emit the remove logic for an individual CMR field.
 * Like the addCmr logic, each field is guarded by an
 * if test on the property name.
 *
 * @param mv
 * @param cmrField
 */
private void createOpenEJB_removeCmr(final MethodVisitor mv, final CmrField cmrField) {
    // if (${cmrField.name}.equals(arg1))
    mv.visitLdcInsn(cmrField.getName());
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if not equal jump to end
    final Label end = new Label();
    mv.visitJumpInsn(IFEQ, end);
    // collection valued CMR field.  Remove the object from the collection.
    if (cmrField.getCmrStyle() != CmrStyle.SINGLE) {
        // ${cmrField.name}.remove(arg2)
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEINTERFACE, cmrField.getCmrStyle().getCollectionType().getInternalName(), "remove", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        // return;
        mv.visitInsn(RETURN);
    } else {
        // single valued, so just null out the field.
        // this.${cmrField.name} = null;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ACONST_NULL);
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return;
        mv.visitInsn(RETURN);
    }
    // end of if statement
    mv.visitLabel(end);
}
Also used : Label(org.apache.xbean.asm9.Label)

Example 15 with Label

use of org.apache.xbean.asm9.Label in project tomee by apache.

the class MakeTxLookup method createNewHibernateStrategy.

private static void createNewHibernateStrategy(final File basedir, final String target, final String abstractJtaPlatformPackage) throws Exception {
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, target.replace('.', '/'), null, abstractJtaPlatformPackage + "/AbstractJtaPlatform", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, abstractJtaPlatformPackage + "/AbstractJtaPlatform", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateTransactionManager", "()Ljavax/transaction/TransactionManager;", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateUserTransaction", "()Ljavax/transaction/UserTransaction;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
        mv.visitLabel(l0);
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/loader/SystemInstance", "get", "()Lorg/apache/openejb/loader/SystemInstance;", false);
        mv.visitLdcInsn(Type.getType("Lorg/apache/openejb/spi/ContainerSystem;"));
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/openejb/loader/SystemInstance", "getComponent", "(Ljava/lang/Class;)Ljava/lang/Object;", false);
        mv.visitTypeInsn(CHECKCAST, "org/apache/openejb/spi/ContainerSystem");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/openejb/spi/ContainerSystem", "getJNDIContext", "()Ljavax/naming/Context;", true);
        mv.visitLdcInsn("comp/UserTransaction");
        mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/Context", "lookup", "(Ljava/lang/String;)Ljava/lang/Object;", true);
        mv.visitTypeInsn(CHECKCAST, "javax/transaction/UserTransaction");
        mv.visitLabel(l1);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "javax/naming/NamingException" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(basedir, cw, target.replace('.', '/'));
}
Also used : Label(org.apache.xbean.asm9.Label) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

Label (org.apache.xbean.asm9.Label)7 Label (com.google.api.ads.admanager.axis.v202108.Label)5 LabelServiceInterface (com.google.api.ads.admanager.axis.v202108.LabelServiceInterface)5 Label (com.google.api.ads.admanager.axis.v202111.Label)5 LabelServiceInterface (com.google.api.ads.admanager.axis.v202111.LabelServiceInterface)5 Label (com.google.api.ads.admanager.axis.v202202.Label)5 LabelServiceInterface (com.google.api.ads.admanager.axis.v202202.LabelServiceInterface)5 MethodVisitor (org.apache.xbean.asm9.MethodVisitor)5 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)4 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)4 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)4 LabelPage (com.google.api.ads.admanager.axis.v202108.LabelPage)4 LabelPage (com.google.api.ads.admanager.axis.v202111.LabelPage)4 LabelPage (com.google.api.ads.admanager.axis.v202202.LabelPage)4 Random (java.util.Random)3 net.sf.cglib.asm.$Label (net.sf.cglib.asm.$Label)3 ByteBuf (io.netty.buffer.ByteBuf)2 Label (jodd.asm5.Label)2 Type1LabelCase (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCase)2 Type1Label (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1Label)2