alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.refactoring;

import java.lang.reflect.Modifier;
import java.util.*;
import org.netbeans.jmi.javamodel.*;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;

/**
 * Methods for validating parameters. Used in different refactorings, e.g.
 * rename class, change method signature etc.
 *
 * @author  Dan Prusa
 * @author  Pavel Flaska
 */
public final class CheckUtils {
    
    /**
     * Check if the provided method is overriden by another method.
     *
     * @param method
     * @param name
     * @param argTypes
     *
     * @return  method which overrides provided method, otherwise null
     */
    public static Collection isOverridden(Method method, String name, List argTypes) {
        if (!isVirtual(method))
            return null;                

        Collection res = new HashSet();

        ClassDefinition jc = method.getDeclaringClass();
        LinkedList subtypes = new LinkedList();
        addSubtypes(jc, subtypes);
        while (subtypes.size() > 0) {
            jc = (ClassDefinition) subtypes.removeFirst();
            Method m = getMethod(jc, name, argTypes);
            if ((m != null) && isVirtual(m))
                res.add(m);
            addSubtypes(jc, subtypes);
        }
        return res;
    }

    /**
     * @param method
     * @param name
     * @param argTypes
     * @param findFinal
     * @return
     */    
    public static Collection overrides(Method method, String name, List argTypes, boolean findFinal) {
        Collection res = new HashSet();

        if (!isVirtual(method))
            return null;

        ClassDefinition jc = method.getDeclaringClass ();
        LinkedList supertypes = new LinkedList ();

        supertypes.addAll (jc.getInterfaces ());
        jc = jc.getSuperClass ();
        if (jc != null)
            supertypes.add (jc);
        while (supertypes.size () > 0) {
            jc = (ClassDefinition) supertypes.removeFirst ();
            Method m = getMethod (jc, name, argTypes);
            if ((m != null) && isVirtual(m)) {
                if ((m.getModifiers () & Modifier.FINAL) > 0) {
                    res.add(m);
                    continue;
                } else if (res == null) {
                    res.add(m);
                    if (!findFinal)
                        continue;
                }
            }
            supertypes.addAll (jc.getInterfaces ());
            jc = jc.getSuperClass ();
            if (jc != null)
                supertypes.add (jc);
        }        
        return res;
    }
    
    /**
     * Checks, if method or constructor has variable arguments paramater.
     * If so, returns true, otherwise false.
     *
     * @param   constructor or method to check
     * @return  true, if callable contain variable argument parameter
     */
    public static boolean hasVarArgs(CallableFeature callable) {
        for (Iterator it = callable.getParameters().iterator(); it.hasNext(); ) {
            if (((Parameter) it.next()).isVarArg()) {
                return true;
            }
        }
        return false;
    }
    
    private static boolean isVirtual(Feature feature) {
        int mod = feature.getModifiers ();
        return ((mod & Modifier.PROTECTED) > 0) || ((mod & Modifier.PUBLIC) > 0);
    }
    
    public static Constructor getConstructor(ClassDefinition cd, List argTypes) {
        return (Constructor) getCallable(cd, null, argTypes);
    }
    
    public static Method getMethod (ClassDefinition cd, String name, List argTypes) {
        return (Method) getCallable(cd, name, argTypes);
    }
    
    public static CallableFeature getCallable(ClassDefinition cd, String name, List argTypes) {
        boolean isMethod = name != null;
        Iterator iter = cd.getFeatures().iterator();
        while (iter.hasNext()) {
            Object obj = iter.next ();
            if (!(obj instanceof CallableFeature)) {
                continue;
            }
            CallableFeature method = (CallableFeature) obj;
            if (isMethod && !name.equals(method.getName())) {
                continue;
            }
            List pars = method.getParameters ();
            if (argTypes.size () != pars.size ()) {
                continue;
            }
            Iterator parsIter = pars.iterator ();
            Iterator argsIter = argTypes.iterator ();
            boolean matches = true;
            while (parsIter.hasNext ()) {
                String t1 = ((Parameter) parsIter.next()).getType().getName();
                String t2 = ((Type) argsIter.next()).getName();
                if (!t1.equals (t2)) {
                    matches = false;
                    break;
                }
            } // while
            if (matches) {
                return method;
            }
        } // while
        return null;
    }
    
    public static Field getField (ClassDefinition cd, String fieldName) {
        for (Iterator it = cd.getFeatures().iterator(); it.hasNext();) {
            Object f = it.next();
            if ((f instanceof Field) && fieldName.equals(((Field) f).getName())) {
                return (Field) f;
            }
        }
        return null;    
    }

    public static JavaClass getClass (JavaPackage pkg, String clsName) {
        for (Iterator it = pkg.getResources().iterator(); it.hasNext();) {
            Resource res = (Resource) it.next();
            for (Iterator it2 = res.getClassifiers().iterator(); it2.hasNext();) {
                Object obj = it2.next();
                if (obj instanceof JavaClass) {
                    if (clsName.equals(((JavaClass) obj).getName())) {
                        return (JavaClass) obj;
                    }
                }
            } // for
        } // for
        return null;
    }

    public static boolean isFolderEmpty(FileObject f) {
        DataFolder dob = DataFolder.findFolder(f);
        DataObject children[] = dob.getChildren();
        for (int i = 0; i < children.length; i++) {
            if (!(children[i] instanceof DataFolder))
                return false;
        }
        return true;
    }
    
    
    ////////////////////////////////////////////////////////////////////////////
    // INTERNAL METHODS
    ////////////////////////////////////////////////////////////////////////////
    private static void addSubtypes(ClassDefinition cd, List list) {
        if (!(cd instanceof JavaClass)) {
            return;
        }
        JavaClass jc = (JavaClass) cd;
        Collection desc = null;
        if (jc.isInterface()) {            
            desc = jc.getImplementors();
        } else {
            desc = jc.getSubClasses();            
        }
        list.addAll(desc);        
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.