1 /* 2 * Copyright 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 package androidx.webkit.internal.codegen.representations; 17 18 import com.intellij.psi.PsiMethod; 19 import com.intellij.psi.PsiParameter; 20 import com.squareup.javapoet.MethodSpec; 21 import com.squareup.javapoet.ParameterSpec; 22 23 import javax.lang.model.element.Modifier; 24 25 import androidx.webkit.internal.codegen.TypeConversionUtils; 26 27 /** 28 * Representation of a method for the support library to implement. 29 */ 30 public class MethodRepr { 31 public final PsiMethod psiMethod; 32 33 private MethodRepr(PsiMethod psiMethod) { 34 this.psiMethod = psiMethod; 35 } 36 37 /** 38 * Generate a MethodRepr from a PsiMethod. 39 */ 40 public static MethodRepr fromPsiMethod(PsiMethod psiMethod) { 41 return new MethodRepr(psiMethod); 42 } 43 44 /** 45 * Generate a method declaration, to be put in a boundary interface, from this MethodRepr 46 * representing an android.webkit method. 47 */ 48 public MethodSpec createBoundaryInterfaceMethodDeclaration() { 49 MethodSpec.Builder builder = MethodSpec.methodBuilder(this.psiMethod.getName()) 50 .returns(TypeConversionUtils.getBoundaryType(this.psiMethod.getReturnType())) 51 // The ABSTRACT modifier here ensures the method doesn't have a body. 52 .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC); 53 for (PsiParameter param : this.psiMethod.getParameterList().getParameters()) { 54 builder.addParameter( 55 ParameterSpec.builder( 56 TypeConversionUtils.getBoundaryType(param.getType()), 57 param.getName() 58 ).build()); 59 } 60 return builder.build(); 61 } 62 } 63