Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java generator: try-catch-finally, synchronized + super method invocation #259

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ public void testStaticMethod() {
);
}

@Test
public void testInterfaceSuperMethod() {
Assertions.assertEquals(
"ABCSTT102",
new MethodRepositoryInvoker() {

@Override
public String interfaceMethod(String string, Integer integer, int i) {
throw new UnsupportedOperationException();
}

@Override
public double interfaceMethodReturnsDouble() {
throw new UnsupportedOperationException();
}

@Override
public long interfaceMethodReturnsLong() {
throw new UnsupportedOperationException();
}

@Override
public int interfaceMethodReturnsInt() {
throw new UnsupportedOperationException();
}
}.defaultMethod("STT", 100, 2)
);
}

@Test
public void testDefaultMethodIgnoreResult() {
Assertions.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.ast.MethodElement;
import io.micronaut.inject.visitor.TypeElementVisitor;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.sourcegen.custom.example.GenerateMethodInvocation;
Expand All @@ -28,6 +29,7 @@
import io.micronaut.sourcegen.model.ClassTypeDef;
import io.micronaut.sourcegen.model.ExpressionDef;
import io.micronaut.sourcegen.model.FieldDef;
import io.micronaut.sourcegen.model.JavaIdioms;
import io.micronaut.sourcegen.model.MethodDef;
import io.micronaut.sourcegen.model.StatementDef;
import io.micronaut.sourcegen.model.TypeDef;
Expand Down Expand Up @@ -172,6 +174,22 @@ public void visitClass(ClassElement element, VisitorContext context) {

sourceGenerator.write(classDef, context, element);

ClassTypeDef myRepoType = ClassTypeDef.of(myRepository);
MethodElement defaultMethod = myRepository.findMethod("defaultMethod").get();
ClassDef interfaceSuperInvokerDef = ClassDef.builder("io.micronaut.sourcegen.example.MethodRepositoryInvoker")
.addModifiers(Modifier.ABSTRACT)
.addSuperinterface(myRepoType)
.addMethod(MethodDef.override(defaultMethod)
.build((aThis, methodParameters) ->
JavaIdioms.concatStrings(
ExpressionDef.constant("ABC"),
aThis.superRef(myRepoType)
.invoke(defaultMethod, methodParameters)
).returning())
).build();

sourceGenerator.write(interfaceSuperInvokerDef, context, element);

FieldDef targetField = FieldDef.builder("target", TypeDef.OBJECT).addModifiers(Modifier.PRIVATE).build();
FieldDef lockField = FieldDef.builder("lock", ClassTypeDef.of(ReentrantReadWriteLock.class))
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
Expand Down Expand Up @@ -224,7 +242,9 @@ public void visitClass(ClassElement element, VisitorContext context) {
methodParameters.get(1).invoke("getAndIncrement", TypeDef.Primitive.INT),
StatementDef.doTry(
StatementDef.multi(
ClassTypeDef.of(IllegalStateException.class).instantiate().doThrow(),
ExpressionDef.trueValue().isTrue().doIf(
ClassTypeDef.of(IllegalStateException.class).instantiate().doThrow()
),
methodParameters.get(1).invoke("getAndIncrement", TypeDef.Primitive.INT),
aThis.field(targetField).returning()
)
Expand All @@ -238,7 +258,9 @@ public void visitClass(ClassElement element, VisitorContext context) {
methodParameters.get(1).invoke("getAndIncrement", TypeDef.Primitive.INT),
StatementDef.doTry(
StatementDef.multi(
ClassTypeDef.of(IllegalStateException.class).instantiate(ExpressionDef.constant("Bam")).doThrow(),
ExpressionDef.trueValue().isTrue().doIf(
ClassTypeDef.of(IllegalStateException.class).instantiate(ExpressionDef.constant("Bam")).doThrow()
),
methodParameters.get(1).invoke("getAndIncrement", TypeDef.Primitive.INT),
aThis.field(targetField).returning()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.visitor.TypeElementVisitor;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.sourcegen.custom.example.GenerateMyRepository1;
import io.micronaut.sourcegen.custom.example.GenerateMyRepository2;
import io.micronaut.sourcegen.generator.SourceGenerator;
import io.micronaut.sourcegen.generator.SourceGenerators;
import io.micronaut.sourcegen.model.ClassDef;
import io.micronaut.sourcegen.model.ClassTypeDef;
import io.micronaut.sourcegen.model.InterfaceDef;
import io.micronaut.sourcegen.model.MethodDef;
import io.micronaut.sourcegen.model.PropertyDef;
import io.micronaut.sourcegen.model.TypeDef;

import javax.lang.model.element.Modifier;
import java.util.List;
import java.util.Optional;

@Internal
public final class GenerateMyRepository2Visitor implements TypeElementVisitor<GenerateMyRepository2, Object> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.example;

public interface MyRepository {

static String staticMethod(String string, Integer integer, int i) {
return string + (integer + i);
}

default String defaultMethod(String string, Integer integer, int i) {
return string + (integer + i);
}

String interfaceMethod(String string, Integer integer, int i);

double interfaceMethodReturnsDouble();

long interfaceMethodReturnsLong();

int interfaceMethodReturnsInt();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@GenerateAnnotatedType
@GenerateInnerTypes
@GenerateMyEnum2
@GenerateMethodInvocation
public class Trigger {
public List<String> copyAddresses;
}
Loading
Loading