Example usage for org.apache.commons.lang3 ClassUtils getPackageName

List of usage examples for org.apache.commons.lang3 ClassUtils getPackageName

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ClassUtils getPackageName.

Prototype

public static String getPackageName(String className) 

Source Link

Document

Gets the package name from a String .

The string passed in is assumed to be a class name - it is not checked.

If the class is unpackaged, return an empty string.

Usage

From source file:io.manasobi.commons.logger.CommonLogger.java

private static String parseLogPrifix(Class clazz) {

    String packageName = ClassUtils.getPackageName(clazz);

    if (packageName.startsWith("io.manasobi.commons")) {
        return "[commons] ";
    } else {//from   www .  ja v  a  2  s.co m
        return "[license] ";
    }
}

From source file:com.xylocore.copybook.generator.CopybookClassGenerator.java

/**
 * FILLIN//w  w w  .ja  v a  2  s.  c  o m
 */
public void generate() {
    validateEnvironment();

    try {
        processCopybook();

        Metadata myMetadata = environment.getMetadata();
        String myPackageName = ClassUtils.getPackageName(myMetadata.getClassName());
        String myClassName = ClassUtils.getShortClassName(myMetadata.getClassName());
        File myGenerationRootDirectory = new File(environment.getGenerationRootDirectory());

        File myOutputDirectory = StringUtils.isNotEmpty(myPackageName)
                ? new File(myGenerationRootDirectory, myPackageName.replace('.', File.separatorChar))
                : myGenerationRootDirectory;
        myOutputDirectory.mkdirs();

        File myOutputFile = new File(myOutputDirectory, myClassName + ".java");

        try (FileWriter myWriter = new FileWriter(myOutputFile)) {
            selectElementsOfInterest();

            copybookClassEmitter.generate(environment, copybook, elementsOfInterest, myWriter);
        } catch (IOException myIOException) {
            // TODO: throw an appropriate exception
        }
    } catch (Exception myException) {
        // TODO: throw an appropriate exception
        throw new RuntimeException(myException.getMessage(), myException);
    }
}

From source file:hoot.services.models.osm.ElementFactory.java

/**
 * Creates an element/*from   w  ww .j  av a2s.  c  o  m*/
 *
 * @param elementType the type of element to create
 * @param conn JDBC Connection
 * @return an element
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public Element create(final long mapId, final ElementType elementType, Connection conn)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException {
    Long oMapId = new Long(mapId);
    return (Element) ConstructorUtils.invokeConstructor(
            Class.forName(ClassUtils.getPackageName(ElementFactory.class) + "." + elementType.toString()),
            new Object[] { oMapId, conn }, new Class<?>[] { Long.class, Connection.class });
}

From source file:de.unentscheidbar.validation.DefaultMessageTextGetterTest.java

@Test
public void testDefaultMessageTexts() {

    Locale[] locales = { Locale.ROOT, Locale.GERMAN };

    Reflections r = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage(ClassUtils.getPackageName(Validators.class)))
            .setScanners(new ResourcesScanner(), new SubTypesScanner(false)));

    Collection<Class<? extends Id>> defaultMessageClasses = Collections2.filter(
            r.getSubTypesOf(ValidationMessage.Id.class), Predicates.and(IS_NOT_ABSTRACT, IsTestClass.NO));
    Assert.assertTrue("Suspiciously few message ID classes found", defaultMessageClasses.size() > 10);

    MessageTextGetter mtg = Validation.defaultMessageTextGetter();

    for (Class<? extends Id> idClass : defaultMessageClasses) {
        /* only works with enums atm (all default message ids are enums) */
        if (!idClass.isEnum()) {
            Assert.fail("Cannot test " + idClass.getName());
        }//from  w ww .jav a 2 s. c  om
        for (Id id : idClass.getEnumConstants()) {
            for (Locale locale : locales) {
                Assert.assertTrue("Missing default message text for " + locale + " -> "
                        + id.getClass().getName() + " -> " + id.name(), mtg.hasText(id, locale));
            }
        }
    }
}

From source file:com.norconex.commons.lang.time.DurationUtil.java

private static String getString(Locale locale, long unitValue, final String key, boolean islong) {
    String k = key;/*from w w  w .  j ava 2s .c  om*/
    if (islong && unitValue > 1) {
        k += "s";
    }
    String pkg = ClassUtils.getPackageName(DurationUtil.class);
    ResourceBundle time;
    if (locale != null && Locale.FRENCH.getLanguage().equals(locale.getLanguage())) {
        time = ResourceBundle.getBundle(pkg + ".time", Locale.FRENCH);
    } else {
        time = ResourceBundle.getBundle(pkg + ".time");
    }
    if (islong) {
        return " " + unitValue + " " + time.getString("timeunit.long." + k);
    }
    return unitValue + time.getString("timeunit.short." + k);
}

From source file:com.wavemaker.tools.apidocs.tools.spring.SpringSwaggerParserTest.java

@Test
public void testGenerate() throws Exception {
    FilterableClassScanner classScanner = new FilterableClassScanner();
    final String packageName = "com.wavemaker.tools.apidocs.tools";
    classScanner.includePackage(packageName);
    Info info = new Info();
    info.description("hrdb database service apis");
    info.setTitle("HRDB service");
    SwaggerConfiguration.Builder builder = new SwaggerConfiguration.Builder("/test", classScanner);
    builder.setInfo(info);//ww  w .j ava2  s .  c om
    builder.setClassLoader(this.getClass().getClassLoader());
    SwaggerParser runner = new SpringSwaggerParser(builder.build());
    Swagger swagger = runner.generate();
    assertNotNull(swagger);
    assertFalse(swagger.getTags().isEmpty());
    assertEquals(NO_OF_CONTROLLERS, swagger.getTags().size());

    for (final Tag tag : swagger.getTags()) {
        final String fullyQualifiedName = tag.getFullyQualifiedName();
        final String classPackage = ClassUtils.getPackageName(fullyQualifiedName);
        assertTrue(classPackage.startsWith(packageName + "."));
    }
    writeToFile(swagger, "swagger.json");
}

From source file:hoot.services.models.osm.ElementFactory.java

/**
 * Creates an element from a new element record
 *
 * @param elementType the type of element to create
 * @param record record to associate with the element
 * @param conn JDBC Connection//  www  .  j  a  v  a 2  s . c om
 * @return an element
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 */
public Element create(final ElementType elementType, final Object record, Connection conn, final long mapId)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException,
        ClassNotFoundException, Exception {
    Object oRec = record;
    Object oElem = oRec;
    if (oRec instanceof Tuple) {
        // This was forced since we are using reflection which need to be refactored to something more solid.

        Tuple tRec = (Tuple) oRec;
        Object[] tRecs = tRec.toArray();
        if (tRecs.length > 0) {
            // assume first record is good.
            oElem = tRecs[0];
        } else {
            throw new Exception(
                    "Bad Record type. Tuple is empty. Please make sure the first object is tuple is DTO that supports setVersion.");
        }
    }
    Long oMapId = new Long(mapId);
    String className = ClassUtils.getPackageName(ElementFactory.class) + "." + elementType.toString();
    return (Element) ConstructorUtils.invokeConstructor(
            Class.forName(ClassUtils.getPackageName(ElementFactory.class) + "." + elementType.toString()),
            new Object[] { oMapId, conn, oElem },
            new Class<?>[] { Long.class, Connection.class, oElem.getClass() });
}

From source file:de.unentscheidbar.validation.swing.DefaultMessageTextGetter.java

private String getBundleName(ValidationMessage.Id messageId) {

    String bundleName = ClassUtils.getPackageName(messageId.getClass());
    bundleName = bundleName.isEmpty() ? "Messages" : bundleName + ".Messages";
    return bundleName;
}

From source file:com.wavemaker.tools.apidocs.tools.spring.SpringSwaggerParserTest.java

@Test
public void testGenerateWithPackagePrefix() throws Exception {
    FilterableClassScanner classScanner = new FilterableClassScanner();
    final String packageName = "com.wavemaker.tools.apidocs.tools.spring.controller";
    classScanner.includePackage(packageName);
    Info info = new Info();
    info.description("hrdb database service apis");
    info.setTitle("HRDB service");
    SwaggerConfiguration.Builder builder = new SwaggerConfiguration.Builder("/test", classScanner);
    builder.setInfo(info);/*  w  w w. j a  va  2s  .c om*/
    builder.setClassLoader(this.getClass().getClassLoader());
    SwaggerParser runner = new SpringSwaggerParser(builder.build());
    Swagger swagger = runner.generate();

    assertFalse(swagger.getTags().isEmpty());
    for (final Tag tag : swagger.getTags()) {
        final String fullyQualifiedName = tag.getFullyQualifiedName();
        final String classPackage = ClassUtils.getPackageName(fullyQualifiedName);
        assertEquals(packageName, classPackage);
    }
}

From source file:hoot.services.models.osm.ElementFactory.java

/**
 * Creates an element by querying for its element record
 *
 * This method assumes the element already exists in the services database.
 *
 * @param mapId ID of the map owning the element
 * @param elementType the type of element to create
 * @param elementId the ID of the element being created
 * @param dbConn JDBC Connection//from w  ww .  j  a  v  a2s.co  m
 * @return an element
 * @throws Exception
 */
public Element create(final long mapId, final ElementType elementType, final long elementId, Connection dbConn)
        throws Exception {
    Long oMapId = new Long(mapId);
    Element element = (Element) ConstructorUtils.invokeConstructor(
            Class.forName(ClassUtils.getPackageName(ElementFactory.class) + "." + elementType.toString()),
            new Object[] { oMapId, dbConn }, new Class<?>[] { Long.class, Connection.class });

    Object record = new SQLQuery(dbConn, DbUtils.getConfiguration(mapId)).from(element.getElementTable())
            .where(element.getElementIdField().eq(elementId)).singleResult(element.getElementTable());

    if (record != null) {
        element.setRecord(record);
    } else {
        throw new Exception("No ecord exists for map with ID: " + mapId + " and element ID: " + elementId
                + " and type: " + elementType.toString());
    }
    return element;
}