Home | History | Annotate | Download | only in processor
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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.
     15  */
     16 
     17 package androidx.room.processor
     18 
     19 import androidx.room.vo.Warning
     20 import com.google.auto.common.AnnotationMirrors
     21 import com.google.auto.common.MoreElements
     22 import javax.lang.model.element.AnnotationValue
     23 import javax.lang.model.element.Element
     24 import javax.lang.model.util.SimpleAnnotationValueVisitor6
     25 
     26 /**
     27  * A visitor that reads SuppressWarnings annotations and keeps the ones we know about.
     28  */
     29 object SuppressWarningProcessor {
     30 
     31     fun getSuppressedWarnings(element: Element): Set<Warning> {
     32         val annotation = MoreElements.getAnnotationMirror(element,
     33                 SuppressWarnings::class.java).orNull()
     34         return if (annotation == null) {
     35             emptySet()
     36         } else {
     37             val value = AnnotationMirrors.getAnnotationValue(annotation, "value")
     38             if (value == null) {
     39                 emptySet()
     40             } else {
     41                 VISITOR.visit(value)
     42             }
     43         }
     44     }
     45 
     46     private object VISITOR : SimpleAnnotationValueVisitor6<Set<Warning>, String>() {
     47         override fun visitArray(values: List<AnnotationValue>?, elementName: String?
     48         ): Set<Warning> {
     49             return values?.mapNotNull {
     50                 Warning.fromPublicKey(it.value.toString())
     51             }?.toSet() ?: emptySet()
     52         }
     53 
     54         override fun defaultAction(o: Any?, p: String?): Set<Warning> {
     55             return emptySet()
     56         }
     57     }
     58 }
     59