027cadea2fac9954164b2de6a4ca1bb586364298
[dcaegen2/collectors/hv-ves.git] /
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             ImplVisitor.also {
35                 file.accept(it)
36                 if(it.publicDeclarations.isNotEmpty()){
37                     for(entity in it.publicDeclarations)
38                         report(CodeSmell(issue, entity, REPORT_MESSAGE))
39                     it.publicDeclarations.clear()
40                 }
41             }
42         }
43     }
44
45     companion object {
46         private val REPORT_MESSAGE =  """
47                                 Implementation package members cannot have public declarations.
48                                 Please, add `internal` modifier for this element to disallow usage outside of module
49                             """.trimIndent()
50         private const val ISSUE_DESCRIPTION = "Reports public modifiers inside '*.impl' package."
51     }
52 }
53
54 private object ImplVisitor: DetektVisitor(){
55     var publicDeclarations = mutableListOf<Entity>()
56
57     override fun visitClassOrObject(classOrObject: KtClassOrObject) {
58         if(classOrObject.isTopLevel() && classOrObject.isPublic){
59             publicDeclarations.add(Entity.from(classOrObject))
60         }
61     }
62
63     override fun visitNamedFunction(function: KtNamedFunction) {
64         if(function.isTopLevel && function.isPublic){
65             publicDeclarations.add(Entity.from(function))
66         }
67     }
68
69     override fun visitProperty(property: KtProperty) {
70         if(property.isTopLevel && property.isPublic) publicDeclarations.add(Entity.from(property))
71     }
72 }