001/* 002 * Copyright (C) 2022 - 2024, the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.github.ascopes.jct.repr; 017 018import io.github.ascopes.jct.diagnostics.TraceDiagnostic; 019import java.util.Locale; 020import org.apiguardian.api.API; 021import org.apiguardian.api.API.Status; 022import org.assertj.core.presentation.Representation; 023import org.jspecify.annotations.Nullable; 024 025/** 026 * Simplified representation of a diagnostic. 027 * 028 * @author Ashley Scopes 029 * @since 0.0.1, rewritten in 4.0.0 030 */ 031@API(since = "4.0.0", status = Status.STABLE) 032public final class TraceDiagnosticRepresentation implements Representation { 033 034 private static final TraceDiagnosticRepresentation INSTANCE 035 = new TraceDiagnosticRepresentation(); 036 037 /** 038 * Get an instance of this diagnostic representation. 039 * 040 * @return the instance. 041 */ 042 public static TraceDiagnosticRepresentation getInstance() { 043 return INSTANCE; 044 } 045 046 private TraceDiagnosticRepresentation() { 047 // Nothing to see here, move along now. 048 } 049 050 @Override 051 public String toStringOf(@Nullable Object object) { 052 if (object == null) { 053 return "null"; 054 } 055 056 var diagnostic = (TraceDiagnostic<?>) object; 057 058 var builder = new StringBuilder("[") 059 .append(diagnostic.getKind()) 060 .append("]"); 061 062 var code = diagnostic.getCode(); 063 if (code != null) { 064 builder.append(' ') 065 .append(code); 066 } 067 068 if (diagnostic.getSource() != null) { 069 builder 070 .append(" in ") 071 .append(diagnostic.getSource().getName()) 072 .append(" (line ") 073 .append(diagnostic.getLineNumber()) 074 .append(", col ") 075 .append(diagnostic.getColumnNumber()) 076 .append(")"); 077 } 078 079 return builder 080 .append("\n") 081 .append("\n") 082 .append(diagnostic.getMessage(Locale.ROOT)) 083 .toString(); 084 } 085}