Example usage for java.lang Class getPackage

List of usage examples for java.lang Class getPackage

Introduction

In this page you can find the example usage for java.lang Class getPackage.

Prototype

public Package getPackage() 

Source Link

Document

Gets the package of this class.

Usage

From source file:org.apache.axis2.jaxws.runtime.description.marshal.impl.PackageSetBuilder.java

/**
 * Return the package associated with the class name.  The className may not be specified (in
 * which case a null Package is returned). if class has unnamed package return ""
 *
 * @param cls Class//from  w  w w .  j  ava2s. c om
 * @return String or null 
 */
public static String getPackageFromClass(Class cls) {
    String pkgName = null;
    if (cls == null) {
        pkgName = null;
    } else if (cls.isArray()) {
        pkgName = getPackageFromClass(cls.getComponentType());
    } else if (cls.isPrimitive()) {
        pkgName = null;
    } else {
        pkgName = (cls.getPackage() == null) ? "" : cls.getPackage().getName();
    }
    return pkgName;
}

From source file:org.makersoft.mvc.builder.PackageBasedUrlPathBuilder.java

/**
 * Determines the namespace(s) for the action based on the action class. If there is a {
 * Namespace} annotation on the class (including parent classes) or on the package that the
 * class is in, than it is used. Otherwise, the Java package name that the class is in is used
 * in conjunction with either the <b>struts.convention.action.packages</b> or
 * <b>struts.convention.package.locators</b> configuration values. These are used to determine
 * which part of the Java package name should be converted into the namespace for the XWork
 * PackageConfig.//from  w w w.j  a  va 2  s .  c om
 * 
 * @param actionClass
 *            The action class.
 * @return The namespace or an empty string.
 */
private String determineControllerNamespace(Class<?> controllerClass) {
    String urlPath;

    String pkg = controllerClass.getPackage().getName();
    String pkgPart = null;
    if (controllerPackages != null) {
        for (String actionPackage : controllerPackages) {
            if (pkg.startsWith(actionPackage)) {
                pkgPart = controllerClass.getName().substring(actionPackage.length() + 1);
            }
        }
    }

    if (pkgPart == null && packageLocators != null) {
        for (String packageLocator : packageLocators) {
            int index = pkg.lastIndexOf(packageLocator);

            // This ensures that the match is at the end, beginning or has a dot on each side of it
            if (index >= 0 && (index + packageLocator.length() == pkg.length() || index == 0
                    || (pkg.charAt(index - 1) == '.' && pkg.charAt(index + packageLocator.length()) == '.'))) {
                pkgPart = controllerClass.getName().substring(index + packageLocator.length() + 1);
            }
        }
    }

    if (pkgPart != null) {
        final int indexOfDot = pkgPart.lastIndexOf('.');
        if (indexOfDot >= 0) {
            // String convertedNamespace = this.build(pkgPart.substring(0, indexOfDot));
            String convertedNamespace = pkgPart.substring(0, indexOfDot);
            urlPath = "/" + convertedNamespace.replace('.', '/');
            return urlPath;
        }
    }

    return "";
}

From source file:jatoo.app.AppTexts.java

public AppTexts(Class<?> clazz, Log logger, AppTexts fallback) {

    this.clazz = clazz;
    this.logger = logger;
    this.fallback = fallback;

    try {//from   w w  w.  j  a v a 2  s  .c  o m
        resourceBundle = ResourceBundle.getBundle(clazz.getPackage().getName() + ".texts");
    }

    catch (Throwable e) {
        resourceBundle = null;
        logger.error("no texts for class: " + clazz.getName(), e);
    }
}

From source file:com.jivesoftware.os.routing.bird.server.JerseyEndpoints.java

@Override
public Handler getHandler(final Server server, String context, String applicationName) {

    ResourceConfig rc = new ResourceConfig();

    if (enableSwagger) {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setResourcePackage(resourcePackage);
        beanConfig.setScan(true);// w  ww .  jav  a  2 s. co  m
        beanConfig.setBasePath("/");
        beanConfig.setTitle(applicationName);

        Set<String> packages = new HashSet<>();
        packages.add(ApiListingResource.class.getPackage().getName());
        for (Class<?> clazz : allClasses) {
            packages.add(clazz.getPackage().getName());
        }
        rc.packages(packages.toArray(new String[0]));
    }

    rc.registerClasses(allClasses);
    rc.register(HttpMethodOverrideFilter.class);
    rc.register(new JacksonFeature().withMapper(mapper));
    rc.register(MultiPartFeature.class); // adds support for multi-part API requests
    rc.registerInstances(allBinders);
    rc.registerInstances(new InjectableBinder(allInjectables), new AbstractBinder() {
        @Override
        protected void configure() {
            bind(server).to(Server.class);
        }
    });

    if (supportCORS) {
        rc.register(CorsContainerResponseFilter.class);
    }

    for (ContainerRequestFilter containerRequestFilter : containerRequestFilters) {
        rc.register(containerRequestFilter);
    }

    for (ContainerResponseFilter containerResponseFilter : containerResponseFilters) {
        rc.register(containerResponseFilter);
    }

    ServletHolder servletHolder = new ServletHolder(new ServletContainer(rc));
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    servletContextHandler.setContextPath(context);
    if (!applicationName.isEmpty()) {
        servletContextHandler.setDisplayName(applicationName);
    }
    servletContextHandler.addServlet(servletHolder, "/*");
    servletContextHandler.addFilter(NewRelicRequestFilter.class, "/", EnumSet.of(DispatcherType.REQUEST));

    return servletContextHandler;
}

From source file:org.neo4j.harness.internal.AbstractInProcessServerBuilder.java

@Override
public TestServerBuilder withExtension(String mountPath, Class<?> extension) {
    return withExtension(mountPath, extension.getPackage().getName());
}

From source file:org.openflexo.sg.generator.SGJavaClassGenerator.java

public void addImport(Class<?> neededImport) {
    if (!neededImport.isPrimitive() && neededImport.getPackage() != Package.getPackage("java.lang")) {
        addImport(neededImport.getName());
    }//from   www  .ja  va 2  s . c  o m
}

From source file:edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper.java

@SuppressWarnings("rawtypes")
@Override/*  www . j av  a 2 s .  com*/
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) {

    // How to define a setter? This is a weak approximation: a method whose name
    // starts with "set" or returns void.
    if (method.getName().startsWith("set")) {
        decision.setExposeMethodAs(null);

    } else if (method.getReturnType().getName().equals("void")) {
        decision.setExposeMethodAs(null);

    } else {

        Class<?> declaringClass = method.getDeclaringClass();
        if (declaringClass.equals(java.lang.Object.class)) {
            decision.setExposeMethodAs(null);

        } else {
            Package pkg = declaringClass.getPackage();
            if (pkg.getName().equals("java.util")) {
                decision.setExposeMethodAs(null);
            }
        }
    }
}

From source file:com.sqewd.open.dal.services.DataServices.java

private boolean showSchema(StructEntityReflect enref, List<String> packages) throws Exception {
    Class<?> type = Class.forName(enref.Class);
    String pname = type.getPackage().getName();
    if (packages.contains(pname))
        return true;
    return false;
}

From source file:axiom.util.Logging.java

public synchronized Log getInstance(Class clazz) {
    return getInstance(clazz.getPackage().getName());
}

From source file:org.kuali.rice.core.api.util.ShadowingInstrumentableClassLoader.java

private Class<?> loadAndInstrumentClass(String name) throws ClassNotFoundException {
    String internalName = StringUtils.replace(name, ".", "/") + ".class";
    attempted.add(name);// ww w.j  a v  a 2  s.co m
    attempted.add(internalName);
    InputStream is = this.enclosingClassLoader.getResourceAsStream(internalName);
    if (is == null) {
        throw new ClassNotFoundException(name);
    }
    byte[] bytes;
    try {
        bytes = ByteStreams.toByteArray(is);
    } catch (IOException e) {
        throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", e);
    } finally {
        Closeables.closeQuietly(is);
    }
    bytes = weavingTransformer.transformIfNecessary(name, bytes);
    Class<?> cls = defineClass(name, bytes, 0, bytes.length);
    // Additional check for defining the package, if not defined yet.
    if (cls.getPackage() == null) {
        int packageSeparator = name.lastIndexOf('.');
        if (packageSeparator != -1) {
            String packageName = name.substring(0, packageSeparator);
            definePackage(packageName, null, null, null, null, null, null, null);
        }
    }
    this.classCache.put(name, cls);
    return cls;
}