List of usage examples for java.lang Class getEnclosingClass
@CallerSensitive public Class<?> getEnclosingClass() throws SecurityException
From source file:org.evosuite.testcase.TestCodeVisitor.java
/** * <p>/* www . j a v a 2s . co m*/ * getClassName * </p> * * @param clazz * a {@link java.lang.Class} object. * @return a {@link java.lang.String} object. */ public String getClassName(Class<?> clazz) { if (classNames.containsKey(clazz)) return classNames.get(clazz); if (clazz.isArray()) { return getClassName(clazz.getComponentType()) + "[]"; } GenericClass c = new GenericClass(clazz); String name = c.getSimpleName(); if (classNames.values().contains(name)) { name = clazz.getCanonicalName(); } else { /* * If e.g. there is a foo.bar.IllegalStateException with * foo.bar being the SUT package, then we need to use the * full package name for java.lang.IllegalStateException */ String fullName = Properties.CLASS_PREFIX + "." + name; if (!fullName.equals(clazz.getCanonicalName())) { try { if (ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()) .hasClass(fullName)) { name = clazz.getCanonicalName(); } } catch (IllegalArgumentException e) { // If the classpath is not correct, then we just don't check // because that cannot happen in regular EvoSuite use, only // from test cases } } } // Ensure outer classes are imported as well Class<?> outerClass = clazz.getEnclosingClass(); if (outerClass != null) { String enclosingName = getClassName(outerClass); String simpleOuterName = outerClass.getSimpleName(); if (simpleOuterName.equals(enclosingName)) { name = enclosingName + name.substring(simpleOuterName.length()); } else { name = enclosingName + name.substring(name.lastIndexOf(simpleOuterName) + simpleOuterName.length()); } } Class<?> declaringClass = clazz.getDeclaringClass(); if (declaringClass != null) { getClassName(declaringClass); } // We can't use "Test" because of JUnit if (name.equals("Test")) { name = clazz.getCanonicalName(); } classNames.put(clazz, name); return name; }
From source file:jp.co.acroquest.jsonic.JSON.java
protected <T> T create(Context context, Class<? extends T> c) throws Exception { Object instance = null;//from www. j ava 2 s. c o m 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:mondrian.olap.Util.java
/** * Creates a new udf instance from the given udf class. * * @param udfClass the class to create new instance for * @param functionName Function name, or null * @return an instance of UserDefinedFunction *//*w ww . jav a 2s . c om*/ public static UserDefinedFunction createUdf(Class<? extends UserDefinedFunction> udfClass, String functionName) { // Instantiate class with default constructor. UserDefinedFunction udf; String className = udfClass.getName(); String functionNameOrEmpty = functionName == null ? "" : functionName; // Find a constructor. Constructor<?> constructor; Object[] args = {}; // 0. Check that class is public and top-level or static. // Before JDK 1.5, inner classes are impossible; retroweaver cannot // handle the getEnclosingClass method, so skip the check. if (!Modifier.isPublic(udfClass.getModifiers()) || (!PreJdk15 && udfClass.getEnclosingClass() != null && !Modifier.isStatic(udfClass.getModifiers()))) { throw MondrianResource.instance().UdfClassMustBePublicAndStatic.ex(functionName, className); } // 1. Look for a constructor "public Udf(String name)". try { constructor = udfClass.getConstructor(String.class); if (Modifier.isPublic(constructor.getModifiers())) { args = new Object[] { functionName }; } else { constructor = null; } } catch (NoSuchMethodException e) { constructor = null; } // 2. Otherwise, look for a constructor "public Udf()". if (constructor == null) { try { constructor = udfClass.getConstructor(); if (Modifier.isPublic(constructor.getModifiers())) { args = new Object[] {}; } else { constructor = null; } } catch (NoSuchMethodException e) { constructor = null; } } // 3. Else, no constructor suitable. if (constructor == null) { throw MondrianResource.instance().UdfClassWrongIface.ex(functionNameOrEmpty, className, UserDefinedFunction.class.getName()); } // Instantiate class. try { udf = (UserDefinedFunction) constructor.newInstance(args); } catch (InstantiationException e) { throw MondrianResource.instance().UdfClassWrongIface.ex(functionNameOrEmpty, className, UserDefinedFunction.class.getName()); } catch (IllegalAccessException e) { throw MondrianResource.instance().UdfClassWrongIface.ex(functionName, className, UserDefinedFunction.class.getName()); } catch (ClassCastException e) { throw MondrianResource.instance().UdfClassWrongIface.ex(functionNameOrEmpty, className, UserDefinedFunction.class.getName()); } catch (InvocationTargetException e) { throw MondrianResource.instance().UdfClassWrongIface.ex(functionName, className, UserDefinedFunction.class.getName()); } return udf; }