Add internal modifiers to comply with new HvVesCustomRule
[dcaegen2/collectors/hv-ves.git] / build / hv-collector-analysis / src / main / kotlin / org / onap / dcae / collectors / veshv / analysis / PublicModifiersInImpl.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.analysis
21
22 import io.gitlab.arturbosch.detekt.api.*
23 import org.jetbrains.kotlin.psi.*
24 import org.jetbrains.kotlin.psi.psiUtil.isPublic
25
26 class PublicModifiersInImpl(config: Config = Config.empty) : Rule(config) {
27     override val issue: Issue = Issue(javaClass.simpleName, Severity.Maintainability,
28             ISSUE_DESCRIPTION, Debt(mins = 10))
29
30     override fun visitKtFile(file: KtFile) {
31         super.visitKtFile(file)
32
33         if (file.packageFqName.toString().contains("impl")) {
34             checkAccessModifiers(file)
35         }
36     }
37
38     private fun checkAccessModifiers(file: KtFile) {
39         val implVisitor = ImplVisitor()
40
41         file.accept(implVisitor)
42         if (implVisitor.publicDeclarations.isNotEmpty()) {
43             reportCodeSmells(implVisitor)
44         }
45     }
46
47     private fun reportCodeSmells(it: ImplVisitor) {
48         for (entity in it.publicDeclarations)
49             report(CodeSmell(issue, entity, REPORT_MESSAGE))
50     }
51
52     companion object {
53         private val REPORT_MESSAGE = """
54                                 Implementation package members cannot have public declarations.
55                                 Please, add `internal` modifier for this element to disallow usage outside of module
56                             """.trimIndent()
57         private const val ISSUE_DESCRIPTION = "Reports public modifiers inside '*.impl' package."
58     }
59 }
60
61 private class ImplVisitor : DetektVisitor() {
62     var publicDeclarations = mutableListOf<Entity>()
63
64     override fun visitClassOrObject(classOrObject: KtClassOrObject) {
65         if (classOrObject.isTopLevel() && classOrObject.isPublic) {
66             publicDeclarations.add(Entity.from(classOrObject))
67         }
68     }
69
70     override fun visitNamedFunction(function: KtNamedFunction) {
71         if (function.isTopLevel && function.isPublic) {
72             publicDeclarations.add(Entity.from(function))
73         }
74     }
75
76     override fun visitProperty(property: KtProperty) {
77         if (property.isTopLevel && property.isPublic) publicDeclarations.add(Entity.from(property))
78     }
79 }