Home | History | Annotate | Download | only in ast
      1 /*
      2  * Copyright (C) 2015 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 com.google.currysrc.api.process.ast;
     17 
     18 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
     19 import org.eclipse.jdt.core.dom.BodyDeclaration;
     20 import org.eclipse.jdt.core.dom.CompilationUnit;
     21 import org.eclipse.jdt.core.dom.MethodDeclaration;
     22 
     23 import java.util.List;
     24 
     25 /**
     26  * Locates the {@link org.eclipse.jdt.core.dom.BodyDeclaration} associated with a method
     27  * declaration.
     28  */
     29 public final class MethodLocator implements BodyDeclarationLocator {
     30 
     31   static final String LOCATOR_TYPE_NAME = "method";
     32 
     33   private final TypeLocator typeLocator;
     34 
     35   private final String methodName;
     36 
     37   private final ParameterMatcher parameterMatcher;
     38 
     39   public MethodLocator(TypeLocator typeLocator, String methodName, List<String> parameterTypes) {
     40     this.typeLocator = typeLocator;
     41     this.methodName = methodName;
     42     this.parameterMatcher = new ParameterMatcher(parameterTypes);
     43   }
     44 
     45   @Override public TypeLocator getTypeLocator() {
     46     return typeLocator;
     47   }
     48 
     49   @Override
     50   public boolean matches(BodyDeclaration node) {
     51     if (node instanceof MethodDeclaration) {
     52       BodyDeclaration parentNode = (BodyDeclaration) node.getParent();
     53       MethodDeclaration methodDeclaration = (MethodDeclaration) node;
     54       return typeLocator.matches(parentNode)
     55           && methodDeclaration.getName().getFullyQualifiedName().equals(methodName)
     56           && parameterMatcher.matches(methodDeclaration);
     57     }
     58     return false;
     59   }
     60 
     61   @Override
     62   public MethodDeclaration find(CompilationUnit cu) {
     63     AbstractTypeDeclaration typeDeclaration = typeLocator.find(cu);
     64     if (typeDeclaration == null) {
     65       return null;
     66     }
     67     for (BodyDeclaration bodyDeclaration
     68         : (List<BodyDeclaration>) typeDeclaration.bodyDeclarations()) {
     69       if (bodyDeclaration instanceof MethodDeclaration) {
     70         MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
     71         if (methodDeclaration.getName().getFullyQualifiedName().equals(methodName)) {
     72           if (parameterMatcher.matches(methodDeclaration)) {
     73             return methodDeclaration;
     74           }
     75         }
     76       }
     77     }
     78     return null;
     79   }
     80 
     81   @Override public String getStringFormType() {
     82     return LOCATOR_TYPE_NAME;
     83   }
     84 
     85   @Override public String getStringFormTarget() {
     86     return typeLocator.getStringFormTarget() + "#" + methodName
     87         + "(" + parameterMatcher.toStringForm() + ")";
     88   }
     89 
     90   @Override
     91   public String toString() {
     92     return "MethodLocator{" +
     93         "typeLocator=" + typeLocator +
     94         ", methodName='" + methodName + '\'' +
     95         ", parameterMatcher=" + parameterMatcher +
     96         '}';
     97   }
     98 }
     99