|
| 1 | +package io.quarkus.panache.mock.impl; |
| 2 | + |
| 3 | +import java.lang.reflect.Modifier; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.function.BiFunction; |
| 9 | + |
| 10 | +import org.jboss.jandex.AnnotationInstance; |
| 11 | +import org.jboss.jandex.ClassInfo; |
| 12 | +import org.jboss.jandex.DotName; |
| 13 | +import org.jboss.jandex.MethodInfo; |
| 14 | +import org.objectweb.asm.ClassVisitor; |
| 15 | +import org.objectweb.asm.Opcodes; |
| 16 | + |
| 17 | +import io.quarkus.arc.processor.BytecodeTransformer; |
| 18 | +import io.quarkus.gizmo.BytecodeCreator; |
| 19 | +import io.quarkus.gizmo.CatchBlockCreator; |
| 20 | +import io.quarkus.gizmo.ClassTransformer; |
| 21 | +import io.quarkus.gizmo.FieldDescriptor; |
| 22 | +import io.quarkus.gizmo.MethodCreator; |
| 23 | +import io.quarkus.gizmo.MethodDescriptor; |
| 24 | +import io.quarkus.gizmo.ResultHandle; |
| 25 | +import io.quarkus.gizmo.TryBlock; |
| 26 | +import io.quarkus.panache.common.deployment.PanacheConstants; |
| 27 | +import io.quarkus.panache.mock.MockPanacheEntities; |
| 28 | +import io.quarkus.panache.mock.PanacheMock; |
| 29 | +import io.quarkus.test.component.QuarkusComponentTestCallbacks; |
| 30 | + |
| 31 | +public class PanacheQuarkusComponentTestCallbacks implements QuarkusComponentTestCallbacks { |
| 32 | + |
| 33 | + private static final DotName PANACHE_ENTITY = DotName.createSimple("io.quarkus.hibernate.orm.panache.PanacheEntity"); |
| 34 | + private static final DotName PANACHE_ENTITY_BASE = DotName |
| 35 | + .createSimple("io.quarkus.hibernate.orm.panache.PanacheEntityBase"); |
| 36 | + |
| 37 | + @Override |
| 38 | + public void beforeBuild(BeforeBuildContext buildContext) { |
| 39 | + Class<?> testClass = buildContext.getTestClass(); |
| 40 | + MockPanacheEntities panacheMocks = testClass.getAnnotation(MockPanacheEntities.class); |
| 41 | + if (panacheMocks == null || panacheMocks.value().length == 0) { |
| 42 | + return; |
| 43 | + } |
| 44 | + List<ClassInfo> mockedEntities = new ArrayList<>(); |
| 45 | + for (Class<?> mockClass : panacheMocks.value()) { |
| 46 | + ClassInfo maybeMockedEntity = buildContext.getComputingBeanArchiveIndex().getClassByName(mockClass); |
| 47 | + DotName superName = maybeMockedEntity.superName(); |
| 48 | + if (maybeMockedEntity.isAbstract() |
| 49 | + || superName == null |
| 50 | + || (!superName.equals(PANACHE_ENTITY) |
| 51 | + && !superName.equals(PANACHE_ENTITY_BASE))) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + mockedEntities.add(maybeMockedEntity); |
| 55 | + } |
| 56 | + |
| 57 | + Map<DotName, List<MethodInfo>> entityUserMethods = new HashMap<>(); |
| 58 | + for (ClassInfo entity : mockedEntities) { |
| 59 | + for (MethodInfo method : entity.methods()) { |
| 60 | + if (!method.isSynthetic() |
| 61 | + && Modifier.isStatic(method.flags()) |
| 62 | + && Modifier.isPublic(method.flags())) { |
| 63 | + List<MethodInfo> userMethods = entityUserMethods.get(entity.name()); |
| 64 | + if (userMethods == null) { |
| 65 | + userMethods = new ArrayList<>(); |
| 66 | + entityUserMethods.put(entity.name(), userMethods); |
| 67 | + } |
| 68 | + userMethods.add(method); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Map<DotName, List<MethodInfo>> entityGeneratedMethods = new HashMap<>(); |
| 74 | + ClassInfo panacheEntityBaseClassInfo = buildContext.getComputingBeanArchiveIndex() |
| 75 | + .getClassByName(DotName.createSimple("io.quarkus.hibernate.orm.panache.PanacheEntityBase")); |
| 76 | + for (ClassInfo entity : mockedEntities) { |
| 77 | + for (MethodInfo method : panacheEntityBaseClassInfo.methods()) { |
| 78 | + if (!userMethodExists(entityUserMethods.get(entity.name()), method)) { |
| 79 | + AnnotationInstance bridge = method.annotation(PanacheConstants.DOTNAME_GENERATE_BRIDGE); |
| 80 | + if (bridge != null) { |
| 81 | + // TODO bridge.value("targetReturnTypeErased") and bridge.value("callSuperMethod") |
| 82 | + List<MethodInfo> generated = entityGeneratedMethods.get(entity.name()); |
| 83 | + if (generated == null) { |
| 84 | + generated = new ArrayList<>(); |
| 85 | + entityGeneratedMethods.put(entity.name(), generated); |
| 86 | + } |
| 87 | + generated.add(method); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + for (ClassInfo entity : mockedEntities) { |
| 94 | + String entityClassName = entity.name().toString(); |
| 95 | + ClassTransformer transformer = new ClassTransformer(entity.name().toString()); |
| 96 | + List<MethodInfo> userMethods = entityUserMethods.get(entity.name()); |
| 97 | + if (userMethods != null) { |
| 98 | + for (MethodInfo userMethod : userMethods) { |
| 99 | + transformer.removeMethod(MethodDescriptor.of(userMethod)); |
| 100 | + addMethod(entityClassName, transformer, userMethod); |
| 101 | + } |
| 102 | + } |
| 103 | + List<MethodInfo> generatedMethods = entityGeneratedMethods.get(entity.name()); |
| 104 | + if (generatedMethods != null) { |
| 105 | + for (MethodInfo generatedMethod : generatedMethods) { |
| 106 | + addMethod(entityClassName, transformer, generatedMethod); |
| 107 | + } |
| 108 | + } |
| 109 | + buildContext.addBytecodeTransformer( |
| 110 | + new BytecodeTransformer(entityClassName, new BiFunction<String, ClassVisitor, ClassVisitor>() { |
| 111 | + @Override |
| 112 | + public ClassVisitor apply(String className, ClassVisitor originalVisitor) { |
| 113 | + return transformer.applyTo(originalVisitor); |
| 114 | + } |
| 115 | + })); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void addMethod(String entityClass, ClassTransformer transformer, MethodInfo method) { |
| 120 | + MethodCreator transform = transformer.addMethod(MethodDescriptor.of(method)); |
| 121 | + transform.setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC); |
| 122 | + // if (!PanacheMock.IsMockEnabled) |
| 123 | + // throw new RuntimeException("Panache mock not enabled"); |
| 124 | + // if (!PanacheMock.isMocked(TestClass.class)) |
| 125 | + // throw new RuntimeException("FooEntity not mocked"); |
| 126 | + // try { |
| 127 | + // return (int)PanacheMock.mockMethod(TestClass.class, "foo", new Class<?>[] {int.class}, new Object[] {arg}); |
| 128 | + // } catch (PanacheMock.InvokeRealMethodException e) { |
| 129 | + // throw new RuntimeException("Unstubbed method called", e); |
| 130 | + // } |
| 131 | + ResultHandle isMockEnabled = transform |
| 132 | + .readStaticField(FieldDescriptor.of(PanacheMock.class, "IsMockEnabled", boolean.class)); |
| 133 | + BytecodeCreator mockNotEnabled = transform.ifTrue(isMockEnabled).falseBranch(); |
| 134 | + mockNotEnabled.throwException(RuntimeException.class, "Panache mock not enabled"); |
| 135 | + ResultHandle isMocked = transform.invokeStaticMethod( |
| 136 | + MethodDescriptor.ofMethod(PanacheMock.class, "isMocked", boolean.class, Class.class), |
| 137 | + transform.loadClass(entityClass)); |
| 138 | + BytecodeCreator notMocked = transform.ifTrue(isMocked).falseBranch(); |
| 139 | + notMocked.throwException(RuntimeException.class, entityClass + " not mocked"); |
| 140 | + ResultHandle entityClazz = transform.loadClass(entityClass); |
| 141 | + ResultHandle methodName = transform.load(method.name()); |
| 142 | + ResultHandle paramTypes = transform.newArray(Class.class, method.parametersCount()); |
| 143 | + for (int i = 0; i < method.parametersCount(); i++) { |
| 144 | + transform.writeArrayValue(paramTypes, i, transform.loadClass(method.parameterType(i).name().toString())); |
| 145 | + } |
| 146 | + ResultHandle args = transform.newArray(Object.class, method.parametersCount()); |
| 147 | + for (int i = 0; i < method.parametersCount(); i++) { |
| 148 | + transform.writeArrayValue(args, i, transform.getMethodParam(i)); |
| 149 | + } |
| 150 | + TryBlock tryInvoke = transform.tryBlock(); |
| 151 | + ResultHandle ret = tryInvoke.invokeStaticMethod(MethodDescriptor.ofMethod(PanacheMock.class, "mockMethod", |
| 152 | + Object.class, Class.class, String.class, Class[].class, Object[].class), entityClazz, methodName, |
| 153 | + paramTypes, |
| 154 | + args); |
| 155 | + tryInvoke.returnValue(ret); |
| 156 | + CatchBlockCreator catched = tryInvoke.addCatch(PanacheMock.InvokeRealMethodException.class); |
| 157 | + catched.throwException(RuntimeException.class, |
| 158 | + "Unstubbed method called: " + method.toString(), catched.getCaughtException()); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void afterStart(AfterStartContext afterStartContext) { |
| 163 | + Class<?> testClass = afterStartContext.getTestClass(); |
| 164 | + MockPanacheEntities panacheMocks = testClass.getAnnotation(MockPanacheEntities.class); |
| 165 | + if (panacheMocks == null || panacheMocks.value().length == 0) { |
| 166 | + return; |
| 167 | + } |
| 168 | + PanacheMock.mock(panacheMocks.value()); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void afterStop(AfterStopContext afterStopContext) { |
| 173 | + PanacheMock.reset(); |
| 174 | + } |
| 175 | + |
| 176 | + private boolean userMethodExists(List<MethodInfo> userMethods, MethodInfo method) { |
| 177 | + if (userMethods == null || userMethods.isEmpty()) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + String descriptor = method.descriptor(); |
| 181 | + for (MethodInfo userMethod : userMethods) { |
| 182 | + if (userMethod.descriptor().equals(descriptor)) { |
| 183 | + return true; |
| 184 | + } |
| 185 | + } |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments