/** * This function find the first matching filename for a Java class file from * the classpath, if none is found it returns null. * * @param className * the class to get * @return the filename for the class */ public static String findClasspathEntry(String className) { String classpath = System.getProperty("java.class.path", ""); return findClasspathEntry(className, classpath); }
/** * Get the Class that owns the function * * @param level * Number of calling function * @return the function owner */ public Class<?> getClass(int level) { try { return ClassUtil.forName(classNames[level]); } catch (ClassNotFoundException ex) { assert false : ex; return null; } }
/** * Gets the class name minus the package name from a <code>Class</code>. * * @param cls * the class to get the short name for, must not be * <code>null</code> * @return the class name without the package name * @throws IllegalArgumentException * if the class is <code>null</code> */ public static String getShortClassName(Class<?> cls) { if (cls == null) { throw new IllegalArgumentException("The class must not be null"); } return getShortClassName(cls.getName()); }
@Override public Object convertToObject(String orig) { try { return ClassUtil.forName(orig); } catch (ClassNotFoundException ex) { log.warn("Class not found: {}", orig, ex); return null; } }
/** * Gets the class name minus the package name for an <code>Object</code>. * * @param object * the class to get the short name for, may be null * @param valueIfNull * the value to return if null * @return the class name of the object without the package name, or the * null value */ public static String getShortClassName(Object object, String valueIfNull) { if (object == null) { return valueIfNull; } return getShortClassName(object.getClass().getName()); }
/** * Build an object using its default constructor. Note: a constructor that * takes a boolean needs a type of boolean.class, but a parameter of type * Boolean. Likewise for other primitives. If this is needed, do not call * this method. * * @param <T> the type of the object to construct * @param className * the full class name of the object * @return the constructed object * @throws ReflectiveOperationException * if this {@code className} represents an abstract class, * an interface, an array class, a primitive type, or void; * or if the class has no reachable nullary constructor; * or if the instantiation fails for some other reason. */ public static <T> T construct(String className) throws ReflectiveOperationException { return construct((Class<T>) ClassUtil.forName(className)); }
/** * Get and load a properties file from the writable area or if that fails * from the classpath (where a default ought to be stored) * * @param <T> the type of the resource * @param clazz * The name of the desired resource * @return The found and loaded properties file * @throws IOException * if the resource can not be loaded */ public static <T> PropertyMap getProperties(Class<T> clazz) throws IOException { return getProperties(clazz, ClassUtil.getShortClassName(clazz)); }
try { clazz = ClassUtil.forName("com.sun.org.apache.xalan.internal.xslt.Process"); main = clazz.getMethod("_main", new Class[] { String[].class}); } catch (ClassNotFoundException e) { try { clazz = ClassUtil.forName("org.apache.xalan.xslt.Process"); main = clazz.getMethod("main", new Class[] { String[].class}); } catch (ClassNotFoundException ex) {
private ResourceBundle getLocalisedResources() { Class<? extends MsgBase> implementingClass = getClass(); String className = implementingClass.getName(); String shortClassName = ClassUtil.getShortClassName(className); Locale currentUserLocale = LocaleProviderManager.getLocale(); Map<String, ResourceBundle> localisedResourceMap = getLazyLocalisedResourceMap(currentUserLocale); ResourceBundle resourceBundle = localisedResourceMap.get(className); if (resourceBundle == null) { resourceBundle = getResourceBundleForClass(implementingClass, className, shortClassName, currentUserLocale, localisedResourceMap); } // if for some reason, we are still looking at a null, then we can only do our best, which is to return the English Locale. if (resourceBundle == null) { resourceBundle = getResourceBundleForClass(implementingClass, className, shortClassName, Locale.ENGLISH, localisedResourceMap); } //if we're still looking at a null, there is definitely nothing else we can do, so throw an exception if (resourceBundle == null) { log.error("Missing resources: Locale={} class={}", currentUserLocale, className); throw new MissingResourceException("Unable to find the language resources.", className, shortClassName); } return resourceBundle; }
/** * Call a static method on a class given a string * * @param call * The text of the invocation, for example * "java.lang.String.getName" * @param params * For example new Object[] { ...} * @return whatever the method returs * @throws ClassNotFoundException if the class is not found * @throws NoSuchMethodException * the method does not exist * @throws IllegalAccessException * if this {@code Constructor} object * is enforcing Java language access control and the underlying * constructor is inaccessible. * @throws InvocationTargetException if the underlying constructor * throws an exception. */ public static Object invoke(String call, Object... params) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Split the call into class name and method name int lastDot = call.lastIndexOf('.'); String className = call.substring(0, lastDot); String methodName = call.substring(lastDot + 1); Class<?> clazz = ClassUtil.forName(className); return invoke(clazz, clazz, methodName, params); }
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { if (stack.isEmpty()) { DataPolice.report(book, key, "Ignoring end tag without corresponding start tag: " + getName()); return; } Object pop = stack.removeFirst(); if (!(pop instanceof Element)) { DataPolice.report(book, key, "expected to pop a Note, but found " + ClassUtil.getShortClassName(pop.getClass())); return; } Element note = (Element) pop; if (note.getContentSize() < 1) { Content top = stack.get(0); if (top instanceof Element) { Element ele = (Element) top; ele.removeContent(note); } } } }
InvocationTargetException, InstantiationException Class<T> clazz = (Class<T>) ClassUtil.forName(className); return construct(clazz, params);
/** * Get and load a plugin file by looking it up as a resource. * * @param <T> the implementor's type * @param clazz * The name of the desired resource * @return The found and loaded plugin file * @throws IOException * if the resource can not be loaded * @throws MissingResourceException * if the resource can not be found */ public static <T> PropertyMap getPlugin(Class<T> clazz) throws IOException { String subject = ClassUtil.getShortClassName(clazz); try { String lookup = subject + PluginUtil.EXTENSION_PLUGIN; InputStream in = ResourceUtil.getResourceAsStream(clazz, lookup); PropertyMap prop = new PropertyMap(); prop.load(in); return prop; } catch (MissingResourceException e) { return new PropertyMap(); } }
calledTypes = describeParameters(params); Class<T> clazz = (Class<T>) ClassUtil.forName(className); final Constructor<T> c = clazz.getConstructor(calledTypes); return c.newInstance(params);
String shortClassName = ClassUtil.getShortClassName(className); ResourceBundle resources = ResourceBundle.getBundle(shortClassName, locale, CWClassLoader.instance(BibleNames.class));
String name = props.get(key); try { Class<T> impl = (Class<T>) ClassUtil.forName(name); if (clazz.isAssignableFrom(impl)) { matches.add(impl);
try { String value = props.get(key); Class<T> impl = (Class<T>) ClassUtil.forName(value); if (clazz.isAssignableFrom(impl)) { matches.put(key, impl);
/** * Get the preferred implementor of some interface or abstract class. This * is currently done by looking up a plugins file by the name of the given * class, and assuming that the "default" key is an implementation of said * class. Warnings are given otherwise. * * @param <T> the implementor's type * @param clazz * The class or interface to find an implementation of. * @return The configured implementing class. * @throws MalformedURLException * if the plugin file can not be found * @throws IOException * if there is a problem reading the found file * @throws ClassNotFoundException * if the read contents are not found * @throws ClassCastException * if the read contents are not valid * @see PluginUtil#getImplementors(Class) */ public static <T> Class<T> getImplementor(Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { PropertyMap props = getPlugin(clazz); String name = props.get(DEFAULT); Class<T> impl = (Class<T>) ClassUtil.forName(name); if (!clazz.isAssignableFrom(impl)) { throw new ClassCastException(JSOtherMsg.lookupText("Class {0} does not implement {1}.", impl.getName(), clazz.getName())); } return impl; }
/** * Convert a String to a Map, without type checking * * @param data * the thing to convert * @return the converted data */ public static PropertyMap string2Map(String data) { PropertyMap commands = new PropertyMap(); String[] parts = StringUtil.split(data, " "); String entry = ""; for (int i = 0; i < parts.length; i++) { try { entry = parts[i]; int pos = entry.indexOf('='); String key = entry.substring(0, pos); String value = entry.substring(pos + 1); Class<?> clazz = ClassUtil.forName(value); if (clazz.isAssignableFrom(Object.class)) { assert false; } else { commands.put(key, value); } } catch (ClassNotFoundException ex) { log.warn("Invalid config file entry: {} System message: {}", entry, ex.getMessage()); Reporter.informUser(Convert.class, ex); } } return commands; }
/** * Get a ChoiceFactory by element. * * @param option * The element to check * @param configResources the resource bundle holding the option * @return One of the ChoiceTypes. * @throws StartupException if startup is not possible * @throws ReflectiveOperationException * if this {@code data} represents an abstract class, * an interface, an array class, a primitive type, or void; * or if the class has no reachable nullary constructor; * or if the instantiation fails for some other reason. */ public static Choice getChoice(Element option, ResourceBundle configResources) throws StartupException, ReflectiveOperationException { Class<Choice> clazz = null; String type = option.getAttributeValue("type"); if ("custom".equals(type)) { String clazzstr = option.getAttributeValue("class"); clazz = (Class<Choice>) ClassUtil.forName(clazzstr); } else { clazz = map.get(type); } Choice choice = ReflectionUtil.construct(clazz); choice.init(option, configResources); return choice; }