001/* 002 * Copyright (c) 2024 QOS.ch Sarl (Switzerland) 003 * All rights reserved. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining 006 * a copy of this software and associated documentation files (the 007 * "Software"), to deal in the Software without restriction, including 008 * without limitation the rights to use, copy, modify, merge, publish, 009 * distribute, sublicense, and/or sell copies of the Software, and to 010 * permit persons to whom the Software is furnished to do so, subject to 011 * the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be 014 * included in all copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 023 * 024 * 025 * 026 */ 027 028package ch.qos.logback.tyler.base.util; 029 030import ch.qos.logback.core.joran.util.StringToObjectConverter; 031import ch.qos.logback.core.spi.ContextAware; 032 033import java.nio.charset.Charset; 034 035public class StringToVariableStament { 036 037 public static String convertArg (Class<?> type) { 038 if (String.class.isAssignableFrom(type)) { 039 return "$S"; 040 } else if (Integer.TYPE.isAssignableFrom(type)) { 041 return "$N"; 042 } else if (Long.TYPE.isAssignableFrom(type)) { 043 return "$N"; 044 } else if (Float.TYPE.isAssignableFrom(type)) { 045 return "$N"; 046 } else if (Double.TYPE.isAssignableFrom(type)) { 047 return "$N"; 048 } else if (Boolean.TYPE.isAssignableFrom(type)) { 049 return "$N"; 050 } else if (type.isEnum()) { 051 return "Enum.valueOf("+type.getName()+", $S)"; 052 } else if (StringToObjectConverter.followsTheValueOfConvention(type)) { 053 return type.getName()+".valueOf($S)"; 054 } else if (isOfTypeCharset(type)) { 055 return "Charset.forName($S)"; 056 } 057 058 return null; 059 } 060 061 static private boolean isOfTypeCharset(Class<?> type) { 062 return Charset.class.isAssignableFrom(type); 063 } 064 065}