List of usage examples for java.lang Class getSimpleName
public String getSimpleName()
From source file:com.discovery.darchrow.lang.ClassUtil.java
/** * class info map for LOGGER./*www.j a v a2 s . c o m*/ * * @param klass * the clz * @return the map for log */ public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) { if (Validator.isNullOrEmpty(klass)) { return null; } Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern" map.put("clz.getName()", klass.getName());//"com.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?trueJVM???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:edu.ku.brc.specify.ui.DBObjDialogFactory.java
/** * @param lockTitle/*from w w w . j a v a 2s . c o m*/ * @param view * @return */ public static boolean unLockOK(final String lockTitle, final ViewIFace view) { Class<?> treeDefClass = ((SpecifyAppContextMgr) AppContextMgr.getInstance()).getTreeDefClass(view); if (treeDefClass != null) { String treeSemaphoreName = treeDefClass.getSimpleName(); String treeFormSemaphoreName = treeDefClass.getSimpleName() + "Form"; DBTableInfo tableInfo = DBTableIdMgr.getInstance().getByClassName(view.getClassName()); String title = tableInfo.getTitle() + " " + "Tree"; if (BaseTreeBusRules.ALLOW_CONCURRENT_FORM_ACCESS) { return TaskSemaphoreMgr.decrementUsageCount(title, treeSemaphoreName, SCOPE.Discipline); } // If this user owns the Tree Form Lock then they can open the View if (!TaskSemaphoreMgr.doesOwnSemaphore(treeFormSemaphoreName, TaskSemaphoreMgr.SCOPE.Discipline)) { return false; } if (TaskSemaphoreMgr.unlock(title, treeFormSemaphoreName, TaskSemaphoreMgr.SCOPE.Discipline)) { if (TaskSemaphoreMgr.unlock(title, treeSemaphoreName, TaskSemaphoreMgr.SCOPE.Discipline)) { return true; } return false; } return false; } return true; }
From source file:com.appassit.common.Utils.java
/** * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from opaque to translucent following a call to {@link #convertActivityFromTranslucent(android.app.Activity)} . * <p>/* www. j a v a2s .c om*/ * Calling this allows the Activity behind this one to be seen again. Once all such Activities have been redrawn * <p> * This call has no effect on non-translucent activities or on activities with the {@link android.R.attr#windowIsFloating} attribute. */ public static void convertActivityToTranslucent(Activity activity) { try { Class<?>[] classes = Activity.class.getDeclaredClasses(); Class<?> translucentConversionListenerClazz = null; for (Class clazz : classes) { if (clazz.getSimpleName().contains("TranslucentConversionListener")) { translucentConversionListenerClazz = clazz; } } Method method = Activity.class.getDeclaredMethod("convertToTranslucent", translucentConversionListenerClazz); method.setAccessible(true); method.invoke(activity, new Object[] { null }); } catch (Throwable t) { } }
From source file:ReflectUtil.java
/** * <p>A better (more concise) toString method for annotation types that yields a String * that should look more like the actual usage of the annotation in a class. The String produced * is similar to that produced by calling toString() on the annotation directly, with the * following differences:</p>//from w ww. j ava2 s . c o m * * <ul> * <li>Uses the classes simple name instead of it's fully qualified name.</li> * <li>Only outputs attributes that are set to non-default values.</li> * * <p>If, for some unforseen reason, an exception is thrown within this method it will be * caught and the return value will be {@code ann.toString()}. * * @param ann the annotation to convert to a human readable String * @return a human readable String form of the annotation and it's attributes */ public static String toString(Annotation ann) { try { Class<? extends Annotation> type = ann.annotationType(); StringBuilder builder = new StringBuilder(128); builder.append("@"); builder.append(type.getSimpleName()); boolean appendedAnyParameters = false; Method[] methods = type.getMethods(); for (Method method : methods) { if (!INHERITED_ANNOTATION_METHODS.contains(method.getName())) { Object defaultValue = method.getDefaultValue(); Object actualValue = method.invoke(ann); // If we have arrays, they have to be treated a little differently Object[] defaultArray = null, actualArray = null; if (Object[].class.isAssignableFrom(method.getReturnType())) { defaultArray = (Object[]) defaultValue; actualArray = (Object[]) actualValue; } // Only print an attribute if it isn't set to the default value if ((defaultArray != null && !Arrays.equals(defaultArray, actualArray)) || (defaultArray == null && !actualValue.equals(defaultValue))) { if (appendedAnyParameters) { builder.append(", "); } else { builder.append("("); } builder.append(method.getName()); builder.append("="); if (actualArray != null) { builder.append(Arrays.toString(actualArray)); } else { builder.append(actualValue); } appendedAnyParameters = true; } } } if (appendedAnyParameters) { builder.append(")"); } return builder.toString(); } catch (Exception e) { return ann.toString(); } }
From source file:org.apache.flume.sink.hdfs.HDFSEventSink.java
private static boolean codecMatches(Class<? extends CompressionCodec> cls, String codecName) { String simpleName = cls.getSimpleName(); if (cls.getName().equals(codecName) || simpleName.equalsIgnoreCase(codecName)) { return true; }//from w ww .java 2 s. c o m if (simpleName.endsWith("Codec")) { String prefix = simpleName.substring(0, simpleName.length() - "Codec".length()); if (prefix.equalsIgnoreCase(codecName)) { return true; } } return false; }
From source file:com.mine.core.util.ReflectUtils.java
/** * ??, Class?. , Object.class.//from www .j av a 2 s .c o m * <p> * 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 getClassGenricType(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: {}, Size of {}'s Parameterized Type: {}", index, clazz.getSimpleName(), 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.mine.core.util.ReflectUtils.java
/** * ??, Class?. , Object.class.//w w w . j a v a 2s . c o m * <p> * 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 */ @SuppressWarnings("rawtypes") 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: {}, Size of {}'s Parameterized Type: {}", index, clazz.getSimpleName(), 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.twinsoft.convertigo.beans.CheckBeans.java
private static void analyzeJavaClass(String javaClassName) { try {//www . ja v a 2s . c o m Class<?> javaClass = Class.forName(javaClassName); String javaClassSimpleName = javaClass.getSimpleName(); if (!DatabaseObject.class.isAssignableFrom(javaClass)) { //Error.NON_DATABASE_OBJECT.add(javaClassName); return; } nBeanClass++; String dboBeanInfoClassName = javaClassName + "BeanInfo"; MySimpleBeanInfo dboBeanInfo = null; try { dboBeanInfo = (MySimpleBeanInfo) (Class.forName(dboBeanInfoClassName)).newInstance(); } catch (ClassNotFoundException e) { if (!Modifier.isAbstract(javaClass.getModifiers())) { Error.MISSING_BEAN_INFO .add(javaClassName + " (expected bean info: " + dboBeanInfoClassName + ")"); } return; } catch (Exception e) { e.printStackTrace(); return; } BeanDescriptor beanDescriptor = dboBeanInfo.getBeanDescriptor(); // Check abstract class if (Modifier.isAbstract(javaClass.getModifiers())) { // Check icon (16x16) String declaredIconName = MySimpleBeanInfo.getIconName(dboBeanInfo, MySimpleBeanInfo.ICON_COLOR_16x16); if (declaredIconName != null) { Error.ABSTRACT_CLASS_WITH_ICON.add(javaClassName); } // Check icon (32x32) declaredIconName = MySimpleBeanInfo.getIconName(dboBeanInfo, MySimpleBeanInfo.ICON_COLOR_32x32); if (declaredIconName != null) { Error.ABSTRACT_CLASS_WITH_ICON.add(javaClassName); } // Check display name if (!beanDescriptor.getDisplayName().equals("?")) { Error.ABSTRACT_CLASS_WITH_DISPLAY_NAME.add(javaClassName); } // Check description if (!beanDescriptor.getShortDescription().equals("?")) { Error.ABSTRACT_CLASS_WITH_DESCRIPTION.add(javaClassName); } } else { nBeanClassNotAbstract++; // Check bean declaration in database_objects.xml if (!dboXmlDeclaredDatabaseObjects.contains(javaClassName)) { Error.BEAN_DEFINED_BUT_NOT_USED.add(javaClassName); } // Check icon name policy (16x16) String declaredIconName = MySimpleBeanInfo.getIconName(dboBeanInfo, MySimpleBeanInfo.ICON_COLOR_16x16); String expectedIconName = javaClassName.replace(javaClassSimpleName, "images/" + javaClassSimpleName); expectedIconName = "/" + expectedIconName.replace('.', '/') + "_color_16x16"; expectedIconName = expectedIconName.toLowerCase() + ".png"; if (declaredIconName != null) { if (!declaredIconName.equals(expectedIconName)) { Error.BEAN_ICON_NAMING_POLICY.add(javaClassName + "\n" + " Declared: " + declaredIconName + "\n" + " Expected: " + expectedIconName); } } // Check icon file (16x16) File iconFile = new File(srcBase + declaredIconName); if (!iconFile.exists()) { Error.BEAN_MISSING_ICON.add(javaClassName + " - icon missing: " + declaredIconName); } else { icons.remove(declaredIconName); } // Check icon name policy (32x32) declaredIconName = MySimpleBeanInfo.getIconName(dboBeanInfo, MySimpleBeanInfo.ICON_COLOR_32x32); expectedIconName = javaClassName.replace(javaClassSimpleName, "images/" + javaClassSimpleName); expectedIconName = "/" + expectedIconName.replace('.', '/') + "_color_32x32"; expectedIconName = expectedIconName.toLowerCase() + ".png"; if (declaredIconName != null) { if (!declaredIconName.equals(expectedIconName)) { Error.BEAN_ICON_NAMING_POLICY.add(javaClassName + "\n" + " Declared: " + declaredIconName + "\n" + " Expected: " + expectedIconName); } } // Check icon file (32x32) iconFile = new File(srcBase + declaredIconName); if (!iconFile.exists()) { Error.BEAN_MISSING_ICON.add(javaClassName + " - icon missing: " + declaredIconName); } else { icons.remove(declaredIconName); } // Check display name if (beanDescriptor.getDisplayName().equals("?")) { Error.BEAN_MISSING_DISPLAY_NAME.add(javaClassName); } // Check description if (beanDescriptor.getShortDescription().equals("?")) { Error.BEAN_MISSING_DESCRIPTION.add(javaClassName); } } // Check declared bean properties PropertyDescriptor[] propertyDescriptors = dboBeanInfo.getLocalPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String propertyName = propertyDescriptor.getName(); try { javaClass.getDeclaredField(propertyName); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { try { // Try to find it in the upper classes javaClass.getField(propertyName); } catch (SecurityException e1) { // printStackTrace(); } catch (NoSuchFieldException e1) { Error.PROPERTY_DECLARED_BUT_NOT_FOUND.add(javaClassName + ": " + propertyName); } } } Method[] methods = javaClass.getDeclaredMethods(); List<Method> listMethods = Arrays.asList(methods); List<String> listMethodNames = new ArrayList<String>(); for (Method method : listMethods) { listMethodNames.add(method.getName()); } Field[] fields = javaClass.getDeclaredFields(); for (Field field : fields) { int fieldModifiers = field.getModifiers(); // Ignore static fields (constants) if (Modifier.isStatic(fieldModifiers)) continue; String fieldName = field.getName(); String errorMessage = javaClassName + ": " + field.getName(); // Check bean info PropertyDescriptor propertyDescriptor = isBeanProperty(fieldName, dboBeanInfo); if (propertyDescriptor != null) { // Check bean property name policy if (!propertyDescriptor.getName().equals(fieldName)) { Error.PROPERTY_NAMING_POLICY.add(errorMessage); } String declaredGetter = propertyDescriptor.getReadMethod().getName(); String declaredSetter = propertyDescriptor.getWriteMethod().getName(); String formattedFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); String expectedGetter = "get" + formattedFieldName; String expectedSetter = "set" + formattedFieldName; // Check getter name policy if (!declaredGetter.equals(expectedGetter)) { Error.GETTER_SETTER_DECLARED_EXPECTED_NAMES_MISMATCH .add(errorMessage + "\n" + " Declared getter: " + declaredGetter + "\n" + " Expected getter: " + expectedGetter); } // Check setter name policy if (!declaredSetter.equals(expectedSetter)) { Error.GETTER_SETTER_DECLARED_EXPECTED_NAMES_MISMATCH .add(errorMessage + "\n" + " Declared setter: " + declaredSetter + "\n" + " Expected setter: " + expectedSetter); } // Check required private modifiers for bean property if (!Modifier.isPrivate(fieldModifiers)) { Error.PROPERTY_NOT_PRIVATE.add(errorMessage); } // Check getter if (!listMethodNames.contains(declaredGetter)) { Error.GETTER_SETTER_DECLARED_BUT_NOT_FOUND .add(errorMessage + " - Declared getter not found: " + declaredGetter); } // Check setter if (!listMethodNames.contains(declaredSetter)) { Error.GETTER_SETTER_DECLARED_BUT_NOT_FOUND .add(errorMessage + " - Declared setter not found: " + declaredGetter); } // Check non transient modifier if (Modifier.isTransient(fieldModifiers)) { Error.PROPERTY_TRANSIENT.add(errorMessage); } } else if (!Modifier.isTransient(fieldModifiers)) { Error.FIELD_NOT_TRANSIENT.add(errorMessage); } } } catch (ClassNotFoundException e) { System.out.println("ERROR on " + javaClassName); e.printStackTrace(); } }
From source file:at.tuwien.ifs.somtoolbox.apps.SOMToolboxMain.java
/** * @param screenWidth the with of the screen * @param runnables {@link ArrayList} of available runnables. *//*w w w. j ava 2 s . c o m*/ private static void printAvailableRunnables(int screenWidth, ArrayList<Class<? extends SOMToolboxApp>> runnables) { Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR); ArrayList<Class<? extends SOMToolboxApp>> runnableClassList = new ArrayList<Class<? extends SOMToolboxApp>>(); ArrayList<String> runnableNamesList = new ArrayList<String>(); ArrayList<String> runnableDeskrList = new ArrayList<String>(); for (Class<? extends SOMToolboxApp> c : runnables) { try { // Ignore abstract classes and interfaces if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { continue; } runnableClassList.add(c); runnableNamesList.add(c.getSimpleName()); String desk = null; try { Field f = c.getDeclaredField("DESCRIPTION"); desk = (String) f.get(null); } catch (Exception e) { } if (desk != null) { runnableDeskrList.add(desk); } else { runnableDeskrList.add(""); } } catch (SecurityException e) { // Should not happen - no Security } catch (IllegalArgumentException e) { e.printStackTrace(); } } StringBuilder sb = new StringBuilder(); String lineSep = System.getProperty("line.separator", "\n"); int maxLen = StringUtils.getLongestStringLength(runnableNamesList); sb.append("Runnable classes:").append(lineSep); for (int i = 0; i < runnableNamesList.size(); i++) { final Type cType = Type.getType(runnableClassList.get(i)); if (i == 0 || !cType.equals(Type.getType(runnableClassList.get(i - 1)))) { sb.append(String.format("-- %s %s%s", cType.toString(), StringUtils.repeatString(screenWidth - (8 + cType.toString().length()), "-"), lineSep)); } sb.append(" "); sb.append(runnableNamesList.get(i)); sb.append(StringUtils.getSpaces(4 + maxLen - runnableNamesList.get(i).length())).append("- "); sb.append(runnableDeskrList.get(i)); sb.append(lineSep); } System.out.println(StringUtils.wrap(sb.toString(), screenWidth, StringUtils.getSpaces(maxLen + 10), true)); }
From source file:com.siberhus.ngai.core.CrudHelper.java
/** * //from w w w .j a v a 2 s .c o m * @param entityClass * @return */ private synchronized final static EntityInfo getEntityInfo(Class<?> entityClass) { EntityInfo entityInfo = ENTITY_INFO_CACHE.get(entityClass); if (entityInfo != null) { return entityInfo; } entityInfo = new EntityInfo(); Entity entityAnnot = (Entity) entityClass.getAnnotation(Entity.class); if (!"".equals(entityAnnot.name())) { entityInfo.setEntityName(entityAnnot.name()); } else { entityInfo.setEntityName(entityClass.getSimpleName()); } Collection<Field> entityFields = ReflectUtil.getFields(entityClass); for (Field field : entityFields) { if (Modifier.isStatic(field.getModifiers())) { continue; } if (field.getName().equals("id")) { continue; } if (!field.isAccessible()) { field.setAccessible(true); } entityInfo.getFieldSet().add(field); } ENTITY_INFO_CACHE.put(entityClass, entityInfo); return entityInfo; }