List of usage examples for java.lang.reflect Constructor getModifiers
@Override public int getModifiers()
From source file:org.ireland.jnetty.dispatch.servlet.ServletConfigImpl.java
/** * Checks the class constructor for the public-zero arg. Class??public *//*www . j ava 2 s.c o m*/ public void checkConstructor() throws ServletException { Constructor[] constructors = _servletClass.getDeclaredConstructors(); Constructor zeroArg = null; for (int i = 0; i < constructors.length; i++) { if (constructors[i].getParameterTypes().length == 0) { zeroArg = constructors[i]; break; } } if (zeroArg == null) throw error(_servletClassName + " | must have a zero arg constructor. Servlets must have public zero-arg constructors.\n" + (constructors != null ? constructors[0] : null) + " is not a valid constructor."); if (!Modifier.isPublic(zeroArg.getModifiers())) throw error(zeroArg + " | must be public. '" + _servletClassName + "' must have a public, zero-arg constructor."); }
From source file:com.github.wshackle.java4cpp.J4CppMain.java
public static boolean hasNoArgConstructor(Constructor[] constructors) { for (Constructor c : constructors) { if ((Modifier.isProtected(c.getModifiers()) || Modifier.isPublic(c.getModifiers())) && c.getParameterTypes().length == 0) { return true; }/*from ww w .jav a2s .c o m*/ } return false; }
From source file:com.github.wshackle.java4cpp.J4CppMain.java
private static boolean checkConstructor(Constructor c, Class clss, List<Class> classes) { if (!Modifier.isPublic(c.getModifiers())) { if (c.getParameterTypes().length != 0 || !Modifier.isProtected(c.getModifiers())) { return true; }/* w w w. ja va 2s . c o m*/ } Constructor ca[] = clss.getDeclaredConstructors(); for (int i = 0; i < ca.length; i++) { Constructor constructor = ca[i]; if (constructor.equals(c)) { break; } if (constructor.getParameterTypes().length == c.getParameterTypes().length) { if (c.getParameterTypes().length >= 1 && String.class.isAssignableFrom(c.getParameterTypes()[0]) != String.class .isAssignableFrom(constructor.getParameterTypes()[0])) { continue; } return true; } } if (c.getParameterTypes().length == 1 && clss.isAssignableFrom(c.getParameterTypes()[0])) { return true; } if (!checkParameters(c.getParameterTypes(), classes)) { return true; } return false; }
From source file:org.argouml.moduleloader.ModuleLoader2.java
/** * Try to load a module from the given ClassLoader. * <p>/*from www . ja va 2 s .co m*/ * * Only add it as a module if it is a module (i.e. it implements the * {@link ModuleInterface} interface. * * @param classLoader * The ClassLoader to load from. * @param classname * The name. * @throws ClassNotFoundException * if the class classname is not found. */ private boolean addClass(ClassLoader classLoader, String classname) throws ClassNotFoundException { LOG.log(Level.INFO, "Loading module " + classname); Class moduleClass; try { moduleClass = classLoader.loadClass(classname); } catch (UnsupportedClassVersionError e) { LOG.log(Level.SEVERE, "Unsupported Java class version for " + classname); return false; } catch (NoClassDefFoundError e) { LOG.log(Level.SEVERE, "Unable to find required class while loading " + classname + " - may indicate an obsolete" + " extension module or an unresolved dependency", e); return false; } catch (Throwable e) { if (e instanceof ClassNotFoundException) { throw (ClassNotFoundException) e; } LOG.log(Level.SEVERE, "Unexpected error while loading " + classname, e); return false; } if (!ModuleInterface.class.isAssignableFrom(moduleClass)) { LOG.log(Level.FINE, "The class {0} is not a module.", classname); return false; } Constructor defaultConstructor; try { defaultConstructor = moduleClass.getDeclaredConstructor(new Class[] {}); } catch (SecurityException e) { LOG.log(Level.SEVERE, "The default constructor for class " + classname + " is not accessable.", e); return false; } catch (NoSuchMethodException e) { LOG.log(Level.SEVERE, "The default constructor for class " + classname + " is not found.", e); return false; } catch (NoClassDefFoundError e) { LOG.log(Level.SEVERE, "Unable to find required class while loading " + classname + " - may indicate an obsolete" + " extension module or an unresolved dependency", e); return false; } catch (Throwable e) { LOG.log(Level.SEVERE, "Unexpected error while loading " + classname, e); return false; } if (!Modifier.isPublic(defaultConstructor.getModifiers())) { LOG.log(Level.SEVERE, "The default constructor for class " + classname + " is not public. Not loaded."); return false; } Object moduleInstance; try { moduleInstance = defaultConstructor.newInstance(new Object[] {}); } catch (IllegalArgumentException e) { LOG.log(Level.SEVERE, "The constructor for class " + classname + " is called with incorrect argument.", e); return false; } catch (InstantiationException e) { LOG.log(Level.SEVERE, "The constructor for class " + classname + " threw an exception.", e); return false; } catch (IllegalAccessException e) { LOG.log(Level.SEVERE, "The constructor for class " + classname + " is not accessible.", e); return false; } catch (InvocationTargetException e) { LOG.log(Level.SEVERE, "The constructor for class " + classname + " cannot be called.", e); return false; } catch (NoClassDefFoundError e) { LOG.log(Level.SEVERE, "Unable to find required class while instantiating " + classname + " - may indicate an obsolete" + " extension module or an unresolved dependency", e); return false; } catch (Throwable e) { LOG.log(Level.SEVERE, "Unexpected error while instantiating " + classname, e); return false; } // The following check should have been satisfied before we // instantiated the module, but double check again if (!(moduleInstance instanceof ModuleInterface)) { LOG.log(Level.SEVERE, "The class " + classname + " is not a module."); return false; } ModuleInterface mf = (ModuleInterface) moduleInstance; addModule(mf); LOG.log(Level.INFO, "Succesfully loaded module {0}", classname); return true; }
From source file:org.evosuite.setup.TestClusterGenerator.java
public static boolean canUse(Constructor<?> c) { if (c.isSynthetic()) { return false; }/*from ww w . ja v a2 s .c o m*/ // synthetic constructors are OK if (Modifier.isAbstract(c.getDeclaringClass().getModifiers())) return false; // TODO we could enable some methods from Object, like getClass //if (c.getDeclaringClass().equals(java.lang.Object.class)) // return false;// handled here to avoid printing reasons if (c.getDeclaringClass().equals(java.lang.Thread.class)) return false;// handled here to avoid printing reasons if (c.getDeclaringClass().isAnonymousClass()) return false; if (c.getDeclaringClass().isLocalClass()) { logger.debug("Skipping constructor of local class {}", c.getName()); return false; } if (c.getDeclaringClass().isMemberClass() && !canUse(c.getDeclaringClass())) return false; if (!Properties.USE_DEPRECATED && c.getAnnotation(Deprecated.class) != null) { logger.debug("Skipping deprecated constructor {}", c.getName()); return false; } if (isForbiddenNonDeterministicCall(c)) { return false; } if (Modifier.isPublic(c.getModifiers())) { makeAccessible(c); return true; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(c.getModifiers())) { // && !Modifier.isProtected(c.getModifiers())) { String packageName = ClassUtils.getPackageName(c.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX)) { makeAccessible(c); return true; } } return false; }
From source file:org.apache.flink.api.java.typeutils.TypeExtractor.java
@SuppressWarnings("unchecked") protected <OUT, IN1, IN2> TypeInformation<OUT> analyzePojo(Class<OUT> clazz, ArrayList<Type> typeHierarchy, ParameterizedType parameterizedType, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) { if (!Modifier.isPublic(clazz.getModifiers())) { LOG.info("Class " + clazz.getName() + " is not public, cannot treat it as a POJO type. Will be handled as GenericType"); return new GenericTypeInfo<OUT>(clazz); }/* w w w .j av a2 s . c o m*/ // add the hierarchy of the POJO itself if it is generic if (parameterizedType != null) { getTypeHierarchy(typeHierarchy, parameterizedType, Object.class); } // create a type hierarchy, if the incoming only contains the most bottom one or none. else if (typeHierarchy.size() <= 1) { getTypeHierarchy(typeHierarchy, clazz, Object.class); } List<Field> fields = getAllDeclaredFields(clazz, false); if (fields.size() == 0) { LOG.info("No fields detected for " + clazz + ". Cannot be used as a PojoType. Will be handled as GenericType"); return new GenericTypeInfo<OUT>(clazz); } List<PojoField> pojoFields = new ArrayList<PojoField>(); for (Field field : fields) { Type fieldType = field.getGenericType(); if (!isValidPojoField(field, clazz, typeHierarchy)) { LOG.info(clazz + " is not a valid POJO type"); return null; } try { ArrayList<Type> fieldTypeHierarchy = new ArrayList<Type>(typeHierarchy); fieldTypeHierarchy.add(fieldType); TypeInformation<?> ti = createTypeInfoWithTypeHierarchy(fieldTypeHierarchy, fieldType, in1Type, in2Type); pojoFields.add(new PojoField(field, ti)); } catch (InvalidTypesException e) { Class<?> genericClass = Object.class; if (isClassType(fieldType)) { genericClass = typeToClass(fieldType); } pojoFields.add(new PojoField(field, new GenericTypeInfo<OUT>((Class<OUT>) genericClass))); } } CompositeType<OUT> pojoType = new PojoTypeInfo<OUT>(clazz, pojoFields); // // Validate the correctness of the pojo. // returning "null" will result create a generic type information. // List<Method> methods = getAllDeclaredMethods(clazz); for (Method method : methods) { if (method.getName().equals("readObject") || method.getName().equals("writeObject")) { LOG.info(clazz + " contains custom serialization methods we do not call."); return null; } } // Try retrieving the default constructor, if it does not have one // we cannot use this because the serializer uses it. Constructor defaultConstructor = null; try { defaultConstructor = clazz.getDeclaredConstructor(); } catch (NoSuchMethodException e) { if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { LOG.info(clazz + " is abstract or an interface, having a concrete " + "type can increase performance."); } else { LOG.info(clazz + " must have a default constructor to be used as a POJO."); return null; } } if (defaultConstructor != null && !Modifier.isPublic(defaultConstructor.getModifiers())) { LOG.info("The default constructor of " + clazz + " should be Public to be used as a POJO."); return null; } // everything is checked, we return the pojo return pojoType; }
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 *///from ww w .j av a 2s. co m 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; }