Home | History | Annotate | Download | only in processor
      1 /*
      2  * Copyright (C) 2016 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 package androidx.room.processor
     17 
     18 import androidx.room.Delete
     19 import androidx.room.ext.typeName
     20 import androidx.room.vo.DeletionMethod
     21 import com.squareup.javapoet.TypeName
     22 import javax.lang.model.element.ExecutableElement
     23 import javax.lang.model.type.DeclaredType
     24 
     25 class DeletionMethodProcessor(baseContext: Context,
     26                               val containing: DeclaredType,
     27                               val executableElement: ExecutableElement) {
     28     val context = baseContext.fork(executableElement)
     29 
     30     fun process(): DeletionMethod {
     31         val delegate = ShortcutMethodProcessor(context, containing, executableElement)
     32         delegate.extractAnnotation(Delete::class, ProcessorErrors.MISSING_DELETE_ANNOTATION)
     33 
     34         val returnTypeName = delegate.extractReturnType().typeName()
     35         context.checker.check(
     36                 returnTypeName == TypeName.VOID || returnTypeName == TypeName.INT,
     37                 executableElement,
     38                 ProcessorErrors.DELETION_METHODS_MUST_RETURN_VOID_OR_INT
     39         )
     40 
     41         val (entities, params) = delegate.extractParams(
     42                 missingParamError = ProcessorErrors
     43                         .DELETION_MISSING_PARAMS
     44         )
     45 
     46         return DeletionMethod(
     47                 element = delegate.executableElement,
     48                 name = delegate.executableElement.simpleName.toString(),
     49                 entities = entities,
     50                 returnCount = returnTypeName == TypeName.INT,
     51                 parameters = params
     52         )
     53     }
     54 }
     55