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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.analysis
22 import io.gitlab.arturbosch.detekt.api.*
23 import org.jetbrains.kotlin.psi.*
24 import org.jetbrains.kotlin.psi.psiUtil.isPublic
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))
30 override fun visitKtFile(file: KtFile) {
31 super.visitKtFile(file)
33 if (file.packageFqName.toString().contains("impl")) {
34 checkAccessModifiers(file)
38 private fun checkAccessModifiers(file: KtFile) {
39 val implVisitor = ImplVisitor()
41 file.accept(implVisitor)
42 if (implVisitor.publicDeclarations.isNotEmpty()) {
43 reportCodeSmells(implVisitor)
47 private fun reportCodeSmells(it: ImplVisitor) {
48 for (entity in it.publicDeclarations)
49 report(CodeSmell(issue, entity, REPORT_MESSAGE))
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
57 private const val ISSUE_DESCRIPTION = "Reports public modifiers inside '*.impl' package."
61 private class ImplVisitor : DetektVisitor() {
62 var publicDeclarations = mutableListOf<Entity>()
64 override fun visitClassOrObject(classOrObject: KtClassOrObject) {
65 if (classOrObject.isTopLevel() && classOrObject.isPublic) {
66 publicDeclarations.add(Entity.from(classOrObject))
70 override fun visitNamedFunction(function: KtNamedFunction) {
71 if (function.isTopLevel && function.isPublic) {
72 publicDeclarations.add(Entity.from(function))
76 override fun visitProperty(property: KtProperty) {
77 if (property.isTopLevel && property.isPublic) publicDeclarations.add(Entity.from(property))