Search in sources :

Example 1 with BranchResult

use of io.quarkus.gizmo.BranchResult in project quarkus by quarkusio.

the class StockMethodsAdder method generateSaveAll.

private void generateSaveAll(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor, String generatedClassName, DotName entityDotName, String entityTypeStr, Map<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {
    MethodDescriptor saveAllDescriptor = MethodDescriptor.ofMethod(generatedClassName, "saveAll", List.class, Iterable.class);
    MethodDescriptor bridgeSaveAllDescriptor = MethodDescriptor.ofMethod(generatedClassName, "saveAll", Iterable.class, Iterable.class);
    if (allMethodsToBeImplementedToResult.containsKey(saveAllDescriptor) || allMethodsToBeImplementedToResult.containsKey(bridgeSaveAllDescriptor)) {
        if (!classCreator.getExistingMethods().contains(saveAllDescriptor)) {
            MethodDescriptor save = MethodDescriptor.ofMethod(generatedClassName, "save", entityTypeStr, entityTypeStr);
            try (MethodCreator saveAll = classCreator.getMethodCreator(saveAllDescriptor)) {
                saveAll.setSignature(String.format("<S:L%s;>(Ljava/lang/Iterable<TS;>;)Ljava/util/List<TS;>;", entityTypeStr.replace('.', '/')));
                saveAll.addAnnotation(Transactional.class);
                ResultHandle iterable = saveAll.getMethodParam(0);
                ResultHandle resultList = saveAll.newInstance(MethodDescriptor.ofConstructor(ArrayList.class));
                ResultHandle iterator = saveAll.invokeInterfaceMethod(ofMethod(Iterable.class, "iterator", Iterator.class), iterable);
                BytecodeCreator loop = saveAll.createScope();
                ResultHandle hasNextValue = loop.invokeInterfaceMethod(ofMethod(Iterator.class, "hasNext", boolean.class), iterator);
                BranchResult hasNextBranch = loop.ifNonZero(hasNextValue);
                BytecodeCreator hasNext = hasNextBranch.trueBranch();
                BytecodeCreator doesNotHaveNext = hasNextBranch.falseBranch();
                ResultHandle next = hasNext.invokeInterfaceMethod(ofMethod(Iterator.class, "next", Object.class), iterator);
                ResultHandle saveResult = hasNext.invokeVirtualMethod(save, hasNext.getThis(), next);
                hasNext.invokeInterfaceMethod(MethodDescriptor.ofMethod(List.class, "add", boolean.class, Object.class), resultList, saveResult);
                hasNext.continueScope(loop);
                doesNotHaveNext.breakScope(loop);
                saveAll.returnValue(resultList);
            }
            try (MethodCreator bridgeSaveAll = classCreator.getMethodCreator(bridgeSaveAllDescriptor)) {
                MethodDescriptor saveAll = MethodDescriptor.ofMethod(generatedClassName, "saveAll", List.class.getName(), Iterable.class);
                ResultHandle result = bridgeSaveAll.invokeVirtualMethod(saveAll, bridgeSaveAll.getThis(), bridgeSaveAll.getMethodParam(0));
                bridgeSaveAll.returnValue(result);
            }
        }
        allMethodsToBeImplementedToResult.put(saveAllDescriptor, true);
        allMethodsToBeImplementedToResult.put(bridgeSaveAllDescriptor, true);
    }
}
Also used : BranchResult(io.quarkus.gizmo.BranchResult) MethodCreator(io.quarkus.gizmo.MethodCreator) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) List(java.util.List) ArrayList(java.util.ArrayList) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor)

Example 2 with BranchResult

use of io.quarkus.gizmo.BranchResult in project quarkus by quarkusio.

the class StockMethodsAdder method generateDeleteById.

private void generateDeleteById(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor, String generatedClassName, String entityTypeStr, String idTypeStr, Map<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {
    MethodDescriptor deleteByIdDescriptor = MethodDescriptor.ofMethod(generatedClassName, "deleteById", void.class.getName(), idTypeStr);
    MethodDescriptor bridgeDeleteByIdDescriptor = MethodDescriptor.ofMethod(generatedClassName, "deleteById", void.class, Object.class);
    if (allMethodsToBeImplementedToResult.containsKey(deleteByIdDescriptor) || allMethodsToBeImplementedToResult.containsKey(bridgeDeleteByIdDescriptor)) {
        if (!classCreator.getExistingMethods().contains(deleteByIdDescriptor)) {
            try (MethodCreator deleteById = classCreator.getMethodCreator(deleteByIdDescriptor)) {
                deleteById.addAnnotation(Transactional.class);
                ResultHandle id = deleteById.getMethodParam(0);
                ResultHandle entityClass = deleteById.readInstanceField(entityClassFieldDescriptor, deleteById.getThis());
                ResultHandle deleted = deleteById.invokeVirtualMethod(MethodDescriptor.ofMethod(AbstractJpaOperations.class, "deleteById", boolean.class, Class.class, Object.class), deleteById.readStaticField(operationsField), entityClass, id);
                BranchResult deletedBranch = deleteById.ifNonZero(deleted);
                BytecodeCreator deletedFalse = deletedBranch.falseBranch();
                ResultHandle idToString = deletedFalse.invokeVirtualMethod(ofMethod(Object.class, "toString", String.class), id);
                ResultHandle formatArgsArray = deletedFalse.newArray(Object.class, 1);
                deletedFalse.writeArrayValue(formatArgsArray, deletedFalse.load(0), idToString);
                ResultHandle messageFormat = deletedFalse.load("No entity " + entityTypeStr + " with id %s exists");
                ResultHandle message = deletedFalse.invokeStaticMethod(MethodDescriptor.ofMethod(String.class, "format", String.class, String.class, Object[].class), messageFormat, formatArgsArray);
                ResultHandle exception = deletedFalse.newInstance(MethodDescriptor.ofConstructor(IllegalArgumentException.class, String.class), message);
                deletedFalse.throwException(exception);
                deletedFalse.breakScope();
                deleteById.returnValue(null);
            }
            try (MethodCreator bridgeDeleteById = classCreator.getMethodCreator(bridgeDeleteByIdDescriptor)) {
                MethodDescriptor deleteById = MethodDescriptor.ofMethod(generatedClassName, "deleteById", void.class, idTypeStr);
                ResultHandle methodParam = bridgeDeleteById.getMethodParam(0);
                ResultHandle castedMethodParam = bridgeDeleteById.checkCast(methodParam, idTypeStr);
                ResultHandle result = bridgeDeleteById.invokeVirtualMethod(deleteById, bridgeDeleteById.getThis(), castedMethodParam);
                bridgeDeleteById.returnValue(result);
            }
        }
        allMethodsToBeImplementedToResult.put(deleteByIdDescriptor, true);
        allMethodsToBeImplementedToResult.put(bridgeDeleteByIdDescriptor, true);
    }
}
Also used : BranchResult(io.quarkus.gizmo.BranchResult) MethodCreator(io.quarkus.gizmo.MethodCreator) AbstractJpaOperations(io.quarkus.hibernate.orm.panache.common.runtime.AbstractJpaOperations) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor)

Example 3 with BranchResult

use of io.quarkus.gizmo.BranchResult in project quarkus by quarkusio.

the class StockMethodsAdder method generateFindAllWithPageable.

private void generateFindAllWithPageable(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor, String generatedClassName, String entityTypeStr, Map<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {
    MethodDescriptor findAllDescriptor = MethodDescriptor.ofMethod(generatedClassName, "findAll", Page.class, Pageable.class);
    if (allMethodsToBeImplementedToResult.containsKey(findAllDescriptor)) {
        if (!classCreator.getExistingMethods().contains(findAllDescriptor)) {
            try (MethodCreator findAll = classCreator.getMethodCreator(findAllDescriptor)) {
                findAll.setSignature(String.format("(Lorg/springframework/data/domain/Pageable;)Lorg/springframework/data/domain/Page<L%s;>;", entityTypeStr.replace('.', '/')));
                ResultHandle pageable = findAll.getMethodParam(0);
                ResultHandle pageableSort = findAll.invokeInterfaceMethod(MethodDescriptor.ofMethod(Pageable.class, "getSort", Sort.class), pageable);
                ResultHandle panachePage = findAll.invokeStaticMethod(MethodDescriptor.ofMethod(TypesConverter.class, "toPanachePage", io.quarkus.panache.common.Page.class, Pageable.class), pageable);
                ResultHandle panacheSort = findAll.invokeStaticMethod(MethodDescriptor.ofMethod(TypesConverter.class, "toPanacheSort", io.quarkus.panache.common.Sort.class, org.springframework.data.domain.Sort.class), pageableSort);
                // depending on whether there was a io.quarkus.panache.common.Sort returned, we need to execute a different findAll method
                BranchResult sortNullBranch = findAll.ifNull(panacheSort);
                BytecodeCreator sortNullTrue = sortNullBranch.trueBranch();
                BytecodeCreator sortNullFalse = sortNullBranch.falseBranch();
                AssignableResultHandle panacheQueryVar = findAll.createVariable(PanacheQuery.class);
                ResultHandle panacheQueryWithoutSort = sortNullTrue.invokeVirtualMethod(ofMethod(AbstractJpaOperations.class, "findAll", Object.class, Class.class), sortNullTrue.readStaticField(operationsField), sortNullTrue.readInstanceField(entityClassFieldDescriptor, sortNullTrue.getThis()));
                sortNullTrue.assign(panacheQueryVar, panacheQueryWithoutSort);
                sortNullTrue.breakScope();
                ResultHandle panacheQueryWithSort = sortNullFalse.invokeVirtualMethod(ofMethod(AbstractJpaOperations.class, "findAll", Object.class, Class.class, io.quarkus.panache.common.Sort.class), sortNullFalse.readStaticField(operationsField), sortNullFalse.readInstanceField(entityClassFieldDescriptor, sortNullFalse.getThis()), panacheSort);
                sortNullFalse.assign(panacheQueryVar, panacheQueryWithSort);
                sortNullFalse.breakScope();
                ResultHandle panacheQuery = findAll.invokeInterfaceMethod(MethodDescriptor.ofMethod(PanacheQuery.class, "page", PanacheQuery.class, io.quarkus.panache.common.Page.class), panacheQueryVar, panachePage);
                ResultHandle list = findAll.invokeInterfaceMethod(MethodDescriptor.ofMethod(PanacheQuery.class, "list", List.class), panacheQuery);
                ResultHandle count = findAll.invokeInterfaceMethod(MethodDescriptor.ofMethod(PanacheQuery.class, "count", long.class), panacheQuery);
                ResultHandle pageResult = findAll.newInstance(MethodDescriptor.ofConstructor(PageImpl.class, List.class, Pageable.class, long.class), list, findAll.getMethodParam(0), count);
                findAll.returnValue(pageResult);
            }
        }
        allMethodsToBeImplementedToResult.put(findAllDescriptor, true);
    }
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) AbstractJpaOperations(io.quarkus.hibernate.orm.panache.common.runtime.AbstractJpaOperations) PanacheQuery(io.quarkus.hibernate.orm.panache.PanacheQuery) Page(org.springframework.data.domain.Page) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor) BranchResult(io.quarkus.gizmo.BranchResult) Pageable(org.springframework.data.domain.Pageable) MethodCreator(io.quarkus.gizmo.MethodCreator) Sort(org.springframework.data.domain.Sort) TypesConverter(io.quarkus.spring.data.runtime.TypesConverter) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with BranchResult

use of io.quarkus.gizmo.BranchResult in project quarkus by quarkusio.

the class StockMethodsAdder method generateSave.

private void generateSave(ClassCreator classCreator, String generatedClassName, DotName entityDotName, String entityTypeStr, Map<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {
    MethodDescriptor saveDescriptor = MethodDescriptor.ofMethod(generatedClassName, "save", entityTypeStr, entityTypeStr);
    MethodDescriptor bridgeSaveDescriptor = MethodDescriptor.ofMethod(generatedClassName, "save", Object.class, Object.class);
    if (allMethodsToBeImplementedToResult.containsKey(saveDescriptor) || allMethodsToBeImplementedToResult.containsKey(bridgeSaveDescriptor)) {
        if (!classCreator.getExistingMethods().contains(saveDescriptor)) {
            try (MethodCreator save = classCreator.getMethodCreator(saveDescriptor)) {
                save.addAnnotation(Transactional.class);
                ResultHandle entity = save.getMethodParam(0);
                // if an entity is Persistable, then all we need to do is call isNew to determine if it's new or not
                if (isPersistable(entityDotName)) {
                    ResultHandle isNew = save.invokeVirtualMethod(ofMethod(entityDotName.toString(), "isNew", boolean.class.toString()), entity);
                    BranchResult isNewBranch = save.ifTrue(isNew);
                    generatePersistAndReturn(entity, isNewBranch.trueBranch());
                    generateMergeAndReturn(entity, isNewBranch.falseBranch());
                } else {
                    AnnotationTarget idAnnotationTarget = getIdAnnotationTarget(entityDotName, index);
                    ResultHandle idValue = generateObtainValue(save, entityDotName, entity, idAnnotationTarget);
                    Type idType = getTypeOfTarget(idAnnotationTarget);
                    Optional<AnnotationTarget> versionValueTarget = getVersionAnnotationTarget(entityDotName, index);
                    if (versionValueTarget.isPresent()) {
                        Type versionType = getTypeOfTarget(versionValueTarget.get());
                        if (versionType instanceof PrimitiveType) {
                            throw new IllegalArgumentException("The '@Version' annotation cannot be used on primitive types. Offending entity is '" + entityDotName + "'.");
                        }
                        ResultHandle versionValue = generateObtainValue(save, entityDotName, entity, versionValueTarget.get());
                        BranchResult versionValueIsNullBranch = save.ifNull(versionValue);
                        generatePersistAndReturn(entity, versionValueIsNullBranch.trueBranch());
                        generateMergeAndReturn(entity, versionValueIsNullBranch.falseBranch());
                    }
                    BytecodeCreator idValueUnset;
                    BytecodeCreator idValueSet;
                    if (idType instanceof PrimitiveType) {
                        if (!idType.name().equals(DotNames.PRIMITIVE_LONG) && !idType.name().equals(DotNames.PRIMITIVE_INTEGER)) {
                            throw new IllegalArgumentException("Id type of '" + entityDotName + "' is invalid.");
                        }
                        if (idType.name().equals(DotNames.PRIMITIVE_LONG)) {
                            ResultHandle longObject = save.invokeStaticMethod(MethodDescriptor.ofMethod(Long.class, "valueOf", Long.class, long.class), idValue);
                            idValue = save.invokeVirtualMethod(MethodDescriptor.ofMethod(Long.class, "intValue", int.class), longObject);
                        }
                        BranchResult idValueNonZeroBranch = save.ifNonZero(idValue);
                        idValueSet = idValueNonZeroBranch.trueBranch();
                        idValueUnset = idValueNonZeroBranch.falseBranch();
                    } else {
                        BranchResult idValueNullBranch = save.ifNull(idValue);
                        idValueSet = idValueNullBranch.falseBranch();
                        idValueUnset = idValueNullBranch.trueBranch();
                    }
                    generatePersistAndReturn(entity, idValueUnset);
                    generateMergeAndReturn(entity, idValueSet);
                }
            }
            try (MethodCreator bridgeSave = classCreator.getMethodCreator(bridgeSaveDescriptor)) {
                MethodDescriptor save = MethodDescriptor.ofMethod(generatedClassName, "save", entityTypeStr, entityTypeStr);
                ResultHandle methodParam = bridgeSave.getMethodParam(0);
                ResultHandle castedMethodParam = bridgeSave.checkCast(methodParam, entityTypeStr);
                ResultHandle result = bridgeSave.invokeVirtualMethod(save, bridgeSave.getThis(), castedMethodParam);
                bridgeSave.returnValue(result);
            }
        }
        allMethodsToBeImplementedToResult.put(saveDescriptor, true);
        allMethodsToBeImplementedToResult.put(bridgeSaveDescriptor, true);
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) BranchResult(io.quarkus.gizmo.BranchResult) ParameterizedType(org.jboss.jandex.ParameterizedType) ClassType(org.jboss.jandex.ClassType) Type(org.jboss.jandex.Type) PrimitiveType(org.jboss.jandex.PrimitiveType) MethodCreator(io.quarkus.gizmo.MethodCreator) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) PrimitiveType(org.jboss.jandex.PrimitiveType) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor)

Example 5 with BranchResult

use of io.quarkus.gizmo.BranchResult in project quarkus by quarkusio.

the class PagingAndSortingMethodsImplementor method setOrder.

private void setOrder(BytecodeCreator creator, ResultHandle orderArray, ResultHandle column) {
    ResultHandle columnName = creator.invokeVirtualMethod(ofMethod(PANACHE_COLUMN, "getName", String.class), column);
    ResultHandle direction = creator.invokeVirtualMethod(ofMethod(PANACHE_COLUMN, "getDirection", PANACHE_DIRECTION), column);
    BranchResult isAscendingBranch = isAscending(creator, direction);
    isAscendingBranch.trueBranch().writeArrayValue(orderArray, 0, isAscendingBranch.trueBranch().invokeStaticMethod(ofMethod(Sort.Order.class, "asc", Sort.Order.class, String.class), columnName));
    isAscendingBranch.falseBranch().writeArrayValue(orderArray, 0, isAscendingBranch.falseBranch().invokeStaticMethod(ofMethod(Sort.Order.class, "desc", Sort.Order.class, String.class), columnName));
}
Also used : BranchResult(io.quarkus.gizmo.BranchResult) Sort(org.springframework.data.domain.Sort) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) ResultHandle(io.quarkus.gizmo.ResultHandle)

Aggregations

BranchResult (io.quarkus.gizmo.BranchResult)43 ResultHandle (io.quarkus.gizmo.ResultHandle)39 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)28 BytecodeCreator (io.quarkus.gizmo.BytecodeCreator)27 MethodCreator (io.quarkus.gizmo.MethodCreator)22 TryBlock (io.quarkus.gizmo.TryBlock)10 ClassCreator (io.quarkus.gizmo.ClassCreator)9 FunctionCreator (io.quarkus.gizmo.FunctionCreator)9 List (java.util.List)9 Type (org.jboss.jandex.Type)8 FieldDescriptor (io.quarkus.gizmo.FieldDescriptor)6 MethodDescriptor (io.quarkus.gizmo.MethodDescriptor)5 MethodDescriptor.ofMethod (io.quarkus.gizmo.MethodDescriptor.ofMethod)5 Supplier (java.util.function.Supplier)5 CatchBlockCreator (io.quarkus.gizmo.CatchBlockCreator)4 Sort (io.quarkus.panache.common.Sort)4 RestDataResource (io.quarkus.rest.data.panache.RestDataResource)4 ResourceMetadata (io.quarkus.rest.data.panache.deployment.ResourceMetadata)4 ResourceProperties (io.quarkus.rest.data.panache.deployment.properties.ResourceProperties)4 ResponseImplementor (io.quarkus.rest.data.panache.deployment.utils.ResponseImplementor)4