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.EnumConstantDeclaration; 22 import org.eclipse.jdt.core.dom.EnumDeclaration; 23 24 import java.util.List; 25 26 /** 27 * Locates the {@link org.eclipse.jdt.core.dom.BodyDeclaration} associated with an enum 28 * declaration. 29 */ 30 public final class EnumConstantLocator implements BodyDeclarationLocator { 31 32 static final String LOCATOR_TYPE_NAME = "enumConstant"; 33 34 private final TypeLocator typeLocator; 35 36 private final String constantName; 37 38 public EnumConstantLocator(String packageName, String typeName, String constantName) { 39 this(new TypeLocator(packageName, typeName), constantName); 40 } 41 42 public EnumConstantLocator(TypeLocator typeLocator, String constantName) { 43 this.typeLocator = typeLocator; 44 this.constantName = constantName; 45 } 46 47 @Override public TypeLocator getTypeLocator() { 48 return typeLocator; 49 } 50 51 @Override 52 public boolean matches(BodyDeclaration node) { 53 if (!(node instanceof EnumConstantDeclaration)) { 54 return false; 55 } 56 57 if (typeLocator.matches((BodyDeclaration) node.getParent())) { 58 EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) node; 59 if (enumConstantDeclaration.getName().getFullyQualifiedName().equals(constantName)) { 60 return true; 61 } 62 } 63 return false; 64 } 65 66 @Override 67 public BodyDeclaration find(CompilationUnit cu) { 68 AbstractTypeDeclaration typeDeclaration = typeLocator.find(cu); 69 if (typeDeclaration == null || !(typeDeclaration instanceof EnumDeclaration)) { 70 return null; 71 } 72 for (EnumConstantDeclaration enumConstantDeclaration 73 : (List<EnumConstantDeclaration>) ((EnumDeclaration) typeDeclaration).enumConstants()) { 74 if (enumConstantDeclaration.getName().getFullyQualifiedName().equals(constantName)) { 75 return enumConstantDeclaration; 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() + "#" + constantName; 87 } 88 89 @Override 90 public String toString() { 91 return "EnumConstantLocator{" + 92 "typeLocator=" + typeLocator + 93 ", constantName='" + constantName + '\'' + 94 '}'; 95 } 96 } 97