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.javacore.jmiimpl.javamodel;

import java.util.*;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.MethodInvocation;
import org.netbeans.jmi.javamodel.NamedElement;
import org.netbeans.jmi.javamodel.PrimaryExpression;
import org.netbeans.jmi.javamodel.Type;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;


/**
 *
 * @author  Martin Matula
 */
public abstract class MethodInvocationImpl extends InvocationImpl implements MethodInvocation {
    private boolean hasSuper = false;
    private PrimaryExpression parentClass;
    
    /** Creates a new instance of MethodInvocationImpl */
    public MethodInvocationImpl(StorableObject o) {
        super(o);
    }
    
    public void setParentClass(PrimaryExpression expression) {
        objectChanged(CHANGED_PARENT_CLASS);
        changeChild(getParentClass(), expression);
        this.parentClass = expression;
    }
    
    public PrimaryExpression getParentClass() {
        if (!childrenInited) {
            initChildren();
        }
        return parentClass;
    }
    
    public boolean getHasSuper() {
        if (isChanged(CHANGED_HAS_SUPER)) {
            return hasSuper;
        } else {
            return getASTree().getSubTrees()[1] != null;
        }
    }
    
    public void setHasSuper(boolean hasSuper) {
        objectChanged(CHANGED_HAS_SUPER);
        this.hasSuper = hasSuper;
    }
    
    public List getChildren() {
        List list = new ArrayList();
        addIfNotNull(list, getParentClass()); 
        list.addAll(super.getChildren());
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            ASTree[] parts = tree.getSubTrees();
            parentClass = (PrimaryExpression) initOrCreate(parentClass, parts[0]);
            parameters = createChildrenList(parameters, "parameters", parts[4], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); // NOI18N
        }
        childrenInited = true;
    }
    
     /**
     * Returns the value of reference type.
     * @return Value of reference type.
     */
    public Type getType() {
        Object semInfo = getParser().getSemanticInfo(getASTree(), this);
        
        if (semInfo instanceof Method)
            return ((Method)semInfo).getType();
        return null;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        StringBuffer buf = new StringBuffer();
        boolean hasSuper = getHasSuper ();
        StatementImpl parentClass = (StatementImpl) getParentClass();
        List params = getParameters();
        String methodName = getName();
        if (parentClass != null) {
            buf.append(parentClass.getSourceText());
            buf.append("."); // NOI18N
        }
        if (hasSuper) {
            buf.append("super."); // NOI18N
        }
        buf.append(methodName);
        formatElementPart(STMT_OPEN_BRACKET, buf);
        Iterator iter = params.iterator();
        while (iter.hasNext()) {
            StatementImpl par = (StatementImpl) iter.next();
            buf.append(par.getSourceText());
            if (iter.hasNext()) {
                formatElementPart(MetadataElement.COMMA, buf);
            }
        }
        formatElementPart(STMT_CLOSE_BRACKET, buf);
        return buf.toString();
    }

    public void getDiff(List diff) {
        ASTProvider parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();
        
        if (isChanged(CHANGED_HAS_SUPER)) {
            int endOffset = parser.getToken(children[3].getFirstToken()-1).getEndOffset();
            int startOffset = children[0]!=null?parser.getToken(children[1].getLastToken()).getEndOffset():endOffset;
            diff.add(new DiffElement(startOffset, endOffset, isHasSuper()?"super":"")); //NOI18N
        }
        
        MetadataElement parentClass = (MetadataElement) getParentClass();
        if (isChanged(CHANGED_PARENT_CLASS) || (parentClass != null && parentClass.isChanged())) {
            getChildDiff(diff, parser, children[0], parentClass, CHANGED_PARENT_CLASS);
            //replaceNode(diff, parser, tree, getSourceText(), 0, null);
        }
        if (isChanged(CHANGED_NAME)) {
            replaceNode(diff, parser, children[3], getName(), 0, null);
        }
        getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[4], ASTreeTypes.ARGUMENT_LIST, getParameters(), 
            parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(MetadataElement.COMMA));
}
    
    void setData(String name, List parameters, PrimaryExpression parentClass, boolean hasSuper) {
        setData(name, parameters);
        changeChild(null, parentClass);
        this.parentClass = parentClass;
        this.hasSuper = hasSuper;
    }

    protected ASTree getNameAST() {
        return getASTree().getSubTrees()[3];
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChild(parentClass);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (oldElement.equals(parentClass)) {
                setParentClass((PrimaryExpression)newElement);
            } else
                super.replaceChild(oldElement,newElement);
        }
    }
}
... 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.