Skip to content

Commit

Permalink
Declarative logging module (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
slutmaker authored Aug 2, 2023
1 parent 1248459 commit 5ed088b
Show file tree
Hide file tree
Showing 56 changed files with 2,621 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.annotation.processor.common.compile.ByteArrayJavaFileObject;
import ru.tinkoff.kora.annotation.processor.common.compile.KoraCompileTestJavaFileManager;
import ru.tinkoff.kora.application.graph.*;
Expand All @@ -16,11 +17,13 @@
import javax.tools.ToolProvider;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -125,6 +128,7 @@ public Object newObject(String className, Object... params) {
throw new RuntimeException(e);
}
}

public Object invoke(Object target, String name, Object... params) {
try {
for (var method : target.getClass().getMethods()) {
Expand Down Expand Up @@ -227,4 +231,42 @@ public <T> PromiseOf<T> promiseOf(Node<T> node) {
return graph.promiseOf(node);
}
}

protected static class TestObject {
public final Class<?> objectClass;
private final Object object;

public TestObject(Class<?> objectClass, Object object) {
this.objectClass = objectClass;
this.object = object;
}

@SuppressWarnings("unchecked")
public <T> T invoke(String methodName, Object... args) {
for (var method : objectClass.getDeclaredMethods()) {
if (method.getName().equals(methodName) && method.getParameters().length == args.length) {
method.setAccessible(true);
try {
var result = method.invoke(this.object, args);
if (result instanceof Mono<?> mono) {
return (T) mono.block();
}
if (result instanceof Future<?> future) {
return (T) future.get();
}
return (T) result;
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException re) {
throw re;
} else {
throw new RuntimeException(e);
}
} catch (IllegalAccessException | ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
throw new IllegalArgumentException();
}
}
}
1 change: 1 addition & 0 deletions annotation-processors/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
api project(':cache:cache-annotation-processor')
api project(':validation:validation-annotation-processor')
api project(':mapstruct:mapstruct-java-extension')
api project(':logging:declarative-logging:declarative-logging-annotation-processor')
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.tinkoff.kora.aop.symbol.processor

import com.google.devtools.ksp.*
import com.google.devtools.ksp.isConstructor
import com.google.devtools.ksp.isProtected
import com.google.devtools.ksp.isPublic
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
Expand All @@ -9,14 +11,12 @@ import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.toTypeVariableName
import ru.tinkoff.kora.common.Component
import ru.tinkoff.kora.ksp.common.*
import ru.tinkoff.kora.ksp.common.AnnotationUtils.isAnnotationPresent
import ru.tinkoff.kora.ksp.common.KspCommonUtils.generated
import ru.tinkoff.kora.ksp.common.exception.ProcessingErrorException
import kotlin.reflect.KClass

@KspExperimental
class AopProcessor(private val aspects: List<KoraAspect>, private val resolver: Resolver) {

private class TypeFieldFactory(private val resolver: Resolver) : KoraAspect.FieldFactory {
Expand Down Expand Up @@ -227,8 +227,8 @@ class AopProcessor(private val aspects: List<KoraAspect>, private val resolver:

typeBuilder.generated(generatedClasses)

if (classDeclaration.isAnnotationPresent(Component::class)) {
typeBuilder.addAnnotation(Component::class)
if (classDeclaration.isAnnotationPresent(CommonClassNames.component)) {
typeBuilder.addAnnotation(CommonClassNames.component)
}

val constructorBuilder = FunSpec.constructorBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package ru.tinkoff.kora.aop.symbol.processor

import com.google.devtools.ksp.*
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.isAbstract
import com.google.devtools.ksp.isOpen
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.ksp.writeTo
import ru.tinkoff.kora.common.AopAnnotation
import ru.tinkoff.kora.ksp.common.*
import ru.tinkoff.kora.ksp.common.AnnotationUtils.isAnnotationPresent
import ru.tinkoff.kora.ksp.common.exception.ProcessingError
import ru.tinkoff.kora.ksp.common.exception.ProcessingErrorException
import java.util.*

@KspExperimental
class AopSymbolProcessor(
environment: SymbolProcessorEnvironment,
) : BaseSymbolProcessor(environment) {
Expand All @@ -32,7 +34,7 @@ class AopSymbolProcessor(
aopProcessor = AopProcessor(aspects, resolver)
annotations = aspects.asSequence().map { it.getSupportedAnnotationTypes() }.flatten().mapNotNull { resolver.getClassDeclarationByName(it) }.toList()

val noAopAnnotation = annotations.filter { !it.isAnnotationPresent(AopAnnotation::class) }
val noAopAnnotation = annotations.filter { !it.isAnnotationPresent(CommonClassNames.aopAnnotation) }

noAopAnnotation.forEach { noAop ->
KoraSymbolProcessingEnv.logger.warn("Annotation ${noAop.simpleName.asString()} has no @AopAnnotation marker, it will not be handled by some util methods" )
Expand Down Expand Up @@ -146,7 +148,6 @@ class AopSymbolProcessor(
}


@KspExperimental
class AopSymbolProcessorProvider : SymbolProcessorProvider {

override fun create(
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ tasks.register("other-test") {
if (p.depth == 2) {
name = p.parent.name + ":" + name
}
if (p.depth == 3) {
name = p.parent.parent.name + ":" + p.parent.name + ":" + name
}
dependsOn(":" + name + ":test")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,13 @@

import org.assertj.core.api.Assertions;
import org.intellij.lang.annotations.Language;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.annotation.processor.common.AbstractAnnotationProcessorTest;
import ru.tinkoff.kora.database.annotation.processor.RepositoryAnnotationProcessor;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public abstract class AbstractRepositoryTest extends AbstractAnnotationProcessorTest {
protected static class TestRepository {
public final Class<?> repositoryClass;
private final Object repositoryObject;

protected TestRepository(Class<?> repositoryClass, Object repositoryObject) {
this.repositoryClass = repositoryClass;
this.repositoryObject = repositoryObject;
}

@SuppressWarnings("unchecked")
public <T> T invoke(String method, Object... args) {
for (var repositoryClassMethod : repositoryClass.getDeclaredMethods()) {
if (repositoryClassMethod.getName().equals(method) && repositoryClassMethod.getParameters().length == args.length) {
try {
repositoryClassMethod.setAccessible(true);
var result = repositoryClassMethod.invoke(this.repositoryObject, args);
if (result instanceof Mono<?> mono) {
return (T) mono.block();
}
if (result instanceof Future<?> future) {
return (T) future.get();
}
return (T) result;
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException re) {
throw re;
} else {
throw new RuntimeException(e);
}
} catch (IllegalAccessException | ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
throw new IllegalArgumentException();
}
}

@Override
protected String commonImports() {
return super.commonImports() + """
Expand All @@ -58,7 +17,7 @@ protected String commonImports() {
""";
}

protected TestRepository compile(Object connectionFactory, List<?> arguments, @Language("java") String... sources) {
protected TestObject compile(Object connectionFactory, List<?> arguments, @Language("java") String... sources) {
var compileResult = compile(List.of(new RepositoryAnnotationProcessor()), sources);
if (compileResult.isFailed()) {
throw compileResult.compilationException();
Expand All @@ -77,13 +36,13 @@ protected TestRepository compile(Object connectionFactory, List<?> arguments, @L
}
}
var repository = repositoryClass.getConstructors()[0].newInstance(realArgs);
return new TestRepository(repositoryClass, repository);
return new TestObject(repositoryClass, repository);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

protected TestRepository compileForArgs(List<?> arguments, @Language("java") String... sources) {
protected TestObject compileForArgs(List<?> arguments, @Language("java") String... sources) {
var compileResult = compile(List.of(new RepositoryAnnotationProcessor()), sources);
if (compileResult.isFailed()) {
throw compileResult.compilationException();
Expand All @@ -95,7 +54,7 @@ protected TestRepository compileForArgs(List<?> arguments, @Language("java") Str
var repositoryClass = compileResult.loadClass("$TestRepository_Impl");
var realArgs = arguments.toArray();
var repository = repositoryClass.getConstructors()[0].newInstance(realArgs);
return new TestRepository(repositoryClass, repository);
return new TestObject(repositoryClass, repository);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected String commonImports() {
""";
}

protected TestRepository compileCassandra(List<?> arguments, @Language("java") String... sources) {
protected TestObject compileCassandra(List<?> arguments, @Language("java") String... sources) {
return this.compile(this.executor, arguments, sources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public interface TestRepository extends CassandraRepository {
verify(executor.boundStatementBuilder).setLong(0, 42L);
verify(mapper).apply(any(), eq(1), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(CassandraParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand All @@ -425,7 +425,7 @@ public interface TestRepository extends CassandraRepository {

verify(mapper).apply(any(), eq(0), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(CassandraParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand All @@ -445,7 +445,7 @@ public interface TestRepository extends CassandraRepository {
public record TestRecord(String value){}
""");

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(CassandraParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public interface TestRepository extends CassandraRepository {
verify(executor.mockSession).execute(any(Statement.class));
verify(mapper).apply(executor.resultSet);

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(CassandraResultSetMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected String commonImports() {
""";
}

protected TestRepository compileJdbc(List<?> arguments, @Language("java") String... sources) {
protected TestObject compileJdbc(List<?> arguments, @Language("java") String... sources) {
return this.compile(this.executor, arguments, sources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public interface TestRepository extends JdbcRepository {
verify(executor.preparedStatement).setLong(1, 42L);
verify(mapper).set(any(), eq(2), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(JdbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down Expand Up @@ -385,7 +385,7 @@ public interface TestRepository extends JdbcRepository {

verify(mapper).set(any(), eq(1), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(JdbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand All @@ -405,7 +405,7 @@ public interface TestRepository extends JdbcRepository {
public record TestRecord(String value){}
""");

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(JdbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public interface TestRepository extends JdbcRepository {
verify(mapper).apply(executor.resultSet);
executor.reset();

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(JdbcResultSetMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected String commonImports() {
""";
}

protected TestRepository compileR2dbc(List<?> arguments, @Language("java") String... sources) {
protected TestObject compileR2dbc(List<?> arguments, @Language("java") String... sources) {
return this.compile(this.executor, arguments, sources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public interface TestRepository extends R2dbcRepository {
verify(executor.statement).bind(0, 42L);
verify(mapper).apply(any(), eq(1), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(R2dbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand All @@ -265,7 +265,7 @@ public interface TestRepository extends R2dbcRepository {

verify(mapper).apply(any(), eq(0), eq("test-value"));

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(R2dbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand All @@ -285,7 +285,7 @@ public interface TestRepository extends R2dbcRepository {
public record TestRecord(String value){}
""");

var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(R2dbcParameterColumnMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public interface TestRepository extends R2dbcRepository {
verify(mapper).apply(any());


var mapperConstructorParameter = repository.repositoryClass.getConstructors()[0].getParameters()[1];
var mapperConstructorParameter = repository.objectClass.getConstructors()[0].getParameters()[1];
assertThat(mapperConstructorParameter.getType()).isEqualTo(R2dbcResultFluxMapper.class);
var tag = mapperConstructorParameter.getAnnotation(Tag.class);
assertThat(tag).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected String commonImports() {
""";
}

protected TestRepository compileVertx(List<?> arguments, @Language("java") String... sources) {
protected TestObject compileVertx(List<?> arguments, @Language("java") String... sources) {
return this.compile(this.executor, arguments, sources);
}

Expand Down
Loading

0 comments on commit 5ed088b

Please sign in to comment.