Skip to content

Commit 912801d

Browse files
committed
improve annotation abstraction
1 parent 081d1d4 commit 912801d

17 files changed

Lines changed: 121 additions & 158 deletions

File tree

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/framework/Framework.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2020 the 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-
* http://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.
2+
* Copyright 2020 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
154
*/
165

176
package io.openapiprocessor.core.framework

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/framework/FrameworkAnnotation.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
/*
2-
* Copyright 2020 the 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-
* http://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.
2+
* Copyright 2020 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
154
*/
165

176
package io.openapiprocessor.core.framework
187

198
import io.openapiprocessor.core.model.parameters.Parameter
209
import io.openapiprocessor.core.parser.HttpMethod
10+
import io.openapiprocessor.core.model.Annotation
2111

2212
/**
2313
* provides annotation details of the framework.
24-
*
25-
* @author Martin Hauner
2614
*/
2715
interface FrameworkAnnotations {
2816

@@ -32,14 +20,14 @@ interface FrameworkAnnotations {
3220
* @param httpMethod requested http method
3321
* @return annotation details
3422
*/
35-
fun getAnnotation(httpMethod: HttpMethod): FrameworkAnnotation
23+
fun getAnnotation(httpMethod: HttpMethod): Annotation
3624

3725
/**
3826
* provides the details of the requested method parameter annotation.
3927
*
4028
* @param parameter requested parameter
4129
* @return annotation details
4230
*/
43-
fun getAnnotation(parameter: Parameter): FrameworkAnnotation
31+
fun getAnnotation(parameter: Parameter): Annotation
4432

4533
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.model
7+
8+
import io.openapiprocessor.core.converter.mapping.ParameterValue
9+
10+
open class Annotation(
11+
private val canonicalName: String,
12+
val parameters: LinkedHashMap<String, ParameterValue> = linkedMapOf()
13+
) {
14+
val typeName: String
15+
get() {
16+
return canonicalName.substring(canonicalName.lastIndexOf('.') + 1)
17+
}
18+
19+
val packageName: String
20+
get() {
21+
return canonicalName.substring(0, canonicalName.lastIndexOf('.') + 1)
22+
}
23+
24+
val imports = setOf(canonicalName)
25+
26+
val referencedImports: Set<String>
27+
get() {
28+
return parameters
29+
.values.mapNotNull { it.import }
30+
.toSet()
31+
}
32+
33+
/**
34+
* The full annotation name with a leading @.
35+
*/
36+
val annotationName: String
37+
get() = "@${typeName}"
38+
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/Annotation.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/AnnotationWriter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* PDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.openapiprocessor.core.writer.java;
6+
package io.openapiprocessor.core.writer.java
77

8+
import io.openapiprocessor.core.model.Annotation
89
import java.io.StringWriter
910
import java.io.Writer
1011

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/BeanValidationFactory.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.mapping.ParameterValue
99
import io.openapiprocessor.core.converter.mapping.SimpleParameterValue
10+
import io.openapiprocessor.core.model.Annotation
1011
import io.openapiprocessor.core.model.datatypes.*
1112
import org.apache.commons.text.StringEscapeUtils.escapeJava
1213

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/BeanValidationInfo.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.openapiprocessor.core.writer.java
77

8+
import io.openapiprocessor.core.model.Annotation
89
import io.openapiprocessor.core.model.datatypes.*
910

1011
data class BeanValidationValue(
@@ -40,7 +41,7 @@ class BeanValidationInfoSimple(
4041
)
4142

4243
private val annotationImports: Set<String>
43-
get() = annotations.map { it.import }.toSet()
44+
get() = annotations.map { it.imports }.flatten().toSet()
4445

4546
private val annotationValues: List<String>
4647
get() = annotations.map { buildAnnotation(it) }.toList()
@@ -147,13 +148,13 @@ class BeanValidationInfoCollection(
147148
}
148149

149150
private val annotationImports: Set<String>
150-
get() = annotations.map { it.import }.toSet()
151+
get() = annotations.map { it.imports }.flatten().toSet()
151152

152153
private val annotationValues: List<String>
153154
get() = annotations.map { buildAnnotation(it) }.toList()
154155

155156
private val itemAnnotationImports: Set<String>
156-
get() = item.annotations.map { it.import }.toSet()
157+
get() = item.annotations.map { it.imports }.flatten().toSet()
157158

158159
private val itemAnnotationValues: List<String>
159160
get() = item.annotations.map { buildAnnotation(it) }.toList()

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriterBase.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
99
import io.openapiprocessor.core.converter.MappingFinder
10+
import io.openapiprocessor.core.model.Annotation
1011
import io.openapiprocessor.core.model.datatypes.DataType
1112
import io.openapiprocessor.core.model.datatypes.ModelDataType
1213
import io.openapiprocessor.core.model.datatypes.ObjectDataType

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/InterfaceWriter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class InterfaceWriter(
5656
imports.add(generatedWriter.getImport())
5757

5858
endpoints.forEach { ep ->
59-
imports.add(annotations.getAnnotation (ep.method).fullyQualifiedName)
59+
imports.addAll(annotations.getAnnotation (ep.method).imports)
6060

6161
if (ep.deprecated) {
6262
imports.add (java.lang.Deprecated::class.java.canonicalName)
@@ -87,7 +87,7 @@ class InterfaceWriter(
8787
}
8888

8989
if (parameter.withAnnotation) {
90-
imports.add(annotations.getAnnotation(parameter).fullyQualifiedName)
90+
imports.addAll(annotations.getAnnotation(parameter).imports)
9191
}
9292

9393
if (parameter is AdditionalParameter && parameter.annotationDataType != null) {

0 commit comments

Comments
 (0)