1 /* 2 * Copyright (C) 2021 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.android.annotationvisitor; 17 18 import org.apache.bcel.classfile.FieldOrMethod; 19 import org.apache.bcel.classfile.JavaClass; 20 21 import java.util.Formatter; 22 import java.util.Locale; 23 24 /** 25 * Encapsulates context for a single annotation on a class member. 26 */ 27 public class AnnotatedMemberContext extends AnnotationContext { 28 29 public final FieldOrMethod member; 30 public final String signatureFormatString; 31 AnnotatedMemberContext( Status status, JavaClass definingClass, FieldOrMethod member, String signatureFormatString)32 public AnnotatedMemberContext( 33 Status status, 34 JavaClass definingClass, 35 FieldOrMethod member, 36 String signatureFormatString) { 37 super(status, definingClass); 38 this.member = member; 39 this.signatureFormatString = signatureFormatString; 40 } 41 42 @Override getMemberDescriptor()43 public String getMemberDescriptor() { 44 return String.format(Locale.US, signatureFormatString, 45 getClassDescriptor(), member.getName(), member.getSignature()); 46 } 47 buildReportString(String message, Object... args)48 private String buildReportString(String message, Object... args) { 49 Formatter error = new Formatter(); 50 error 51 .format("%s: %s.%s: ", definingClass.getSourceFileName(), 52 definingClass.getClassName(), member.getName()) 53 .format(Locale.US, message, args); 54 return error.toString(); 55 } 56 57 @Override reportError(String message, Object... args)58 public void reportError(String message, Object... args) { 59 status.error(buildReportString(message, args)); 60 } 61 62 @Override reportWarning(String message, Object... args)63 public void reportWarning(String message, Object... args) { 64 status.warning(buildReportString(message, args)); 65 } 66 } 67