All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.ClassExplorer Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;


import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

//------------------------------------------------------------------------------
/**
 General utility for exploring java classes.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class ClassExplorer { private Map> mIsAssignableFromMap; //########################################################################### // PUBLIC METHODS //########################################################################### //-------------------------------------------------------------------------- public List findExtendingClasses(Class inBaseClass) throws IOException { List extendingClassnames = new ArrayList<>(20); recursiveFindExtendingClasses(inBaseClass.getName(), extendingClassnames); return extendingClassnames; } //########################################################################### // PRIVATE METHODS //########################################################################### //-------------------------------------------------------------------------- private void recursiveFindExtendingClasses(String inBaseClass, List inExtendingClasses) throws IOException { Set extendingClasses = getIsAssignableFromMap().get(inBaseClass); if (extendingClasses != null) { inExtendingClasses.addAll(extendingClasses); for (String classname : extendingClasses) { recursiveFindExtendingClasses(classname, inExtendingClasses); } } } //-------------------------------------------------------------------------- private synchronized Map> getIsAssignableFromMap() throws IOException { if (null == mIsAssignableFromMap) { mIsAssignableFromMap = buildCache(); } return mIsAssignableFromMap; } //-------------------------------------------------------------------------- private Map> buildCache() throws IOException { Map> assignableFromMap = new HashMap<>(); final StandardJavaFileManager fileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null); for (JavaFileObject javaFileObj : fileManager.list(StandardLocation.CLASS_PATH, "", Collections.singleton(JavaFileObject.Kind.CLASS), true)) { if (javaFileObj.getName().contains("ct.sym(")) { // Don't process the linking lib continue; } DataInputStream stream = null; try { stream = new DataInputStream(javaFileObj.openInputStream()); BasicClassFileInfo classInfo = new BasicClassFileInfo(stream); if (! classInfo.getSuperClassname().equals(Object.class.getName())) { Set extendingOrImplementingClasses = assignableFromMap.get(classInfo.getSuperClassname()); if (null == extendingOrImplementingClasses) { extendingOrImplementingClasses = new HashSet<>(10); assignableFromMap.put(classInfo.getSuperClassname(), extendingOrImplementingClasses); } extendingOrImplementingClasses.add(classInfo.getClassname()); } String[] interfaces = classInfo.getInterfaceClassnames(); if (interfaces != null) { for (String interfaceName : interfaces) { Set extendingOrImplementingClasses = assignableFromMap.get(interfaceName); if (null == extendingOrImplementingClasses) { extendingOrImplementingClasses = new HashSet<>(5); assignableFromMap.put(interfaceName, extendingOrImplementingClasses); } extendingOrImplementingClasses.add(classInfo.getClassname()); } } } catch (Throwable e) { // System.out.println(fullClassName); } finally { if (stream != null) { stream.close(); } } } return assignableFromMap; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy