Example usage for java.lang Class getSimpleName

List of usage examples for java.lang Class getSimpleName

Introduction

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

Prototype

public String getSimpleName() 

Source Link

Document

Returns the simple name of the underlying class as given in the source code.

Usage

From source file:com.taobao.itest.spring.context.SpringContextManager.java

public static void findAnnotatedMethodsAndInjectBeanByName(Class<? extends Annotation> annotationType,
        Object object, ApplicationContext applicationContext) {
    Set<Method> methods = getMethodsAnnotatedWith(object.getClass(), annotationType);
    for (Method method : methods) {
        try {// ww w. jav a  2 s.  co  m
            if (!isSetter(method)) {
                invokeMethod(object, method, getBean(method.getParameterTypes()[0], applicationContext));
            } else {
                invokeMethod(object, method, getBean(getPropertyName(method), applicationContext));
            }

        } catch (InvocationTargetException e) {
            throw new RuntimeException(
                    "Unable to assign the Spring bean value to method annotated with @"
                            + annotationType.getSimpleName() + ". Method " + "has thrown an exception.",
                    e.getCause());
        }
    }
}

From source file:com.evolveum.liferay.usercreatehook.ws.ModelPortWrapper.java

private static String getTypeUri(Class<? extends ObjectType> type) {
    // QName typeQName = JAXBUtil.getTypeQName(type);
    // String typeUri = QNameUtil.qNameToUri(typeQName);
    String typeUri = NS_COMMON + "#" + type.getSimpleName();
    return typeUri;
}

From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java

private static void addFields(Class entity, EntityDocModel docModel) {
    if (entity != null) {
        Field[] fields = entity.getDeclaredFields();
        for (Field field : fields) {
            //Skip static field
            if ((Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) == false) {

                EntityFieldModel fieldModel = new EntityFieldModel();
                fieldModel.setName(field.getName());
                fieldModel.setType(field.getType().getSimpleName());
                fieldModel.setOriginClass(entity.getSimpleName());
                fieldModel.setEmbeddedType(ReflectionUtil.isComplexClass(field.getType()));
                if (ReflectionUtil.isCollectionClass(field.getType())) {
                    DataType dataType = (DataType) field.getAnnotation(DataType.class);
                    if (dataType != null) {
                        String typeClass = dataType.value().getSimpleName();
                        if (StringUtils.isNotBlank(dataType.actualClassName())) {
                            typeClass = dataType.actualClassName();
                        }//from  w  w w  . ja  v a 2 s . c om
                        fieldModel.setGenericType(typeClass);
                    }
                }

                APIDescription description = (APIDescription) field.getAnnotation(APIDescription.class);
                if (description != null) {
                    fieldModel.setDescription(description.value());
                }

                for (Annotation annotation : field.getAnnotations()) {
                    if (annotation.annotationType().getName().equals(APIDescription.class.getName()) == false) {
                        EntityConstraintModel entityConstraintModel = new EntityConstraintModel();
                        entityConstraintModel.setName(annotation.annotationType().getSimpleName());

                        APIDescription annotationDescription = (APIDescription) annotation.annotationType()
                                .getAnnotation(APIDescription.class);
                        if (annotationDescription != null) {
                            entityConstraintModel.setDescription(annotationDescription.value());
                        }

                        //rules
                        Object annObj = field.getAnnotation(annotation.annotationType());
                        if (annObj instanceof NotNull) {
                            entityConstraintModel.setRules("Field is required");
                        } else if (annObj instanceof PK) {
                            PK pk = (PK) annObj;
                            entityConstraintModel.setRules("<b>Generated:</b> " + pk.generated());
                            fieldModel.setPrimaryKey(true);
                        } else if (annObj instanceof FK) {
                            FK fk = (FK) annObj;

                            StringBuilder sb = new StringBuilder();
                            sb.append("<b>Foreign Key:</b> ").append(fk.value().getSimpleName());
                            sb.append(" (<b>Enforce</b>: ").append(fk.enforce());
                            sb.append(" <b>Soft reference</b>: ").append(fk.softReference());
                            if (StringUtils.isNotBlank(fk.referencedField())) {
                                sb.append(" <b>Reference Field</b>: ").append(fk.referencedField());
                            }
                            sb.append(" )");
                            entityConstraintModel.setRules(sb.toString());
                        } else if (annObj instanceof ConsumeField) {
                            entityConstraintModel.setRules("");
                        } else if (annObj instanceof Size) {
                            Size size = (Size) annObj;
                            entityConstraintModel
                                    .setRules("<b>Min:</b> " + size.min() + " <b>Max:</b> " + size.max());
                        } else if (annObj instanceof Pattern) {
                            Pattern pattern = (Pattern) annObj;
                            entityConstraintModel.setRules("<b>Pattern:</b> " + pattern.regexp());
                        } else if (annObj instanceof Sanitize) {
                            Sanitize sanitize = (Sanitize) annObj;
                            entityConstraintModel
                                    .setRules("<b>Sanitize:</b> " + sanitize.value().getSimpleName());
                        } else if (annObj instanceof Unique) {
                            Unique unique = (Unique) annObj;
                            entityConstraintModel.setRules("<b>Handler:</b> " + unique.value().getSimpleName());
                        } else if (annObj instanceof ValidValueType) {
                            ValidValueType validValueType = (ValidValueType) annObj;
                            StringBuilder sb = new StringBuilder();
                            if (validValueType.value().length > 0) {
                                sb.append(" <b>Values:</b> ").append(Arrays.toString(validValueType.value()));
                            }

                            if (validValueType.lookupClass().length > 0) {
                                sb.append(" <b>Lookups:</b> ");
                                for (Class lookupClass : validValueType.lookupClass()) {
                                    sb.append(lookupClass.getSimpleName()).append("  ");
                                }
                            }

                            if (validValueType.enumClass().length > 0) {
                                sb.append(" <b>Enumerations:</b> ");
                                for (Class enumClass : validValueType.enumClass()) {
                                    sb.append(enumClass.getSimpleName()).append("  (");
                                    sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")");
                                }
                            }

                            entityConstraintModel.setRules(sb.toString());
                        } else if (annObj instanceof Min) {
                            Min min = (Min) annObj;
                            entityConstraintModel.setRules("<b>Min value:</b> " + min.value());
                        } else if (annObj instanceof Max) {
                            Max max = (Max) annObj;
                            entityConstraintModel.setRules("<b>Max value:</b> " + max.value());
                        } else if (annObj instanceof Version) {
                            entityConstraintModel.setRules("Entity version; For Multi-Version control");
                        } else if (annObj instanceof DataType) {
                            DataType dataType = (DataType) annObj;
                            String typeClass = dataType.value().getSimpleName();
                            if (StringUtils.isNotBlank(dataType.actualClassName())) {
                                typeClass = dataType.actualClassName();
                            }
                            entityConstraintModel.setRules("<b>Type:</b> " + typeClass);
                        } else {
                            entityConstraintModel.setRules(annotation.toString());
                        }

                        //Annotations that have related classes
                        if (annObj instanceof DataType) {
                            DataType dataType = (DataType) annObj;
                            entityConstraintModel.getRelatedClasses().add(dataType.value().getSimpleName());
                        }
                        if (annObj instanceof FK) {
                            FK fk = (FK) annObj;
                            entityConstraintModel.getRelatedClasses().add(fk.value().getSimpleName());
                        }
                        if (annObj instanceof ValidValueType) {
                            ValidValueType validValueType = (ValidValueType) annObj;
                            for (Class lookupClass : validValueType.lookupClass()) {
                                entityConstraintModel.getRelatedClasses().add(lookupClass.getSimpleName());
                            }

                            StringBuilder sb = new StringBuilder();
                            for (Class enumClass : validValueType.enumClass()) {
                                sb.append("<br>");
                                sb.append(enumClass.getSimpleName()).append(":  (");
                                sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")");
                            }
                            entityConstraintModel
                                    .setRules(entityConstraintModel.getRules() + " " + sb.toString());
                        }

                        fieldModel.getConstraints().add(entityConstraintModel);
                    }
                }

                docModel.getFieldModels().add(fieldModel);
            }
        }
        addFields(entity.getSuperclass(), docModel);
    }
}

From source file:com.ett.common.util.ReflectUtil.java

/**
 * @param object//from w  w  w .ja  v  a  2  s .  co m
 * @param methodName
 * @param params
 * @return
 * @throws NoSuchMethodException
 */
public static Object invokePrivateMethod(Object object, String methodName, Object... params)
        throws NoSuchMethodException {
    Assert.notNull(object);
    Assert.hasText(methodName);
    Class[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }

    Class clazz = object.getClass();
    Method method = null;
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {
            method = superClass.getDeclaredMethod(methodName, types);
            break;
        } catch (NoSuchMethodException e) {
            // ??17,?
        }
    }

    if (method == null)
        throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);

    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object result = null;
    try {
        result = method.invoke(object, params);
    } catch (Exception e) {
        ReflectionUtils.handleReflectionException(e);
    }
    method.setAccessible(accessible);
    return result;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param object//from   ww  w .j a va  2  s .co  m
 * @param methodName
 * @param params
 * @return
 * @throws NoSuchMethodException
 */
public static Object invokePrivateMethod(Object object, String methodName, Object... params)
        throws NoSuchMethodException {
    Assert.notNull(object);
    Assert.hasText(methodName);
    Class[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }

    Class clazz = object.getClass();
    Method method = null;
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {
            method = superClass.getDeclaredMethod(methodName, types);
            break;
        } catch (NoSuchMethodException e) {
            // ??17,?
        }
    }

    if (method == null)
        throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);

    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object result = null;
    try {
        result = method.invoke(object, params);
    } catch (Exception e) {
        ReflectionUtils.handleReflectionException(e);
    }
    method.setAccessible(accessible);
    return result;
}

From source file:com.hihframework.core.utils.ReflectUtil.java

/**
 * ??,Class?.//  w w  w .java 2  s.c  om
 * , Object.class.
 * 
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */

public static Class<?> getSuperClassGenricType(final Class<?> clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        // logger.warn(clazz.getSimpleName() +
        // "'s superclass not ParameterizedType");
        return Object.class;
    }

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class<?>) params[index];
}

From source file:com.github.jinahya.sql.database.metadata.bind.MetadataContext.java

private static String suppression(final Class<?> beanClass, final Field beanField) {

    return decapitalize(beanClass.getSimpleName()) + "/" + beanField.getName();
}

From source file:com.oltpbenchmark.util.ClassUtil.java

public static <T> T newInstance(Class<T> target_class, Object params[], Class<?> classes[]) {
    //        Class<?> const_params[] = new Class<?>[params.length];
    //        for (int i = 0; i < params.length; i++) {
    //            const_params[i] = params[i].getClass();
    //            System.err.println("[" + i + "] " + params[i] + " " + params[i].getClass());
    //        } // FOR

    Constructor<T> constructor = ClassUtil.getConstructor(target_class, classes);
    T ret = null;//w w  w. j  a  v a2s  .  c  om
    try {
        ret = constructor.newInstance(params);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to create new instance of " + target_class.getSimpleName(), ex);
    }
    return (ret);
}

From source file:fr.inria.atlanmod.neoemf.core.PersistentEObjectAdapter.java

/**
 * Returns the given {@code object} adapted in a specific {@code type}.
 *
 * @param adaptableObject the object to adapt
 * @param adapterType     the class in which the object must be adapted
 *
 * @return an adapted object in the given {@code type}, or {@code null} if the {@code object} cannot be assigned as
 * a {@code type}// ww  w . j a  v  a  2  s . co  m
 *
 * @throws NullPointerException if the {@code type} is {@code null}
 */
public static <T extends PersistentEObject> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (isNull(adaptableObject)) {
        return null;
    }
    checkNotNull(adapterType);

    Object adapter = null;
    if (adapterType.isInstance(adaptableObject)) {
        adapter = adaptableObject;
    } else if (adaptableObject instanceof InternalEObject) {
        adapter = ADAPTED_OBJECTS_CACHE.getIfPresent(adaptableObject);
        if (isNull(adapter) || !adapterType.isAssignableFrom(adapter.getClass())) {
            adapter = createAdapter(adaptableObject, adapterType);
            ADAPTED_OBJECTS_CACHE.put((InternalEObject) adaptableObject, (PersistentEObject) adapter);
        }
    }

    if (isNull(adapter)) {
        NeoLogger.warn("Unable to create a {0} adapter for this object of type {1}",
                adapterType.getSimpleName(), adaptableObject.getClass().getSimpleName());
    }

    return adapterType.cast(adapter);
}

From source file:com.codepunk.codepunklib.util.log.FormattingLogger.java

/**
 * Returns the concrete information from the given {@link StackTraceElement} that is represented
 * by the given Placeholder.//from  w  w w . j  a  va2 s  .  c  o m
 * @param element The {@link StackTraceElement}.
 * @param placeholder The {@link Placeholder} to replace.
 * @return The information represented by the Placeholder.
 */
private static Object getFromElement(StackTraceElement element, Placeholder placeholder) {
    try {
        switch (placeholder) {
        case FILE_NAME:
            return element.getFileName();
        case HASH_CODE:
            return element.hashCode();
        case LINE_NUMBER:
            return element.getLineNumber();
        case METHOD_NAME:
            return element.getMethodName();
        case PACKAGE: {
            return getClass(element).getPackage().getName();
        }
        case CLASS_NAME:
        case SIMPLE_CLASS_NAME:
        default: {
            String name;
            Class cls = getClass(element);
            do {
                if (cls == null) {
                    name = UNKNOWN_CLASS_NAME;
                } else if (placeholder == Placeholder.CLASS_NAME) {
                    name = cls.getName();
                } else {
                    name = cls.getSimpleName();
                }
            } while (TextUtils.isEmpty(name));
            return name;
        }
        }
    } catch (ClassNotFoundException e) {
        return UNKNOWN_CLASS_NAME;
    }
}