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 javax.jmi.reflect.ConstraintViolationException;
import org.netbeans.jmi.javamodel.ConstructorInvocation;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.PrimaryExpression;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;
import org.netbeans.lib.java.parser.ParserTokens;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;

/**
 *
 * @author  Martin Matula
 */
public abstract class ConstructorInvocationImpl extends InvocationImpl implements ConstructorInvocation {
    private boolean hasSuper = false;
    private PrimaryExpression parentClass;
    private int type;
    
    /** Creates a new instance of ConstructorInvocationImpl */
    public ConstructorInvocationImpl(StorableObject o) {
        super(o);
    }
    
    public String getName() {
        return null;
    }
    
    public void setName(String name) {
        throw new ConstraintViolationException(null, null, "Cannot change name of constructor invocation."); // NOI18N
    }
    
    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 {
            if (type == 0) {
                type = getASTree().getSubTrees()[0].getType();
            }
            return type != ParserTokens.THIS;
        }
    }
    
    public void setHasSuper(boolean hasSuper) {
        objectChanged(CHANGED_HAS_SUPER);
        this.hasSuper = hasSuper;
    }
    
    public List getChildren() {
        List list = new ArrayList(4);
        addIfNotNull(list, getParentClass()); 
        list.addAll(super.getChildren());
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            ASTree[] parts = tree.getSubTrees();
            if (parts[0]==null) {
                parentClass = null;
            } else {
                int type = parts[0].getType();
                parentClass = (PrimaryExpression) (((type == ParserTokens.THIS) || (type == ParserTokens.SUPER)) ? null : initOrCreate(parentClass, parts[0]));
            }
            parameters = createChildrenList(parameters, "parameters", parts[3], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); // NOI18N
        }
        childrenInited = true;
    }
    
    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();
        buf.append('\n');
        buf.append(getIndentation());
        if (parentClass != null) {
            buf.append(parentClass.getSourceText());
            buf.append('.');
        }
        if (hasSuper) {
            buf.append("super"); // NOI18N
        } else {
            buf.append("this"); // NOI18N
        }            
        formatElementPart(PAR_OPEN_BRACKET);
        Iterator iter = params.iterator();
        while (iter.hasNext()) {
            StatementImpl par = (StatementImpl) iter.next();
            buf.append(par.getSourceText());
            if (iter.hasNext()) {
                formatElementPart(MetadataElement.COMMA, buf);
            }
        }
        formatElementPart(PAR_CLOSE_BRACKET);
        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[2].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);
        } else {
            getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[3], ASTreeTypes.ARGUMENT_LIST, getParameters(), parser.getToken(tree.getLastToken() - 1).getStartOffset(), formatElementPart(MetadataElement.COMMA));
        }
    }
    
    void setData(boolean hasSuper, PrimaryExpression parentClass, List parameters) {
        //if (name != null) throw new ConstraintViolationException(null, null, "Cannot set name of constructor invocation to a non-null value.");
        setData(name, parameters);
        changeChild(null, parentClass);
        this.parentClass = parentClass;
        this.hasSuper = hasSuper;
    }

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