|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.sourcegen.custom.visitor; |
| 17 | + |
| 18 | +import io.micronaut.core.annotation.Internal; |
| 19 | +import io.micronaut.core.annotation.NonNull; |
| 20 | +import io.micronaut.inject.ast.ClassElement; |
| 21 | +import io.micronaut.inject.visitor.TypeElementVisitor; |
| 22 | +import io.micronaut.inject.visitor.VisitorContext; |
| 23 | +import io.micronaut.sourcegen.custom.example.GenerateMyRepository1; |
| 24 | +import io.micronaut.sourcegen.custom.example.GenerateMyRepository2; |
| 25 | +import io.micronaut.sourcegen.generator.SourceGenerator; |
| 26 | +import io.micronaut.sourcegen.generator.SourceGenerators; |
| 27 | +import io.micronaut.sourcegen.model.ClassDef; |
| 28 | +import io.micronaut.sourcegen.model.ClassTypeDef; |
| 29 | +import io.micronaut.sourcegen.model.InterfaceDef; |
| 30 | +import io.micronaut.sourcegen.model.MethodDef; |
| 31 | +import io.micronaut.sourcegen.model.PropertyDef; |
| 32 | +import io.micronaut.sourcegen.model.TypeDef; |
| 33 | + |
| 34 | +import javax.lang.model.element.Modifier; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +@Internal |
| 39 | +public final class GenerateMyRepository2Visitor implements TypeElementVisitor<GenerateMyRepository2, Object> { |
| 40 | + |
| 41 | + @Override |
| 42 | + public @NonNull VisitorKind getVisitorKind() { |
| 43 | + return VisitorKind.ISOLATING; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void visitClass(ClassElement element, VisitorContext context) { |
| 48 | + SourceGenerator sourceGenerator = SourceGenerators.findByLanguage(context.getLanguage()).orElse(null); |
| 49 | + if (sourceGenerator == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + InterfaceDef myRepositoryRef = InterfaceDef.builder(element.getPackageName() + ".MyRepository2") |
| 54 | + .addModifiers(Modifier.PUBLIC) |
| 55 | + |
| 56 | + .addSuperinterface(TypeDef.of( |
| 57 | + element.withTypeArguments( |
| 58 | + List.of( |
| 59 | + context.getClassElement(Integer.class).get(), |
| 60 | + context.getClassElement(String.class).get() |
| 61 | + ) |
| 62 | + ) |
| 63 | + )) |
| 64 | + .build(); |
| 65 | + |
| 66 | + sourceGenerator.write(myRepositoryRef, context, element); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments