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.*;
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.MDRParser;

/**
 *
 * @author Martin Matula
 */
public abstract class MultipartIdImpl extends TypeReferenceImpl implements MultipartId {
    private LightAttrList typeArguments;

    /** Creates a new instance of MultipartIdImpl */
    public MultipartIdImpl(StorableObject o) {
        super(o);
    }

    void setData(String name, MultipartId parent, List typeArguments) {
        super.setData(name, parent);
        this.typeArguments = createChildrenList("typeArguments", typeArguments, CHANGED_TYPE_ARGUMENTS); // NOI18N
    }

    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();
        typeArguments = createChildrenList(typeArguments, "typeArguments", children == null || children.length < 3 ? null : children[2], ASTreeTypes.TYPE_ARGUMENTS, CHANGED_TYPE_ARGUMENTS, false); // NOI18N
        super.initChildren();
    }

    protected ASTree getNameAST() {
        ASTree tree = getASTree();
        if (tree.getType() == ASTreeTypes.MULTI_PART_ID) {
            tree = tree.getSubTrees()[1];
        }
        return tree;
    }

    public List getTypeArguments() {
        if (!childrenInited) {
            initChildren();
        }
        return typeArguments;
    }

    public String getSourceText() {
        StringBuffer buf = new StringBuffer();
        MetadataElement parent = (MetadataElement) getParent();
        if (parent != null) {
            buf.append(parent.getSourceText() + '.');
        }
        buf.append(getName());
        generateNewTypeArguments(buf);
        return buf.toString();
    }

    private void generateNewTypeArguments(StringBuffer buf) {
        if (!typeArguments.isEmpty()) {
            buf.append('<');
            Iterator it = typeArguments.iterator();
            while (it.hasNext()) {
                buf.append(((MetadataElement) it.next()).getSourceText());
                if (it.hasNext()) {
                    formatElementPart(MetadataElement.COMMA, buf);
                }
            }
            buf.append('>');
        }
    }

    public void getDiff(List diff) {
        MDRParser parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();
        ASTree nameAST = getNameAST();
        
        if (isChanged(CHANGED_PARENT) && children != null && children.length > 0 && getParent()==null) {
            // parent was there, but is not here any more
            int start = getStartOffset(parser, children[0], false);
            int end = getEndOffset(parser,children[0]);
            diff.add(new DiffElement(start, end + 1, ""));
        } else {
            getChildDiff(diff, parser, (children == null || children.length == 0) ? null : children[0], (MetadataElement) getParent(), CHANGED_PARENT, getStartOffset(parser, tree, false), "");
        }

        if (isChanged(CHANGED_NAME)) {
            replaceNode(diff, parser, nameAST, getName(), 0, null);
        }

        if (children == null || children.length < 3 || children[2] == null) {
            if (isChanged(CHANGED_TYPE_ARGUMENTS)) {
                StringBuffer buf = new StringBuffer();
                generateNewTypeArguments(buf);
                int endOffset = getEndOffset(parser, getNameAST());
                diff.add(new DiffElement(endOffset, endOffset, buf.toString()));
            }
        } else if (getTypeArguments().isEmpty()) {
            if (isChanged(CHANGED_TYPE_ARGUMENTS)) {
                replaceNode(diff, parser, children[2], "", 0, null);
            }
        } else {
            getCollectionDiff(diff, parser, CHANGED_TYPE_ARGUMENTS, children[2], ASTreeTypes.TYPE_ARGUMENTS, getTypeArguments(), parser.getToken(children[2].getLastToken()).getStartOffset(), ", "); // NOI18N
        }
    }

    public NamedElement getElement() {
        ASTree tree = getASTree();
        if (tree == null) {
            // [PENDING] remove this branch when semantic info for new elements is fixed
            TypeClass typeClass = ((JavaModelPackage) refImmediatePackage()).getType();
            return typeClass.resolve(getName());
        } else {
            MDRParser parser = (MDRParser) tree.getASTContext();
            switch (tree.getType()) {
                case ASTreeTypes.MULTI_PART_ID:
                    return (NamedElement) parser.getSemanticInfo(tree.getSubTrees()[1], this);
                case ParserTokens.IDENTIFIER:
                    return (NamedElement) parser.getSemanticInfo(tree, this);
                case ASTreeTypes.PRIMITIVE_TYPE:
                    return ((JavaModelPackage) refImmediatePackage()).getType().resolve(getName());
                default:
                    throw new IllegalArgumentException("Illegal tree type " + tree.getType() + "."); // NOI18N
            }
        }
    }

    /**
     * Creates a copy of this MultipartId instance
     */
    public Element duplicate() {
        return ((MultipartIdClass) this.refClass()).createMultipartId(
            getName(),
            duplicateParent(),
            duplicateTypeArguments()
        );
    }

    private List duplicateTypeArguments() {
        ArrayList result = new ArrayList(getTypeArguments().size());

        for (Iterator it = getTypeArguments().iterator(); it.hasNext();) {
            result.add(((Element) it.next()).duplicate());
        }

        return result;
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChildren(typeArguments);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }

    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (replaceObject(getTypeArguments(),oldElement,newElement)) return;
            super.replaceChild(oldElement,newElement);
        }
    }

    public List getChildren() {
        List list = super.getChildren();
        list.addAll(getTypeArguments());
        return list;
    }
}
... 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.