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