Example usage for java.lang Class getCanonicalName

List of usage examples for java.lang Class getCanonicalName

Introduction

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

Prototype

public String getCanonicalName() 

Source Link

Document

Returns the canonical name of the underlying class as defined by the Java Language Specification.

Usage

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static String getPackageName(Class clazz) {
    // we can cheat and use the FilenameUtils to remove the class name
    return FilenameUtils.removeExtension(clazz.getCanonicalName());
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassUtil.java

/**
 *  class info map for LOGGER./*from   ww  w.j  a  v  a  2  s .com*/
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?? ,voidboolean?byte?char?short?int?long?float  double?
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ??,
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ????,?,?????
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class????java??false,?true,JVM???,java??????
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

From source file:Main.java

/**
 * @param clazz//from  w  w  w  .j a  v  a2 s.  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:com.sqewd.open.dal.core.persistence.db.EntityHelper.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setColumnValue(final ResultSet rs, final StructAttributeReflect attr,
        final AbstractEntity entity, final AbstractJoinGraph gr, final Stack<KeyValuePair<Class<?>>> path)
        throws Exception {

    KeyValuePair<String> alias = gr.getAliasFor(path, attr.Column, 0);
    String tabprefix = alias.getKey();

    if (EnumPrimitives.isPrimitiveType(attr.Field.getType())) {
        EnumPrimitives prim = EnumPrimitives.type(attr.Field.getType());
        switch (prim) {
        case ECharacter:
            String sv = rs.getString(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), sv.charAt(0));
            }//from  w ww. jav  a  2 s  . co  m
            break;
        case EShort:
            short shv = rs.getShort(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), shv);
            }
            break;
        case EInteger:
            int iv = rs.getInt(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), iv);
            }
            break;
        case ELong:
            long lv = rs.getLong(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), lv);
            }
            break;
        case EFloat:
            float fv = rs.getFloat(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), fv);
            }
            break;
        case EDouble:
            double dv = rs.getDouble(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dv);
            }
            break;
        default:
            throw new Exception("Unsupported Data type [" + prim.name() + "]");
        }
    } else if (attr.Convertor != null) {
        String value = rs.getString(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            attr.Convertor.load(entity, attr.Column, value);
        }
    } else if (attr.Field.getType().equals(String.class)) {
        String value = rs.getString(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), value);
        }
    } else if (attr.Field.getType().equals(Date.class)) {
        long value = rs.getLong(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            Date dt = new Date(value);
            PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dt);
        }
    } else if (attr.Field.getType().isEnum()) {
        String value = rs.getString(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            Class ecls = attr.Field.getType();
            Object evalue = Enum.valueOf(ecls, value);
            PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), evalue);
        }
    } else if (attr.Reference != null) {
        Class<?> rt = Class.forName(attr.Reference.Class);
        Object obj = rt.newInstance();
        if (!(obj instanceof AbstractEntity))
            throw new Exception("Unsupported Entity type [" + rt.getCanonicalName() + "]");
        AbstractEntity rentity = (AbstractEntity) obj;
        if (path.size() > 0) {
            path.peek().setKey(attr.Column);
        }

        KeyValuePair<Class<?>> cls = new KeyValuePair<Class<?>>();
        cls.setValue(rentity.getClass());
        path.push(cls);
        setEntity(rentity, rs, gr, path);
        PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), rentity);
        path.pop();
    }
}

From source file:com.sqewd.open.dal.core.persistence.db.EntityHelper.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object getColumnValue(final ResultSet rs, final StructAttributeReflect attr,
        final AbstractEntity entity, final AbstractJoinGraph gr, final Stack<KeyValuePair<Class<?>>> path)
        throws Exception {

    Object value = null;/*  w  w w.  j a  v a 2s  .  co m*/

    KeyValuePair<String> alias = gr.getAliasFor(path, attr.Column, 0);
    String tabprefix = alias.getKey();

    if (EnumPrimitives.isPrimitiveType(attr.Field.getType())) {
        EnumPrimitives prim = EnumPrimitives.type(attr.Field.getType());
        switch (prim) {
        case ECharacter:
            String sv = rs.getString(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), sv.charAt(0));
            }
            break;
        case EShort:
            short shv = rs.getShort(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), shv);
            }
            break;
        case EInteger:
            int iv = rs.getInt(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), iv);
            }
            break;
        case ELong:
            long lv = rs.getLong(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), lv);
            }
            break;
        case EFloat:
            float fv = rs.getFloat(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), fv);
            }
            break;
        case EDouble:
            double dv = rs.getDouble(tabprefix + "." + attr.Column);
            if (!rs.wasNull()) {
                PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dv);
            }
            break;
        default:
            throw new Exception("Unsupported Data type [" + prim.name() + "]");
        }
    } else if (attr.Convertor != null) {
        // TODO : Not supported at this time.
        value = rs.getString(tabprefix + "." + attr.Column);

    } else if (attr.Field.getType().equals(String.class)) {
        value = rs.getString(tabprefix + "." + attr.Column);
        if (rs.wasNull()) {
            value = null;
        }
    } else if (attr.Field.getType().equals(Date.class)) {
        long lvalue = rs.getLong(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            Date dt = new Date(lvalue);
            value = dt;
        }
    } else if (attr.Field.getType().isEnum()) {
        String svalue = rs.getString(tabprefix + "." + attr.Column);
        if (!rs.wasNull()) {
            Class ecls = attr.Field.getType();
            value = Enum.valueOf(ecls, svalue);
        }
    } else if (attr.Reference != null) {
        Class<?> rt = Class.forName(attr.Reference.Class);
        Object obj = rt.newInstance();
        if (!(obj instanceof AbstractEntity))
            throw new Exception("Unsupported Entity type [" + rt.getCanonicalName() + "]");
        AbstractEntity rentity = (AbstractEntity) obj;
        if (path.size() > 0) {
            path.peek().setKey(attr.Column);
        }

        KeyValuePair<Class<?>> cls = new KeyValuePair<Class<?>>();
        cls.setValue(rentity.getClass());
        path.push(cls);
        setEntity(rentity, rs, gr, path);
        value = rentity;
        path.pop();
    }
    return value;
}

From source file:com.palantir.atlasdb.table.description.ColumnValueDescription.java

public static ColumnValueDescription forPersister(Class<? extends Persister<?>> clazz,
        Compression compression) {
    return new ColumnValueDescription(Format.PERSISTER, clazz.getName(), clazz.getCanonicalName(), compression,
            null);/*from   ww  w  .jav a  2 s  .  co  m*/
}

From source file:com.palantir.atlasdb.table.description.ColumnValueDescription.java

public static ColumnValueDescription forProtoMessage(Class<? extends GeneratedMessage> clazz,
        Compression compression) {
    return new ColumnValueDescription(Format.PROTO, clazz.getName(), clazz.getCanonicalName(), compression,
            getDescriptor(clazz));//from w  ww . j a va  2s.co  m
}

From source file:com.sqewd.open.dal.core.persistence.DataManager.java

/**
 * Create a new instance of an AbstractEntity type. This method should be
 * used when creating new AbstarctEntity types.
 * /*from w  w w  .  j  a  v a  2 s  .c  o  m*/
 * @param type
 *            - Class (type) to create instance of.
 * 
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static <T extends AbstractEntity> T newInstance(final Class<?> type) throws Exception {
    Object obj = type.newInstance();
    if (!(obj instanceof AbstractEntity))
        throw new Exception("Invalid Class : [" + type.getCanonicalName() + "] does not extend ["
                + AbstractEntity.class.getCanonicalName() + "]");

    ((AbstractEntity) obj).setState(EnumEntityState.New);

    return (T) obj;
}

From source file:com.link_intersystems.beans.BeanClass.java

/**
 * Constructs a new {@link BeanClass} for the specified clazz. The given
 * class must fulfill the java bean specification and declare a public
 * default constructor.//from   w  w w. j a  v  a2  s .  c o  m
 *
 * @param clazz
 * @return a {@link Class2} for the given {@link Class}.
 * @throws IllegalArgumentException
 *             if the clazz argument does not declare a public default
 *             constructor.
 * @since 1.2.0.0
 */
@SuppressWarnings("unchecked")
public static <T> BeanClass<T> get(Class<T> clazz) {
    Assert.notNull("clazz", clazz);
    BeanClass<T> class2 = (BeanClass<T>) CLASS_TO_BEANCLASS.get(clazz);
    if (class2 == null) {
        if (!hasBeanConstructor(clazz)) {
            throw new IllegalArgumentException(
                    "Class " + clazz.getCanonicalName() + " does not declare a public default constructor "
                            + "and therefore does not fulfill the bean specification");
        }
        class2 = new BeanClass<T>(clazz);
        CLASS_TO_BEANCLASS.put(clazz, class2);
    }
    return class2;
}

From source file:org.web4thejob.util.CoreUtil.java

public static Query getQuery(Class<? extends Entity> targetType, String name) {
    Query lookup = ContextUtil.getEntityFactory().buildQuery(Query.class);
    lookup.addCriterion(new Path(Query.FLD_FLAT_TARGET_TYPE), Condition.EQ, targetType.getCanonicalName());
    lookup.addCriterion(new Path(Query.FLD_NAME), Condition.EQ, name);
    lookup.setCached(true);/*from   w  w  w.j  av a 2  s . c  om*/
    return ContextUtil.getDRS().findUniqueByQuery(lookup);
}