Java tutorial
//package com.java2s; /* * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Same as {@link #getShortClassName(Class, boolean) getShortClassName(pClass, false)}. * * @since 2.3.20 */ public static String getShortClassName(Class pClass) { return getShortClassName(pClass, false); } /** * Returns a class name without "java.lang." and "java.util." prefix, also shows array types in a format like * {@code int[]}; useful for printing class names in error messages. * * @param pClass can be {@code null}, in which case the method returns {@code null}. * @param shortenFreeMarkerClasses if {@code true}, it will also shorten FreeMarker class names. The exact rules * aren't specified and might change over time, but right now, {@code freemarker.ext.beans.NumberModel} for * example becomes to {@code f.e.b.NumberModel}. * * @since 2.3.20 */ public static String getShortClassName(Class pClass, boolean shortenFreeMarkerClasses) { if (pClass == null) { return null; } else if (pClass.isArray()) { return getShortClassName(pClass.getComponentType()) + "[]"; } else { String cn = pClass.getName(); if (cn.startsWith("java.lang.") || cn.startsWith("java.util.")) { return cn.substring(10); } else { if (shortenFreeMarkerClasses) { if (cn.startsWith("freemarker.template.")) { return "f.t" + cn.substring(19); } else if (cn.startsWith("freemarker.ext.beans.")) { return "f.e.b" + cn.substring(20); } else if (cn.startsWith("freemarker.core.")) { return "f.c" + cn.substring(15); } else if (cn.startsWith("freemarker.ext.")) { return "f.e" + cn.substring(14); } else if (cn.startsWith("freemarker.")) { return "f" + cn.substring(10); } // Falls through } return cn; } } } }