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 java.util.Objects; 19 import org.eclipse.jdt.core.dom.ASTNode; 20 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 21 import org.eclipse.jdt.core.dom.BodyDeclaration; 22 import org.eclipse.jdt.core.dom.CompilationUnit; 23 import org.eclipse.jdt.core.dom.FieldDeclaration; 24 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * Locates the {@link org.eclipse.jdt.core.dom.BodyDeclaration} associated with an field 31 * declaration. 32 */ 33 public final class FieldLocator implements BodyDeclarationLocator { 34 35 static final String LOCATOR_TYPE_NAME = "field"; 36 37 private final TypeLocator typeLocator; 38 39 private final String fieldName; 40 FieldLocator(String packageName, String typeName, String fieldName)41 public FieldLocator(String packageName, String typeName, String fieldName) { 42 this(new TypeLocator(packageName, typeName), fieldName); 43 } 44 FieldLocator(TypeLocator typeLocator, String fieldName)45 public FieldLocator(TypeLocator typeLocator, String fieldName) { 46 this.typeLocator = typeLocator; 47 this.fieldName = fieldName; 48 } 49 getTypeLocator()50 @Override public TypeLocator getTypeLocator() { 51 return typeLocator; 52 } 53 54 @Override matches(BodyDeclaration node)55 public boolean matches(BodyDeclaration node) { 56 if (node instanceof FieldDeclaration) { 57 FieldDeclaration fieldDeclaration = (FieldDeclaration) node; 58 for (VariableDeclarationFragment variableDeclarationFragment 59 : (List<VariableDeclarationFragment>) fieldDeclaration.fragments()) { 60 String nodeFieldName = variableDeclarationFragment.getName().getFullyQualifiedName(); 61 if (nodeFieldName.equals(fieldName)) { 62 ASTNode parentNode = node.getParent(); 63 // Parent could be an AnonymousClassDeclaration which is not a BodyDeclaration so check 64 // first. 65 if (parentNode instanceof BodyDeclaration) { 66 BodyDeclaration parentDeclarationNode = (BodyDeclaration) parentNode; 67 return typeLocator.matches(parentDeclarationNode); 68 } 69 } 70 } 71 } 72 return false; 73 } 74 75 @Override find(CompilationUnit cu)76 public FieldDeclaration find(CompilationUnit cu) { 77 AbstractTypeDeclaration typeDeclaration = typeLocator.find(cu); 78 if (typeDeclaration == null) { 79 return null; 80 } 81 for (BodyDeclaration bodyDeclaration 82 : (List<BodyDeclaration>) typeDeclaration.bodyDeclarations()) { 83 if (bodyDeclaration instanceof FieldDeclaration) { 84 FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration; 85 for (VariableDeclarationFragment variableDeclarationFragment 86 : (List<VariableDeclarationFragment>) fieldDeclaration.fragments()) { 87 String nodeFieldName = variableDeclarationFragment.getName().getFullyQualifiedName(); 88 if (nodeFieldName.equals(fieldName)) { 89 return fieldDeclaration; 90 } 91 } 92 } 93 } 94 return null; 95 } 96 getStringFormType()97 @Override public String getStringFormType() { 98 return LOCATOR_TYPE_NAME; 99 } 100 getStringFormTarget()101 @Override public String getStringFormTarget() { 102 return typeLocator.getStringFormTarget() + "#" + fieldName; 103 } 104 105 /** 106 * Returns the names of the fields declared in the supplied {@code fieldDeclarationNode}. 107 */ getFieldNames(FieldDeclaration fieldDeclarationNode)108 public static List<String> getFieldNames(FieldDeclaration fieldDeclarationNode) { 109 List<VariableDeclarationFragment> fieldDeclarations = fieldDeclarationNode.fragments(); 110 List<String> fieldNames = new ArrayList<>(fieldDeclarations.size()); 111 for (VariableDeclarationFragment fieldDeclaration : fieldDeclarations) { 112 fieldNames.add(fieldDeclaration.getName().getIdentifier()); 113 } 114 return fieldNames; 115 } 116 117 @Override equals(Object o)118 public boolean equals(Object o) { 119 if (this == o) { 120 return true; 121 } 122 if (!(o instanceof FieldLocator)) { 123 return false; 124 } 125 FieldLocator that = (FieldLocator) o; 126 return Objects.equals(typeLocator, that.typeLocator) && 127 Objects.equals(fieldName, that.fieldName); 128 } 129 130 @Override hashCode()131 public int hashCode() { 132 return Objects.hash(typeLocator, fieldName); 133 } 134 135 @Override toString()136 public String toString() { 137 return "FieldLocator{" + 138 "typeLocator=" + typeLocator + 139 ", fieldName='" + fieldName + '\'' + 140 '}'; 141 } 142 } 143