1 /* 2 * Copyright (c) 2022 Uber Technologies, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 * THE SOFTWARE. 21 */ 22 23 package com.uber.nullaway.fixserialization.location; 24 25 import com.sun.tools.javac.code.Symbol; 26 import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; 27 28 /** Provides method for symbol locations. */ 29 public interface SymbolLocation { 30 31 /** 32 * returns string representation of contents of the instance. It must have the format below: kind 33 * of the element, symbol of the containing class, symbol of the enclosing method, symbol of the 34 * variable, index of the element and uri to containing file. 35 * 36 * @param adapter adapter used to serialize symbols. 37 * @return string representation of contents in a line seperated by tabs. 38 */ tabSeparatedToString(SerializationAdapter adapter)39 String tabSeparatedToString(SerializationAdapter adapter); 40 41 /** 42 * Creates header of an output file containing all {@link SymbolLocation} written in string which 43 * values are separated tabs. 44 * 45 * @return string representation of the header separated by tabs. 46 */ header()47 static String header() { 48 return String.join("\t", "kind", "class", "method", "param", "index", "uri"); 49 } 50 51 /** 52 * returns the appropriate subtype of {@link SymbolLocation} based on the target kind. 53 * 54 * @param target Target element. 55 * @return subtype of {@link SymbolLocation} matching target's type. 56 */ createLocationFromSymbol(Symbol target)57 static SymbolLocation createLocationFromSymbol(Symbol target) { 58 switch (target.getKind()) { 59 case PARAMETER: 60 return new MethodParameterLocation(target); 61 case METHOD: 62 return new MethodLocation(target); 63 case FIELD: 64 return new FieldLocation(target); 65 default: 66 throw new IllegalArgumentException("Cannot locate node: " + target); 67 } 68 } 69 } 70