Java tutorial
/* * Copyright (c) 2013 ITOCHU Techno-Solutions Corporation. * * 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. */ package jp.co.ctc_g.jfw.core.util; import java.util.ResourceBundle; import jp.co.ctc_g.jfw.core.internal.InternalMessages; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p> * ?????????????? * </p> * @author ITOCHU Techno-Solutions Corporation. */ public final class Classes { private static final Log L = LogFactory.getLog(Classes.class); private static final ResourceBundle R = InternalMessages.getBundle(Classes.class); private Classes() { } /** * ????? * ??????????? * ??????????????? * @return ? */ public static ClassLoader preferredClassLoader() { ClassLoader loader = null; try { loader = Thread.currentThread().getContextClassLoader(); } catch (SecurityException e) { // ??????????????? // ??????(???????) } if (loader == null) { loader = Classes.class.getClassLoader(); } return loader; } /** * ???????? * ???{@link #preferredClassLoader()}?????? * @param <T> ?? * @param className ???? * @return */ @SuppressWarnings("unchecked") public static <T> Class<T> forName(String className) { return (Class<T>) forName(className, preferredClassLoader()); } /** * ???????? * @param <T> ?? * @param className ???? * @param loader ? * @return */ @SuppressWarnings("unchecked") public static <T> Class<T> forName(String className, ClassLoader loader) { Args.checkNotBlank(className); loader = loader == null ? preferredClassLoader() : loader; Class<?> clazz = null; try { clazz = Class.forName(className, true, loader); } catch (ClassNotFoundException e) { if (L.isDebugEnabled()) { String message = Strings.substitute(R.getString("D-UTIL#0001"), Maps.hash("class", className)); L.debug(message); } } return (Class<T>) clazz; } /** * ??????????????? * ????????? * ?????? * <pre class="brush:java"> * Classes.howFar(Object.class, Object.class); // 0 * Classes.howFar(String.class, Object.class); // 1 * Classes.howFar(JPanel.class, Object.class); // 4 * Classes.howFar(JPanel.class, JComponent.class); // 1 * </pre> * ?????????<code>-1</code>????? * @param from ?? * @param to ?? * @return ?? */ public static int howFar(Class<?> from, Class<?> to) { Args.checkNotNull(from); Args.checkNotNull(to); if (from == to) return 0; if (!to.isAssignableFrom(from)) return -1; int distance = 0; Class<?> hierarchyExplorer = from; while (hierarchyExplorer != null && hierarchyExplorer != to) { hierarchyExplorer = hierarchyExplorer.getSuperclass(); if (hierarchyExplorer != null) { distance++; } else { // ??????????????? assert false; } } return distance; } }