Example usage for java.lang.reflect Method getModifiers

List of usage examples for java.lang.reflect Method getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Method getModifiers.

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:org.evosuite.assertion.InspectorManager.java

private boolean isInspectorMethod(Method method) {
    if (!Modifier.isPublic(method.getModifiers()))
        return false;

    if (!method.getReturnType().isPrimitive() && !method.getReturnType().equals(String.class)
            && !method.getReturnType().isEnum() && !ClassUtils.isPrimitiveWrapper(method.getReturnType())) {
        return false;
    }/*from  ww w. j a va2  s  .  co m*/

    if (method.getReturnType().equals(void.class))
        return false;

    if (method.getParameterTypes().length != 0)
        return false;

    if (method.getName().equals("hashCode"))
        return false;

    if (method.getDeclaringClass().equals(Object.class))
        return false;

    if (method.getDeclaringClass().equals(Enum.class))
        return false;

    if (method.isSynthetic())
        return false;

    if (method.isBridge())
        return false;

    if (method.getName().equals("pop"))
        return false;

    if (isBlackListed(method))
        return false;

    if (isImpureJDKMethod(method))
        return false;

    if (isAWTToString(method))
        return false;

    if (Properties.PURE_INSPECTORS) {
        if (!CheapPurityAnalyzer.getInstance().isPure(method)) {
            return false;
        }
    }

    return true;

}

From source file:de.contentreich.instrumentation.SpringBeansHelper.java

public List<String[]> getPublicMethodSignatures(String className) {
    logger.debug("Get public method signatures for class " + className);
    ArrayList<String[]> methodSignatures = new ArrayList<String[]>();
    try {/*ww  w  .  j  ava2  s. com*/
        Class clazz = Class.forName(className);
        List<Method> methods = (List<Method>) Arrays.asList(clazz.getMethods());
        // Filtering - a pita in java
        for (Iterator<Method> iterator = methods.iterator(); iterator.hasNext();) {
            Method method = iterator.next();
            if (!method.getDeclaringClass().getName().startsWith("java")
                    && Modifier.isPublic(method.getModifiers())) {
                methodSignatures.add(new String[] { method.toString(), method.getDeclaringClass().getName() });
            }
        }
        Collections.sort(methodSignatures, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                String s1 = ((String[]) o1)[0];
                String s2 = ((String[]) o2)[0];
                return s1.compareTo(s2);
            }

        });
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }
    return methodSignatures;
}

From source file:org.dhatim.javabean.factory.BasicFactoryDefinitionParser.java

/**
 * Creates a StaticMethodFactory object.
 *
 * @param factoryDefinition/*w w  w . ja  v  a  2s .c o  m*/
 * @param className
 * @param methodDef
 * @return
 * @throws ClassNotFoundException
 * @throws SecurityException
 * @throws NoSuchMethodException
 */
private Factory<?> createStaticMethodFactory(String factoryDefinition, String className, String methodDef)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    Class<?> factoryClass = ClassUtil.forName(className, this.getClass());
    Method factoryMethod = factoryClass.getMethod(methodDef);

    if (!Modifier.isStatic(factoryMethod.getModifiers())) {
        throw new NoSuchMethodException(
                "No static method with the name '" + methodDef + "' can be found on the class '" + className
                        + "' while processing the factory definition '" + factoryDefinition + "'.");
    }

    return new StaticMethodFactory(factoryDefinition, factoryMethod);
}

From source file:com.urbancode.x2o.xml.XmlWrite.java

public List<Method> findMethodsForClass(String methodPrefix, Class clazz) throws ClassNotFoundException {
    List<Method> result = new ArrayList<Method>();
    for (Method method : clazz.getMethods()) {
        if (method.getName().startsWith(methodPrefix)) {
            if (!(method.getName().equals("getClass") || Modifier.isProtected(method.getModifiers()))) {
                result.add(method);/*from www.jav  a 2s  .c o  m*/
            }
        }
    }
    return result;
}

From source file:io.konik.utils.RandomInvoiceGenerator.java

public Object populteData(Class<?> root, String name) throws InstantiationException, IllegalAccessException,
        NoSuchMethodException, InvocationTargetException {
    Object rootObj;//  w  ww  .  j  a  v a2 s  .c  om
    if (isLeafType(root)) {//final type
        return generatePrimitveValue(root, name);
    }
    rootObj = createNewInstance(root);

    // get method and populate each of them
    Method[] methods = root.getMethods();
    for (Method method : methods) {
        int methodModifiers = method.getModifiers();
        Class<?> methodParameter = null;
        if (Modifier.isAbstract(methodModifiers) || method.isSynthetic())
            continue;
        if (method.getName().startsWith("add")) {
            methodParameter = method.getParameterTypes()[0];
            if (methodParameter != null && !methodParameter.isArray()
                    && (methodParameter.isInterface() || Modifier.isAbstract(methodParameter.getModifiers()))) {
                continue;
            }
        }
        //getter
        else if (method.getName().startsWith("get")
                && !Collection.class.isAssignableFrom(method.getReturnType())
                && !method.getName().equals("getClass") && !Modifier.isAbstract(methodModifiers)) {
            methodParameter = method.getReturnType();
        } else {
            continue;// next on setter
        }
        if (methodParameter == null || methodParameter.isInterface()) {
            continue;
        }
        Object popultedData = populteData(methodParameter, method.getName());
        setValue(rootObj, method, popultedData);
    }
    return rootObj;
}

From source file:com.smartitengineering.loadtest.engine.impl.management.AbstracltTestCaseBatchCreator.java

private TestCaseCreationFactory getTestCaseCreationFactory(UnitTestInstance testInstance)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    final String instanceFactoryClassName = testInstance.getInstanceFactoryClassName();
    if (!StringUtils.isEmpty(instanceFactoryClassName)) {
        try {//from   ww  w . j  ava 2s. co m
            final Class<? extends TestCaseCreationFactory> classForName = (Class<? extends TestCaseCreationFactory>) Class
                    .forName(instanceFactoryClassName);
            try {
                Method method = classForName.getDeclaredMethod("getInstance");
                if (method != null && Modifier.isStatic(method.getModifiers())) {
                    return (TestCaseCreationFactory) method.invoke(classForName);
                }
            } catch (NoSuchMethodException exception) {
                exception.printStackTrace();
            }
            return classForName.newInstance();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    return DefaultTestCaseCreationFactory.getInstance();
}

From source file:org.codekaizen.vtj.AbstractValueTypeTest.java

/**
 * Verifies the class has no setter methods.
 *//*from   w  w  w . j  av a  2s  .  c  om*/
@Test(groups = { "api" })
public final void shouldBeImmutable() {
    final Method[] methods = getTestClass().getMethods();

    for (final Method method : methods) {

        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }

        assertFalse(method.getName().startsWith("set"));
    }
}

From source file:com.google.gdt.eclipse.designer.model.property.DateTimeFormatPropertyEditor.java

public void configure(EditorState state, Map<String, Object> parameters) throws Exception {
    m_loader = new CompositeClassLoader() {
        @Override//from ww  w  .  ja v a2  s. c o m
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (ReflectionUtils.class.getCanonicalName().equals(name)) {
                return ReflectionUtils.class;
            }
            return super.loadClass(name);
        }
    };
    m_loader.add(state.getEditorLoader(), null);
    // get static formats
    {
        m_formatClass = m_loader.loadClass("com.google.gwt.i18n.client.DateTimeFormat");
        m_formatMethods = Lists.newArrayList();
        for (Method method : m_formatClass.getMethods()) {
            int modifiers = method.getModifiers();
            if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)
                    && method.getParameterTypes().length == 0 && method.getName().endsWith("Format")) {
                m_formatMethods.add(method);
            }
        }
    }
    // extract script
    {
        final String EXTRACT_PARAM = "extract";
        if (parameters.containsKey(EXTRACT_PARAM)) {
            m_extractScript = (String) parameters.get(EXTRACT_PARAM);
        } else {
            m_extractScript = "";
        }
    }
    // source template
    {
        final String SOURCE_PARAM = "source";
        if (parameters.containsKey(SOURCE_PARAM)) {
            m_sourceTemplate = (String) parameters.get(SOURCE_PARAM);
        } else {
            m_sourceTemplate = "";
        }
    }
}

From source file:org.jgentleframework.configure.ConfigurationProxy.java

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

    if (Modifier.isAbstract(method.getModifiers())) {
        if (method.isAnnotationPresent(Block.class)) {
            Block anno = method.getAnnotation(Block.class);
            Class<?>[] clazzList = anno.value();
            ObjectBlock objBlock = new ObjectBlock(method, clazzList);
            this.objBlockList.add(objBlock);
        }/*from w  w  w . j a v  a 2s .  c o m*/
        // thc thi hm getOptionsList trn ConfigModule interface
        if (method.getName().equals("getOptionsList") && method.getParameterTypes().length == 0) {
            checkBlock(method);
            return this.optionsList;
        } else if (method.getName().equals("getTargetClass") && method.getParameterTypes().length == 0) {
            checkBlock(method);
            return this.targetClass;
        } else if (method.getName().equals("getConfigInstance") && method.getParameterTypes().length == 1) {
            checkBlock(method);
            return this.configObjList.get(args[0]);
        } else {
            if (!this.objBlockList.isEmpty()) {
                ObjectBlock objb = this.objBlockList.get(this.configObjList.size() - 1);
                List<Class<?>> clazzList = null;
                clazzList = objb.getBlockList();
                for (Class<?> clazz : clazzList) {
                    Object objConfig = this.configObjList.get(clazz);
                    List<Method> methodList = Arrays.asList(objConfig.getClass().getMethods());
                    if (methodList.contains(method)) {
                        checkBlock(method);
                        return method.invoke(objConfig, args);
                    } else {
                        continue;
                    }
                }
            }
            // Nu khng c
            for (Class<?> clazz : this.configObjList.keySet()) {
                Object objConfig = this.configObjList.get(clazz);
                List<Method> methodList = Arrays.asList(ReflectUtils.getAllDeclaredMethods(clazz));
                if (methodList.contains(method)) {
                    checkBlock(method);
                    return method.invoke(objConfig, args);
                } else {
                    continue;
                }
            }
            throw new NoSuchMethodException("Could not found " + method + " method!");
        }
    } else {
        checkBlock(method);
        return proxy.invokeSuper(obj, args);
    }
}

From source file:ClassFigure.java

public ClassFigure(Class cls) {
    setLayoutManager(new ToolbarLayout());
    setBorder(new LineBorder(ColorConstants.black));
    setBackgroundColor(ColorConstants.yellow);
    setOpaque(true);//from   w w  w .  j a va2 s .  c  om

    for (int i = 0; i < keys.length; i++)
        registry.put(keys[i], ImageDescriptor.createFromFile(null, "icons/java/" + keys[i] + ".gif"));

    Label title = new Label(cls.getName(), registry.get(KEY_CLASS));
    add(title);
    add(fieldBox);
    add(methodBox);

    // fields.
    Field[] fields = cls.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        Image image = null;
        if (Modifier.isPublic(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PUBLIC);
        } else if (Modifier.isProtected(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PROTECTED);
        } else if (Modifier.isPrivate(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PRIVATE);
        } else {
            image = registry.get(KEY_FIELD_DEFAULT);
        }
        fieldBox.add(new Label(fields[i].getName(), image));
    }

    // fields.
    Method[] methods = cls.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        Image image = null;
        if (Modifier.isPublic(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PUBLIC);
        } else if (Modifier.isProtected(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PROTECTED);
        } else if (Modifier.isPrivate(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PRIVATE);
        } else {
            image = registry.get(KEY_METHOD_DEFAULT);
        }
        methodBox.add(new Label(methods[i].getName(), image));
    }

}