Home | History | Annotate | Download | only in representations
      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 
     17 package androidx.webkit.internal.codegen.representations;
     18 
     19 import com.intellij.psi.PsiClass;
     20 import com.intellij.psi.PsiMethod;
     21 import com.squareup.javapoet.TypeSpec;
     22 
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 
     26 import javax.lang.model.element.Modifier;
     27 
     28 /**
     29  * Representation of a class for the support library to implement. Note that the class should not
     30  * necessarily contain all of the members of the class we are providing a compatibility version of.
     31  */
     32 public class ClassRepr {
     33     /**
     34      * List of methods to implement in the support library version of this class.
     35      */
     36     public final List<MethodRepr> methods;
     37 
     38     /**
     39      * Reference to the original class we want to supplement.
     40      */
     41     public final PsiClass psiClass;
     42 
     43     public ClassRepr(List<MethodRepr> methods, PsiClass psiClass) {
     44         this.methods = methods;
     45         this.psiClass = psiClass;
     46     }
     47 
     48     /**
     49      * Helper method for creating a ClassRepr containing all of the methods in the original Android
     50      * class {@param psiClass}.
     51      */
     52     public static ClassRepr fromPsiClass(PsiClass psiClass) {
     53         List<MethodRepr> methods = new ArrayList<>();
     54         for (PsiMethod psiMethod : psiClass.getMethods()) {
     55             methods.add(MethodRepr.fromPsiMethod(psiMethod));
     56         }
     57         return new ClassRepr(methods, psiClass);
     58     }
     59 
     60 
     61     /**
     62      * Create a Boundary Interface from this ClassRepr.
     63      */
     64     public TypeSpec createBoundaryInterface() {
     65         TypeSpec.Builder interfaceBuilder =
     66                 TypeSpec.interfaceBuilder(this.psiClass.getName() + "BoundaryInterface")
     67                         .addModifiers(Modifier.PUBLIC);
     68         for (MethodRepr methodRepr : this.methods) {
     69             interfaceBuilder.addMethod(methodRepr.createBoundaryInterfaceMethodDeclaration());
     70         }
     71         return interfaceBuilder.build();
     72     }
     73 }
     74