Search in sources :

Example 26 with Label

use of com.google.api.ads.admanager.axis.v202108.Label in project tomee by apache.

the class Cmp2Generator method createOpenEJB_removeCmr.

private void createOpenEJB_removeCmr() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_removeCmr", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);
    mv.visitCode();
    // if (deleted) return;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implClassName, DELETED, "Z");
    final Label notDeleted = new Label();
    mv.visitJumpInsn(IFEQ, notDeleted);
    mv.visitInsn(RETURN);
    mv.visitLabel(notDeleted);
    for (final CmrField cmrField : cmrFields) {
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name}.remove(value);
        // return;
        // }
        // 
        // OR
        // 
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name} = null;
        // return;
        // }
        createOpenEJB_removeCmr(mv, cmrField);
    }
    // throw new IllegalArgumentException("Unknown cmr field " + name + " on entity bean of type " + getClass().getName());
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitLdcInsn("Unknown cmr field ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitLdcInsn(" on entity bean of type ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.apache.xbean.asm9.Label) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 27 with Label

use of com.google.api.ads.admanager.axis.v202108.Label in project tomee by apache.

the class Cmp2Generator method createOpenEJB_addCmr.

/**
 * Generate the OpenEJB_addCmr logic for an individual
 * CMR field.  Each CMR field has a test against the
 * property name, which is passed to the wrappering
 * addCmr method.  This results in a series of
 * if blocks for each defined CMD property.
 *
 * @param mv       The method we're generating within.
 * @param cmrField The CMR field definition.
 */
private void createOpenEJB_addCmr(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 already anchored in the CMR field.
    if (cmrField.getCmrStyle() != CmrStyle.SINGLE) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        final Label fieldNotNull = new Label();
        mv.visitJumpInsn(IFNONNULL, fieldNotNull);
        // lazy creation of the collection type if not already created.
        mv.visitTypeInsn(NEW, cmrField.getInitialValueType().getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, cmrField.getInitialValueType().getInternalName(), "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 3);
        mv.visitLabel(fieldNotNull);
        // ${cmrField.name}.add(arg2)
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEINTERFACE, cmrField.getCmrStyle().getCollectionType().getInternalName(), "add", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 3);
        // unconditionally set the CMR field to the collection.  This is either the
        // original one on entry, or a new one for first access.
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return null;
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
    } else {
        // push: this.${cmrField.name};
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // this.${cmrField.name} = (${cmrField.type}) bean;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitTypeInsn(CHECKCAST, cmrField.getType().getInternalName());
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return pushed value above
        mv.visitInsn(ARETURN);
    }
    // end of if statement
    mv.visitLabel(end);
}
Also used : Label(org.apache.xbean.asm9.Label)

Example 28 with Label

use of com.google.api.ads.admanager.axis.v202108.Label in project tomee by apache.

the class LocalBeanProxyFactory method visit.

public static MethodVisitor visit(final ClassWriter cw, final Method method, final String proxyName, final String handlerName) throws ProxyGenerationException {
    final Class<?> returnType = method.getReturnType();
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final Class<?>[] exceptionTypes = method.getExceptionTypes();
    final int modifiers = method.getModifiers();
    // push the method definition
    int modifier = 0;
    if (Modifier.isPublic(modifiers)) {
        modifier = ACC_PUBLIC;
    } else if (Modifier.isProtected(modifiers)) {
        modifier = ACC_PROTECTED;
    }
    final MethodVisitor mv = cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), 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);
        final Class<?> parameterType = parameterTypes[i];
        // push number onto stack
        pushIntOntoStack(mv, i);
        if (parameterType.isPrimitive()) {
            final String wrapperType = getWrapperType(parameterType);
            mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
        } else {
            mv.visitLdcInsn(Type.getType(getAsmTypeAsString(parameterType, true)));
        }
        mv.visitInsn(AASTORE);
        if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
            length += 2;
        } else {
            length++;
        }
    }
    // invoke getMethod() with the method name and the array of types
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
    // store the returned method for later
    mv.visitVarInsn(ASTORE, length);
    // the following code generates bytecode equivalent to:
    // return ((<returntype>) invocationHandler.invoke(this, method, 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, proxyName, handlerName, "Ljava/lang/reflect/InvocationHandler;");
    // we want to pass "this" in as the first parameter
    mv.visitVarInsn(ALOAD, 0);
    // and the method we fetched earlier
    mv.visitVarInsn(ALOAD, length);
    // need to construct the array of objects passed in
    // create the Object[]
    createArrayDefinition(mv, parameterTypes.length, Object.class);
    int index = 1;
    // push parameters into array
    for (int i = 0; i < parameterTypes.length; i++) {
        // keep copy of array on stack
        mv.visitInsn(DUP);
        final Class<?> parameterType = parameterTypes[i];
        // push number onto stack
        pushIntOntoStack(mv, i);
        if (parameterType.isPrimitive()) {
            final String wrapperType = getWrapperType(parameterType);
            mv.visitVarInsn(getVarInsn(parameterType), index);
            mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf", "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";", false);
            mv.visitInsn(AASTORE);
            if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
                index += 2;
            } else {
                index++;
            }
        } else {
            mv.visitVarInsn(ALOAD, index);
            mv.visitInsn(AASTORE);
            index++;
        }
    }
    // invoke the invocationHandler
    mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/reflect/InvocationHandler", "invoke", "(Ljava/lang/Object;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), "()" + getPrimitiveLetter(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);
        final Label l5 = new Label();
        mv.visitLabel(l5);
        for (int i = 0; i < exceptionTypes.length; i++) {
            final Class<?> exceptionType = exceptionTypes[i];
            mv.visitLdcInsn(Type.getType("L" + exceptionType.getName().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, exceptionType.getName().replace('.', '/'));
            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);
    return mv;
}
Also used : Label(org.apache.xbean.asm9.Label) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 29 with Label

use of com.google.api.ads.admanager.axis.v202108.Label in project googleads-java-lib by googleads.

the class DeactivateCreativeWrappersForLabel method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param labelId the ID of the creative wrapper 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 CreativeWrapperService.
    CreativeWrapperServiceInterface creativeWrapperService = adManagerServices.get(session, CreativeWrapperServiceInterface.class);
    // Create a statement to select the active creative wrappers for the
    // given label.
    StatementBuilder statementBuilder = new StatementBuilder().where("WHERE status = :status AND labelId = :labelId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("status", CreativeWrapperStatus.ACTIVE.toString()).withBindVariableValue("labelId", labelId);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get creative wrappers by statement.
        CreativeWrapperPage page = creativeWrapperService.getCreativeWrappersByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (CreativeWrapper creativeWrapper : page.getResults()) {
                System.out.printf("%d) Creative wrapper with ID %d applying to label ID" + " %d will be deactivated.%n", i++, creativeWrapper.getId(), creativeWrapper.getLabelId());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of creative wrappers to be deactivated: %d%n", totalResultSetSize);
    if (totalResultSetSize > 0) {
        // Remove limit and offset from statement.
        statementBuilder.removeLimitAndOffset();
        // Create action.
        DeactivateCreativeWrappers action = new DeactivateCreativeWrappers();
        // Perform action.
        UpdateResult result = creativeWrapperService.performCreativeWrapperAction(action, statementBuilder.toStatement());
        if (result != null && result.getNumChanges() > 0) {
            System.out.printf("Number of creative wrappers deactivated: %d%n", result.getNumChanges());
        } else {
            System.out.println("No creative wrappers were deactivated.");
        }
    }
}
Also used : CreativeWrapper(com.google.api.ads.admanager.axis.v202108.CreativeWrapper) CreativeWrapperPage(com.google.api.ads.admanager.axis.v202108.CreativeWrapperPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) DeactivateCreativeWrappers(com.google.api.ads.admanager.axis.v202108.DeactivateCreativeWrappers) CreativeWrapperServiceInterface(com.google.api.ads.admanager.axis.v202108.CreativeWrapperServiceInterface) UpdateResult(com.google.api.ads.admanager.axis.v202108.UpdateResult)

Example 30 with Label

use of com.google.api.ads.admanager.axis.v202108.Label in project googleads-java-lib by googleads.

the class GetAllCreativeWrappers 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 CreativeWrapperService.
    CreativeWrapperServiceInterface creativeWrapperService = adManagerServices.get(session, CreativeWrapperServiceInterface.class);
    // Create a statement to select all creative wrappers.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get creative wrappers by statement.
        CreativeWrapperPage page = creativeWrapperService.getCreativeWrappersByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (CreativeWrapper creativeWrapper : page.getResults()) {
                System.out.printf("%d) Creative wrapper with ID %d applying to label ID %d was found.%n", i++, creativeWrapper.getId(), creativeWrapper.getLabelId());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : CreativeWrapper(com.google.api.ads.admanager.axis.v202108.CreativeWrapper) CreativeWrapperPage(com.google.api.ads.admanager.axis.v202108.CreativeWrapperPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) CreativeWrapperServiceInterface(com.google.api.ads.admanager.axis.v202108.CreativeWrapperServiceInterface)

Aggregations

StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)7 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.v202111.StatementBuilder)4 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)4 CreativeWrapper (com.google.api.ads.admanager.axis.v202108.CreativeWrapper)4 CreativeWrapperServiceInterface (com.google.api.ads.admanager.axis.v202108.CreativeWrapperServiceInterface)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 CreativeWrapperPage (com.google.api.ads.admanager.axis.v202108.CreativeWrapperPage)3 Random (java.util.Random)3 net.sf.cglib.asm.$Label (net.sf.cglib.asm.$Label)3 UpdateResult (com.google.api.ads.admanager.axis.v202108.UpdateResult)2