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.ASTNode;
     19 import org.eclipse.jdt.core.dom.CompilationUnit;
     20 import org.eclipse.jdt.core.dom.PackageDeclaration;
     21 
     22 /**
     23  * Matches the package name associated with ASTNodes.
     24  */
     25 public final class PackageMatcher {
     26 
     27   protected final String packageName;
     28 
     29   public PackageMatcher(String packageName) {
     30     this.packageName = packageName;
     31   }
     32 
     33   public boolean matches(CompilationUnit cu) {
     34     PackageDeclaration aPackage = cu.getPackage();
     35     return matches(aPackage);
     36   }
     37 
     38   public boolean matches(PackageDeclaration packageDeclaration) {
     39     String cuPackageName = packageDeclaration.getName().getFullyQualifiedName();
     40     return cuPackageName.equals(packageName);
     41   }
     42 
     43   @Override
     44   public String toString() {
     45     return "PackageMatcher{" +
     46         "packageName='" + packageName + '\'' +
     47         '}';
     48   }
     49 
     50   public String toStringForm() {
     51     return packageName;
     52   }
     53 
     54   /**
     55    * Returns the package name of the compilation unit associated with the supplied node.
     56    */
     57   public static String getPackageName(ASTNode node) {
     58     CompilationUnit compilationUnit = (CompilationUnit) node.getRoot();
     59     PackageDeclaration packageDeclaration = compilationUnit.getPackage();
     60     if (packageDeclaration == null) {
     61       return "";
     62     }
     63     return packageDeclaration.getName().getFullyQualifiedName();
     64   }
     65 }
     66