List of usage examples for java.lang Class isMemberClass
public boolean isMemberClass()
From source file:net.firejack.platform.model.service.reverse.ReverseEngineeringService.java
private EntityModel createBeanEntity(Class clazz, RegistryNodeModel registryNodeModel, List<RelationshipModel> relationships, Map<String, EntityModel> models) { if (clazz == Object.class) return null; EntityModel model = models.get(clazz.getName()); if (model != null) return model; String name = clazz.getSimpleName().replaceAll("\\B([A-Z]+)\\B", " $1"); EntityModel entityModel;/*from www. j a va 2 s . co m*/ if (clazz.isMemberClass()) { entityModel = new SubEntityModel(); name = "Nested " + name; registryNodeModel = models.get(clazz.getEnclosingClass().getName()); } else { entityModel = new EntityModel(); } models.put(clazz.getName(), entityModel); entityModel.setName(name); entityModel.setLookup(DiffUtils.lookup(registryNodeModel.getLookup(), name)); entityModel.setPath(registryNodeModel.getLookup()); entityModel.setAbstractEntity(Modifier.isAbstract(clazz.getModifiers())); entityModel.setTypeEntity(true); entityModel.setReverseEngineer(true); entityModel.setExtendedEntity( createBeanEntity(clazz.getSuperclass(), registryNodeModel, relationships, models)); entityModel.setProtocol(EntityProtocol.HTTP); entityModel.setParent(registryNodeModel); Field[] declaredFields = clazz.getDeclaredFields(); List<FieldModel> fields = new ArrayList<FieldModel>(declaredFields.length); for (Field field : declaredFields) { analyzeField(field.getName(), field.getGenericType(), TYPE, entityModel, field.getAnnotation(XmlElement.class), models, relationships, fields); XmlElements xmlElements = field.getAnnotation(XmlElements.class); if (xmlElements != null) { XmlElement[] elements = xmlElements.value(); List<EntityModel> options = new ArrayList<EntityModel>(elements.length); RegistryNodeModel parent = clazz.isMemberClass() ? registryNodeModel.getParent() : registryNodeModel; for (XmlElement element : elements) { if (element.type().isAnnotationPresent(XmlAccessorType.class)) { EntityModel entity = createBeanEntity(element.type(), parent, relationships, models); options.add(entity); } } FieldModel fieldModel = fields.get(fields.size() - 1); fieldModel.setOptions(options); } } entityModel.setFields(fields); return entityModel; }
From source file:jp.co.acroquest.jsonic.JSON.java
protected <T> T create(Context context, Class<? extends T> c) throws Exception { Object instance = null;/* w ww . jav a 2 s . c om*/ JSONHint hint = context.getHint(); if (hint != null && hint.type() != Object.class) c = hint.type().asSubclass(c); if (c.isInterface()) { if (SortedMap.class.equals(c)) { instance = new TreeMap<Object, Object>(); } else if (Map.class.equals(c)) { instance = new LinkedHashMap<Object, Object>(); } else if (SortedSet.class.equals(c)) { instance = new TreeSet<Object>(); } else if (Set.class.equals(c)) { instance = new LinkedHashSet<Object>(); } else if (List.class.equals(c)) { instance = new ArrayList<Object>(); } else if (Collection.class.equals(c)) { instance = new ArrayList<Object>(); } else if (Appendable.class.equals(c)) { instance = new StringBuilder(); } } else if (Modifier.isAbstract(c.getModifiers())) { if (Calendar.class.equals(c)) { instance = Calendar.getInstance(); } } else if ((c.isMemberClass() || c.isAnonymousClass()) && !Modifier.isStatic(c.getModifiers())) { Class<?> eClass = c.getEnclosingClass(); Constructor<?> con = c.getDeclaredConstructor(eClass); con.setAccessible(true); if (context.contextObject != null && eClass.isAssignableFrom(context.contextObject.getClass())) { instance = con.newInstance(context.contextObject); } else { instance = con.newInstance((Object) null); } } else { if (Date.class.isAssignableFrom(c)) { try { Constructor<?> con = c.getDeclaredConstructor(long.class); con.setAccessible(true); instance = con.newInstance(0l); } catch (NoSuchMethodException e) { // no handle } } if (instance == null) { Constructor<?> con = c.getDeclaredConstructor(); con.setAccessible(true); instance = con.newInstance(); } } return c.cast(instance); }
From source file:org.apache.ignite.spi.deployment.uri.GridUriDeploymentFileProcessor.java
/** * Check that class may be instantiated as {@link org.apache.ignite.compute.ComputeTask} and used * in deployment./*from www .ja v a 2 s .c o m*/ * * Loaded task class must implement interface {@link org.apache.ignite.compute.ComputeTask}. * Only non-abstract, non-interfaces and public classes allowed. * Inner static classes also allowed for loading. * * @param cls Class to check * @return {@code true} if class allowed for deployment. */ private static boolean isAllowedTaskClass(Class<?> cls) { if (!ComputeTask.class.isAssignableFrom(cls)) return false; int modifiers = cls.getModifiers(); return !Modifier.isAbstract(modifiers) && !Modifier.isInterface(modifiers) && (!cls.isMemberClass() || Modifier.isStatic(modifiers)) && Modifier.isPublic(modifiers); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * @return the result of {@link Class#isMemberClass()} and ignore {@link NoClassDefFoundError}. *///w ww .j a va2 s. c om public static boolean isMemberClass(final Class<?> clazz) { Assert.isNotNull(clazz); return ExecutionUtils.runObjectIgnore(new RunnableObjectEx<Boolean>() { public Boolean runObject() throws Exception { return clazz.isMemberClass(); } }, false); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * Returns the short name of {@link Class}, or same name for simple type name. * //w w w . j a v a2s . c o m * <pre> * getShortName("javax.swing.JPanel") = "JPanel" * getShortName("boolean") = "boolean" * </pre> * * @return the short name of given {@link Class}. */ public static String getShortName(Class<?> clazz) { String className = getFullyQualifiedName(clazz, false); // member Class if (clazz.isMemberClass()) { Class<?> topClass = getTopLevelClass(clazz); String topName = topClass.getName(); String topPackage = StringUtils.substringBeforeLast(topName, ".") + "."; return className.substring(topPackage.length()); } // normal top level Class, may be array if (className.indexOf('.') != -1) { return StringUtils.substringAfterLast(className, "."); } // primitive or default package return className; }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
private static Class<?> getTopLevelClass(Class<?> clazz) { while (clazz.isMemberClass()) { clazz = clazz.getEnclosingClass(); }/*from www .j av a 2 s . c o m*/ return clazz; }
From source file:org.gridgain.grid.spi.deployment.uri.GridUriDeploymentFileProcessor.java
/** * Check that class may be instantiated as {@link GridComputeTask} and used * in deployment.//w w w . ja v a 2 s . c o m * * Loaded task class must implement interface {@link GridComputeTask}. * Only non-abstract, non-interfaces and public classes allowed. * Inner static classes also allowed for loading. * * @param cls Class to check * @return {@code true} if class allowed for deployment. */ private static boolean isAllowedTaskClass(Class<?> cls) { if (!GridComputeTask.class.isAssignableFrom(cls)) return false; int modifiers = cls.getModifiers(); return !Modifier.isAbstract(modifiers) && !Modifier.isInterface(modifiers) && (!cls.isMemberClass() || Modifier.isStatic(modifiers)) && Modifier.isPublic(modifiers); }
From source file:org.jtester.utility.ReflectionUtils.java
/** * Creates an instance of the given type * //from w w w .j ava 2 s . c om * @param <T> * The type of the instance * @param type * The type of the instance * @param bypassAccessibility * If true, no exception is thrown if the parameterless * constructor is not public * @param argumentTypes * The constructor arg types, not null * @param arguments * The constructor args, not null * @return An instance of this type * @throws JTesterException * If an instance could not be created */ @SuppressWarnings("rawtypes") public static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments) { if (type.isMemberClass() && !isStatic(type.getModifiers())) { throw new JTesterException( "Creation of an instance of a non-static innerclass is not possible using reflection. The type " + type.getSimpleName() + " is only known in the context of an instance of the enclosing class " + type.getEnclosingClass().getSimpleName() + ". Declare the innerclass as static to make construction possible."); } try { Constructor<T> constructor = type.getDeclaredConstructor(argumentTypes); if (bypassAccessibility) { constructor.setAccessible(true); } return constructor.newInstance(arguments); } catch (InvocationTargetException e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e.getCause()); } catch (Exception e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e); } }
From source file:org.obsidian.test.TestAbstract.java
public <T> void appendDynamicImports(Class<T> classToImport) { boolean shouldBeIgnored = false; //Make sure: not already in dynamic imports for (String s : getDynamicImports()) { if (s.compareToIgnoreCase(classToImport.getName().replaceAll("\\$", ".")) == 0) { shouldBeIgnored = true;//from www .ja v a 2 s. c o m } } if (!classToImport.isPrimitive() && !classToImport.isArray()) { //make sure: not in imports from constants for (String s : Content.IMPORTS) { if (s.compareToIgnoreCase(classToImport.getName()) == 0) { shouldBeIgnored = true; } } //make sure: not in same package if (classToImport.getPackage().toString() .compareToIgnoreCase(classTested.getPackage().toString()) == 0) { //#### Patch Submitted by Michael Cole (micole.3@gmail.com) 2/13/13 if (!(classToImport.isMemberClass() || classToImport.isEnum() || classToImport.isLocalClass())) { shouldBeIgnored = true; } } //make sure not private if (Modifier.isPrivate(classToImport.getModifiers())) { shouldBeIgnored = true; } //make sure: not importing from java.lang unless at least 3 deep //ex)java.lang.reflect String[] packageStructure = classToImport.getPackage().getName().split("\\."); int packageDepth = packageStructure.length; //if dataInputStream java.lang if (packageStructure[0].compareToIgnoreCase("java") == 0 && packageStructure[1].compareToIgnoreCase("lang") == 0) { //and less than three deep if (packageDepth < 3) { shouldBeIgnored = true; classToImport.getName(); } } } else { shouldBeIgnored = true; if (classToImport.isArray() && !classToImport.getComponentType().isPrimitive()) { appendDynamicImports(classToImport.getComponentType()); } } //if: not already in imports and not in same package if (!shouldBeIgnored) { //add to dynamic imports String importName = classToImport.getName(); importName = importName.replaceAll("\\$", "."); getDynamicImports().add(importName); } }
From source file:org.opensingular.form.processor.TypeProcessorAttributeReadFromFile.java
/** Verifica se h um arquivos com valores de atributos associados a classe informada. */ @Nullable/*from w ww. j ava 2s . co m*/ private URL lookForFile(@Nonnull Class<?> typeClass) { String name = typeClass.getSimpleName(); Class<?> context = typeClass; for (; context.isMemberClass(); context = context.getEnclosingClass()) { name = concatNames(context, name); } return context.getResource(name + SUFFIX_PROPERTIES); }