List of usage examples for java.lang Class getPackage
public Package getPackage()
From source file:org.assertj.assertions.generator.util.ClassUtil.java
/** * Gets the simple name of the class but, unlike {@link Class#getSimpleName()}, it includes the name of the outer * class when <code>clazz</code> is an inner class, both class names are concatenated. * <p>// www . j a v a2 s . c o m * Example: * * <pre> * Outer.Inner -> OuterInner * </pre> * * @param clazz * @return */ public static String getSimpleNameWithOuterClassNotSeparatedByDots(Class<?> clazz) { if (isNotNestedClass(clazz)) { return clazz.getSimpleName(); } String nestedClassName = null; nestedClassName = clazz.getName(); nestedClassName = nestedClassName.substring(clazz.getPackage().getName().length() + 1); nestedClassName = StringUtils.remove(nestedClassName, '$'); return nestedClassName; }
From source file:com.wit.android.support.fragment.manage.BaseFragmentFactory.java
/** * Creates a tag for fragment in the required format depends on a package name of the passed * <var>classOfFactory</var> and <var>fragmentName</var>. * <p>//from w w w.j ava2 s .c o m * Example format: <u>com.android.app.fragment.ProfileFragments.TAG.EditProfile</u> * <p> * - where <b>com.android.app.fragment</b> is name of the package where is the specified * <var>classOfFactory</var> placed, <b>ProfileFragments</b> is factory class name, <b>EditProfile</b> * is <var>fragmentName</var> and <b>TAG</b> is tag identifier. * * @param classOfFactory Class of factory which provides fragment with the given name. * @param fragmentName Fragment name (can be fragment class name) for which tag should be created. * @return Fragment tag in required format, or {@code ""} if <var>fragmentName</var> is * {@code null} or empty. */ @Nullable public static String createFragmentTag( @NonNull Class<? extends FragmentController.FragmentFactory> classOfFactory, @NonNull String fragmentName) { if (TextUtils.isEmpty(fragmentName)) { return null; } return classOfFactory.getPackage().getName() + "." + classOfFactory.getSimpleName() + ".TAG." + fragmentName; }
From source file:eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation.java
/** * Determine the locales a resource is available for. * /*from www .j a v a 2s. c om*/ * @param clazz the clazz the resource resides next to * @param baseName the base name of the resource * @param suffix the suffix of the resource file, e.g. * <code>properties</code> * @param defaultLocale the default locale to be assumed for an unqualified * resource * @return the set of locales the resource is available for * @throws IOException if an error occurs trying to determine the resource * files */ public static Set<Locale> findLocales(final Class<?> clazz, final String baseName, final String suffix, Locale defaultLocale) throws IOException { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver( clazz.getClassLoader()); String pkg = clazz.getPackage().getName().replaceAll("\\.", "/"); String pattern = pkg + "/" + baseName + "*." + suffix; return Arrays.stream(resolver.getResources(pattern)).map(resource -> { String fileName = resource.getFilename(); if (fileName != null && fileName.startsWith(baseName)) { fileName = fileName.substring(baseName.length()); if (fileName.endsWith("." + suffix)) { if (fileName.length() == suffix.length() + 1) { // default locale file return defaultLocale; } else { String localeIdent = fileName.substring(0, fileName.length() - suffix.length() - 1); String language = ""; String country = ""; String variant = ""; String[] parts = localeIdent.split("_"); int index = 0; if (parts.length > index && parts[index].isEmpty()) { index++; } if (parts.length > index) { language = parts[index++]; } if (parts.length > index) { country = parts[index++]; } if (parts.length > index) { variant = parts[index++]; } return new Locale(language, country, variant); } } else { log.error("Invalid resource encountered"); return null; } } else { log.error("Invalid resource encountered"); return null; } }).filter(locale -> locale != null).collect(Collectors.toSet()); }
From source file:com.eryansky.common.utils.io.FileUtil.java
@SuppressWarnings("rawtypes") public static String getAppPath(Class cls) { // ??// ww w.j a va 2 s . c o m if (cls == null) throw new java.lang.IllegalArgumentException("???"); ClassLoader loader = cls.getClassLoader(); // ???? String clsName = cls.getName() + ".class"; // ? Package pack = cls.getPackage(); String path = ""; // ????? if (pack != null) { String packName = pack.getName(); // ??JavaJDK if (packName.startsWith("java.") || packName.startsWith("javax.")) throw new java.lang.IllegalArgumentException("????"); // ?????? clsName = clsName.substring(packName.length() + 1); // ????????? if (packName.indexOf(".") < 0) path = packName + "/"; else {// ??????? int start = 0, end = 0; end = packName.indexOf("."); while (end != -1) { path = path + packName.substring(start, end) + "/"; start = end + 1; end = packName.indexOf(".", start); } path = path + packName.substring(start) + "/"; } } // ClassLoadergetResource???? java.net.URL url = loader.getResource(path + clsName); // URL?? String realPath = url.getPath(); // ?????"file:" int pos = realPath.indexOf("file:"); if (pos > -1) realPath = realPath.substring(pos + 5); // ???? pos = realPath.indexOf(path + clsName); realPath = realPath.substring(0, pos - 1); // JARJAR?? if (realPath.endsWith("!")) realPath = realPath.substring(0, realPath.lastIndexOf("/")); /*------------------------------------------------------------ ClassLoadergetResourceutf-8?? ??? URLDecoderdecode? ? -------------------------------------------------------------*/ try { realPath = java.net.URLDecoder.decode(realPath, "utf-8"); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("realPath----->" + realPath); return realPath; }
From source file:com.eryansky.common.utils.io.FileUtils.java
@SuppressWarnings("rawtypes") public static String getAppPath(Class cls) { // ??/*from www . j a va 2s . c o m*/ if (cls == null) throw new IllegalArgumentException("???"); ClassLoader loader = cls.getClassLoader(); // ???? String clsName = cls.getName() + ".class"; // ? Package pack = cls.getPackage(); String path = ""; // ????? if (pack != null) { String packName = pack.getName(); // ??JavaJDK if (packName.startsWith("java.") || packName.startsWith("javax.")) throw new IllegalArgumentException("????"); // ?????? clsName = clsName.substring(packName.length() + 1); // ????????? if (packName.indexOf(".") < 0) path = packName + "/"; else {// ??????? int start = 0, end = 0; end = packName.indexOf("."); while (end != -1) { path = path + packName.substring(start, end) + "/"; start = end + 1; end = packName.indexOf(".", start); } path = path + packName.substring(start) + "/"; } } // ClassLoadergetResource???? java.net.URL url = loader.getResource(path + clsName); // URL?? String realPath = url.getPath(); // ?????"file:" int pos = realPath.indexOf("file:"); if (pos > -1) realPath = realPath.substring(pos + 5); // ???? pos = realPath.indexOf(path + clsName); realPath = realPath.substring(0, pos - 1); // JARJAR?? if (realPath.endsWith("!")) realPath = realPath.substring(0, realPath.lastIndexOf("/")); /*------------------------------------------------------------ ClassLoadergetResourceutf-8?? ??? URLDecoderdecode? ? -------------------------------------------------------------*/ try { realPath = java.net.URLDecoder.decode(realPath, "utf-8"); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("realPath----->" + realPath); return realPath; }
From source file:org.ebayopensource.turmeric.plugins.maven.AbstractTurmericMojo.java
private static String getPackageVersion(Class<?> clazz) { Package p = clazz.getPackage(); if (p == null) { return null; }/* w w w.j av a 2 s . c o m*/ String ver = p.getImplementationVersion(); if (ver == null) { return null; } return ver; }
From source file:org.seasar.mayaa.impl.util.StringUtil.java
protected static String getMessage(Class clazz, int index, String[] params) { Package key = clazz.getPackage(); Properties properties = (Properties) _propFiles.get(key); if (properties == null) { ClassLoaderSourceDescriptor source = new ClassLoaderSourceDescriptor(); source.setSystemID("message.properties"); source.setNeighborClass(clazz);//from ww w . j ava 2s . c o m properties = new Properties(); _propFiles.put(key, properties); if (source.exists()) { InputStream stream = source.getInputStream(); try { properties.load(stream); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtil.close(stream); } } } String className = ObjectUtil.getSimpleClassName(clazz); StringBuffer propertyName = new StringBuffer(className); if (index > 0) { propertyName.append(".").append(index); } String message = properties.getProperty(propertyName.toString()); if (isEmpty(message)) { message = "!" + clazz.getName() + "!"; } if (params == null) { params = ZERO; } return MessageFormat.format(message, params); }
From source file:Main.java
/** * @param clazz//from w w w .jav a 2s.c o m * @return namespace of root element qname or null if this is not object does not represent a * root element */ public static QName getXmlRootElementQName(Class<?> clazz) { // See if the object represents a root element XmlRootElement root = (XmlRootElement) getAnnotation(clazz, XmlRootElement.class); if (root == null) { return null; } String name = root.name(); String namespace = root.namespace(); // The name may need to be defaulted if (name == null || name.length() == 0 || name.equals("##default")) { name = getSimpleName(clazz.getCanonicalName()); } // The namespace may need to be defaulted if (namespace == null || namespace.length() == 0 || namespace.equals("##default")) { Package pkg = clazz.getPackage(); XmlSchema schema = (XmlSchema) getAnnotation(pkg, XmlSchema.class); if (schema != null) { namespace = schema.namespace(); } else { namespace = ""; } } return new QName(namespace, name); }
From source file:org.topazproject.otm.metadata.AnnotationClassMetaFactory.java
private static String getName(Class<?> clazz) { String name = clazz.getName(); Package p = clazz.getPackage(); if (p != null) name = name.substring(p.getName().length() + 1); return name;/*from w w w.j a v a2 s. c om*/ }
From source file:com.cisco.oss.foundation.logging.structured.AbstractFoundationLoggingMarker.java
private static <T extends FoundationLoggingMarker> void buildClassPrefix(Class<T> markerClass, StringBuilder builder) {/*from w ww . j a v a2s.c om*/ builder.append("\npackage ").append(markerClass.getPackage().getName()).append(";\n\n"); builder.append("import ").append(FoundationLoggingMarkerFormatter.class.getName()).append(";\n"); builder.append("import ").append(FoundationLoggingMarker.class.getName()).append(";\n"); // builder.append("import ").append(FoundationLoggingEvent.class.getName()).append(";\n"); builder.append("public class ").append(markerClass.getSimpleName()).append("Formatter") .append(" implements FoundationLoggingMarkerFormatter ").append(" {\n\n"); builder.append("private FoundationLoggingMarker loggingMarker;\n\n"); builder.append(" \n@Override\n" + " public void setMarker(FoundationLoggingMarker marker) {\r\n" + " this.loggingMarker = marker;\r\n" + " }"); builder.append("\n@Override\npublic String getFormat(String appenderName) {\n") .append(markerClass.getSimpleName()).append(" marker = (").append(markerClass.getSimpleName()) .append(")loggingMarker;\n"); }