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

package org.netbeans.modules.java.bridge;

import javax.jmi.reflect.InvalidObjectException;

import java.beans.*;
import java.util.*;
import javax.jmi.reflect.RefBaseObject;
import org.openide.src.*;

import org.netbeans.api.mdr.events.*;

import org.netbeans.jmi.javamodel.Method;
import org.netbeans.jmi.javamodel.IsOfType;
import org.netbeans.jmi.javamodel.TypeReference;
import org.netbeans.jmi.javamodel.ArrayReference;

/**
 * The class implements properties specific to a method.
 *
 * @author  Svatopluk Dedic, Petr Hamernik, Jaroslav Tulach
 * @version 0.1
 * @since 24/11/2000
 */
class MethodElementImpl extends CallableImpl implements MethodElement.Impl {

    private ElementImpl.ElementListener methodListener;
    
    private static final long serialVersionUID = 8831749368310053231L;
    
    // Construction
    ///////////////////////////////////////////////////////////////////////////////////

    MethodElementImpl(DefaultLangModel model, Method method) {
        super(model, method);
    }        
    
    public void connectListener () {
        methodListener = new MethodListener (this);
        methodListener.connect ();
    }
    
    protected void createFromModel(Element el) throws SourceException {
        super.createFromModel(el);
        MethodElement m = (MethodElement)el;
        setReturn(m.getReturn());
    }
    
    // Getters
    ///////////////////////////////////////////////////////////////////////////////
    
    /**
     * Retrieves the return type of the method.
     * @return the return type description.
     */
    public Type getReturn() {
        repository.beginTrans(false);
        try {
            setClassPath();
            return typeReferenceToType (((Method)javaElement).getTypeName ());        
        } catch (InvalidObjectException e) {
            return Type.VOID;
        } finally {
            repository.endTrans(false);
        }
    }
    
    // Setters
    ///////////////////////////////////////////////////////////////////////////////

    /**
     * Sets the return type.
     */
    public void setReturn(Type newType) throws SourceException {                
        checkWritable();
        checkDocument();
        boolean failed = true;
        repository.beginTrans (true);
        try {
            setClassPath();
            Type oldType = getReturn ();
            newType = resolveType(newType);
            if (compareSourceTypes(oldType, newType)) {
                failed = false;
                checkIsValid ();
                return;
            }            
            PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_RETURN, oldType, newType);

            checkReturnTypeConstraints(newType);
            checkVetoablePropertyChange(evt);
            
            ((Method) javaElement).setTypeName (typeToTypeReference (newType));
            failed = false;
        } catch (InvalidObjectException e) {
            throwIsInvalid ();
        } finally {
            repository.endTrans (failed);
        }
    }
    
    public void fireReturnChange (Type oldType, Type newType) {
        oldType = resolveType (oldType);
        newType = resolveType (newType);
        if (compareSourceTypes(oldType, newType))
            return;
        
        PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_RETURN, oldType, newType);
        fireOwnPropertyChange(evt);
        
        MethodElement elem = (MethodElement) cloneSelf ();                            
        try {
            MethodElement.Impl impl = (MethodElement.Impl) elem.getCookie (Element.Impl.class);
            impl.setReturn (oldType);
        } catch (SourceException e) {
            e.printStackTrace ();
        }
        notifyConnectionChange (elem);
    }
    
    // Utility and helper methods.
    ///////////////////////////////////////////////////////////////////////////////
    protected final Element createWrapperElement() {
        return new MethodElement(this, getDeclaringClass());
    }
    
    protected void checkReturnTypeConstraints(Type t) throws SourceException {
        // no checking, no constraings on the rettype.
    }

    // Serialization support
    ///////////////////////////////////////////////////////////////////////////////
    public Object readResolve() {
        return null;
    }
    
    public String toString() {
        return "MethodElementImpl[" + getName().getSourceName() + ", " + // NOI18N
            getParameters().length + " args]"; // NOI18N
    }
    
    protected Element cloneSelf() {
        MethodElement clone = new MethodElement();
        copyCallableProperties(clone);
        try {
            clone.setReturn(getReturn());
        } catch (SourceException ex) {
        }
        return clone;
    }
    
    // ..........................................................................

    static class MethodListener extends CallableImpl.CallableListener implements MDRPreChangeListener {                                

        private Type type;
        private Map eventsMap = new HashMap();
        private Set typeRefs = new HashSet();
        
        MethodListener (MethodElementImpl impl) {
            super (impl);
        }
        
        public void connect () {
            if (REGISTER_LISTENER) {
                super.connect ();
                TypeReference typeRef = ((Method) javaElement).getTypeName();
                if (typeRef != null) {
                    type = ((MethodElementImpl) getImpl ()).typeReferenceToType (typeRef);
                    while (typeRef != null) {
                        ((MDRChangeSource) typeRef).addListener(this);
                        typeRefs.add(typeRef);
                        typeRef = typeRef.getParent();
                    }
                } else {
                    type = null;
                }
            }
        }
        
        public void remove () {
            super.remove ();
            for (Iterator iter = typeRefs.iterator(); iter.hasNext();) {
                try {
                    ((MDRChangeSource) iter.next()).removeListener(this);
                } catch (InvalidObjectException e) {
                }
            }
            typeRefs.clear();
        }
        
        public void doChange(MDRChangeEvent event) {
            super.doChange (event);

            if (event instanceof AttributeEvent) {
                AttributeEvent attrEvent = (AttributeEvent) event;
                String attrName = attrEvent.getAttributeName();
                Object source = event.getSource();
                if (attrName.equals("typeName") || ((source instanceof TypeReference) && // NOI18N
                    (attrName.equals("name") || attrName.equals("dimCount") || attrName.equals("parent") || attrName.equals("typeArguments")))) { // NOI18N
                    EventInfo info = (EventInfo) eventsMap.get(event);
                    Type oldType = type;
                    type = ((MethodElementImpl) getImpl ()).stringToType (info.newName);
                    processEventInfo(info);
                    ((MethodElementImpl) impl).fireReturnChange (oldType, type);
                }
            }
        }
        
        private void processEventInfo(EventInfo info) {
            for (Iterator iter = info.removed.iterator(); iter.hasNext();) {
                Object obj = iter.next();
                ((MDRChangeSource) obj).removeListener(this);
                typeRefs.remove(obj);
            }
            for (Iterator iter = info.added.iterator(); iter.hasNext();) {
                Object obj = iter.next();
                ((MDRChangeSource) obj).addListener(this);
                typeRefs.add(obj);
            }
        }
        
        private TypeReference getRoot(TypeReference typeRef) {
            Object comp = typeRef.refImmediateComposite();
            while (comp instanceof TypeReference) {
                typeRef = (TypeReference)comp;
                comp = typeRef.refImmediateComposite();
            }
            return typeRef;
        }
        
        private void typeRefToName(StringBuffer buf, TypeReference typeRef) {
            TypeReference parent = typeRef.getParent();
            if (parent != null) {
                typeRefToName(buf, parent);
                buf.append('.');
            }
            buf.append(typeRef.getName());
        }
        
        private String typeRefToName(TypeReference typeRef) {
            if (typeRef == null)
                return null;
            int dimCount = 0;
            if (typeRef instanceof ArrayReference) {
                dimCount = ((ArrayReference) typeRef).getDimCount();
                typeRef = typeRef.getParent();
            }
            StringBuffer buf = new StringBuffer();
            typeRefToName(buf, typeRef);
            if (buf.length() > 0) {
                while (dimCount > 0) {
                    buf.append("[]"); // NOI18N
                    dimCount--;
                }
            }
            return buf.toString();
        }
        
        private void collectTypeRefs(List list, TypeReference typeRef) {
            while (typeRef != null) {
                list.add(typeRef);
                typeRef = typeRef.getParent();
            }
        }
        
        public void changeCancelled(MDRChangeEvent e) {
            eventsMap.remove(e);
        }
        
        public void plannedChange(MDRChangeEvent event) {
            RefBaseObject source = (RefBaseObject) event.getSource ();            
            if (event instanceof AttributeEvent) {
                AttributeEvent attrEvent = (AttributeEvent) event;
                String attrName = attrEvent.getAttributeName();
                EventInfo info = new EventInfo();
                if (attrName.equals ("typeName")) { // NOI18N
                    TypeReference prev = (TypeReference) attrEvent.getOldElement ();
                    TypeReference curr = (TypeReference) attrEvent.getNewElement ();
                    if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_SET)) {
                        collectTypeRefs(info.removed, prev);
                        collectTypeRefs(info.added, curr);
                        info.newName = typeRefToName(curr);
                    } else if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_REMOVE)) {
                        collectTypeRefs(info.removed, prev);
                        info.newName = null;
                    } else { // EVENT_ATTRIBUTE_ADD
                        collectTypeRefs(info.added, curr);
                        info.newName = typeRefToName(curr);
                    }
                    eventsMap.put(event, info);
                } else if ((source instanceof TypeReference) && (attrName.equals("name"))) { // NOI18N
                    info.newName = typeRefToName(getRoot((TypeReference) source));
                    eventsMap.put(event, info);
                } else if (attrName.equals("parent")) { // NOI18N
                    TypeReference prev = (TypeReference) attrEvent.getOldElement ();
                    TypeReference curr = (TypeReference) attrEvent.getNewElement ();
                    if (prev != null)
                        collectTypeRefs(info.removed, prev);
                    if (curr != null)
                        collectTypeRefs(info.added, curr);
                    info.newName = typeRefToName(getRoot((TypeReference) source));
                    eventsMap.put(event, info);
                } else if (attrName.equals("dimCount") || attrName.equals("typeArguments")) { // NOI18N
                    info.newName = typeRefToName(getRoot((TypeReference) source));
                    eventsMap.put(event, info);
                }
            }
        }
        
        class EventInfo {            
            List removed = new LinkedList();
            List added = new LinkedList();
            String newName;            
        }

    }

}
... 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.