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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

/*
 * EventSetInheritanceAnalyser.java
 *
 * Created on 24. leden 2001, 9:14
 */

package org.netbeans.modules.beans;

import java.lang.reflect.*;
import java.text.MessageFormat;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.src.*;

/**
 *
 * @author  Petr Suchomel
 * @version 0.1
 * utility class, try to detect if given ClassElement has parent which contains given event set
 */
class EventSetInheritanceAnalyser extends Object {
    
    /** Used to test if PropertyChangeSupport exists
     * @param clazz Class which be tested for PropertyChangeSupport
     * @return Class in which PropertySupport exist, or null
     */    
    static MemberElement detectPropertyChangeSupport(ClassElement clazz){
        return findSupport(clazz, "java.beans.PropertyChangeSupport" ); // NOI18N
    }

    /** Used to test if VetoableChangeSupport exists
     * @param clazz Class which be tested for VetoableChangeSupport
     * @return Class in which VetoableSupport exist, or null
     */    
    static MemberElement detectVetoableChangeSupport(ClassElement clazz){
        return findSupport(clazz, "java.beans.VetoableChangeSupport" ); // NOI18N
    }
    
    /** Used to test if given ChangeSupport exists
     * @param clazz Class which be tested for ChangeSupport
     * @param supportName full name of ChangeSupport
     * @return Class in which ChangeSupport exist, or null
     */    
    private static MemberElement findSupport(ClassElement clazz, String supportName){
        Identifier propertyChangeField = null;
        
        if( clazz == null || clazz.getName().getFullName().equals("java.lang.Object")) //NOI18N
            return null;    //no super class given or super class is Object
        
        Identifier superClass = clazz.getSuperclass();
        if( superClass == null ) //no extends or implements clause
            return null;

        ClassElement parent = ClassElement.forName( superClass.getFullName(), PatternAnalyser.fileObjectForElement( clazz ) );
        if( parent == null )
            return parent; 
        else {
            if( propertyChangeField == null )
                propertyChangeField = Identifier.create( supportName ); // NOI18N                

            MethodElement methods[] = parent.getMethods();
            for( int i = 0; i < methods.length; i++ ) {
                if( !Modifier.isPrivate(methods[i].getModifiers()) && methods[i].getParameters().length == 0 ){
                    if( !methods[i].getReturn().isPrimitive() && !methods[i].getReturn().isArray() && methods[i].getReturn().getClassName().compareTo(propertyChangeField, false )  ){
                        return methods[i];
                    }
                }            
            }            
            FieldElement fields[] = parent.getFields();
            for( int i = 0; i < fields.length; i++ ) {
                if( !Modifier.isPrivate(fields[i].getModifiers()) ){                    
                    if( !fields[i].getType().isPrimitive() && !fields[i].getType().isArray() && fields[i].getType().getClassName().compareTo(propertyChangeField, false )  ){
                        return fields[i];
                    }
                }            
            }            
        }
        return findSupport(parent, supportName);    //Try to search recursively            
    }

    static String showInheritanceEventDialog( MemberElement me , String supportTypeName){        
        String supportName = getInheritanceEventSupportName(me, supportTypeName);
        if( me != null ){
            Object msgfields[] = new Object[] {me.getDeclaringClass().getName().getFullName(), supportTypeName };
            String msg = MessageFormat.format(PatternNode.getString("MSG_Inheritance_Found"), msgfields);
            NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( msg , NotifyDescriptor.YES_NO_OPTION );
            DialogDisplayer.getDefault().notify( nd );
            if( nd.getValue().equals( NotifyDescriptor.YES_OPTION ) ) {     
                return supportName;
            }
        }        
        return null;
    }
    
    static String getInheritanceEventSupportName( MemberElement me , String supportTypeName){
        ElementFormat format = new ElementFormat("{n}({p})"); // NOI18N
        String supportName = null;
        if( me != null ){
            if( me instanceof MethodElement )
                supportName = format.format(((MethodElement)me));
            else
                supportName = me.getName().getFullName();   //prepare for later usage            
        }        
        return supportName;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.