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 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;
import org.netbeans.modules.javacore.parser.MDRParser;
import java.util.*;
import org.netbeans.jmi.javamodel.*;

/**
 *
 * @author  Martin Matula
 */
public abstract class LocalVarDeclarationImpl extends StatementImpl implements LocalVarDeclaration {
    private LightAttrList variables;
    private boolean isFinal;
    private TypeReference typeName;
    
    /** Creates a new instance of LocalVarDeclarationImpl */
    public LocalVarDeclarationImpl(StorableObject o) {
        super(o);
    }
    
    public boolean isFinal() {
        if (isChanged(CHANGED_IS_FINAL)) {
            return isFinal;
        } else {
            return getASTree().getSubTrees()[0] != null;
        }
    }

    public void setFinal(boolean isFinal) {
        objectChanged(CHANGED_IS_FINAL);
        this.isFinal = isFinal;
    }
    
    public TypeReference getTypeName() {
        if (!childrenInited) {
            initChildren();
        }
        return typeName;
    }

    public void setTypeName(TypeReference typeName) {
        objectChanged(CHANGED_TYPE);
        changeChild(getTypeName(), typeName);
        this.typeName = typeName;
    }

    public Type getType() {
        return (Type) getTypeName().getElement();
    }
    
    public void setType(Type newValue) {
        MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId();
        setTypeName(proxy.createMultipartId(newValue.getName(), null, null));
    }

    public List getVariables() {
        if (!childrenInited) {
            initChildren();
        }
        return variables;
    }
    
    public List getChildren() {
        List result = new ArrayList(getVariables().size() + 1);
        addIfNotNull(result, getTypeName());
        result.addAll(getVariables());
        return result;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            typeName = (TypeReference) initOrCreate(typeName, tree.getSubTrees()[1]);
            variables = createChildrenList(variables, "variables", tree.getSubTrees()[2], ASTreeTypes.VARIABLE_DECLARATORS, CHANGED_VARIABLES, false); // NOI18N
        }
        childrenInited = true;
    }

    public String getSourceText() {
        boolean useSemicolon = !(refImmediateComposite() instanceof ForStatement);
        if (!isNew()) {
            if (!isChanged()) {
                MDRParser parser = getParser();
                ASTree tree = getASTree();
                String origSrc = parser.getSourceText();
                int startIndex, endIndex;
                String semicolon;
                String indentation;
                boolean isSemicolon = getParser().getToken(tree.getLastToken()).getType() == ParserTokens.SEMICOLON;
                if (useSemicolon == isSemicolon) {
                    semicolon = indentation = "";
                    startIndex = getBeginIndex();
                    endIndex = getEndIndex();
                } else {
                    startIndex = getStartOffset(parser, tree, false);
                    if (useSemicolon) {
                        semicolon = ";"; // NOI18N
                        indentation = getIndentation();
                        endIndex = getEndIndex();
                    } else {
                        indentation = semicolon = "";
                        endIndex = getEndOffset(parser, parser.getToken(tree.getLastToken() - 1));
                    }
                }
                return indentation + origSrc.substring(startIndex, endIndex) + semicolon;
            } else {
                if (!childrenInited) {
                    initChildren();
                }
            }
        }
        StringBuffer buf = new StringBuffer();

        if (useSemicolon) {
            buf.append(getIndentation());
        }
        if (isFinal()) {
            buf.append("final "); // NOI18N
        }
        TypeReference typeName = getTypeName();
        if (typeName != null) {
            buf.append(((MetadataElement) typeName).getSourceText());
        } else {
            buf.append(getType().getName());
        }
        buf.append(' ');
        Iterator iter = getVariables().iterator();
        while (iter.hasNext()) {
            TransientElement var = (TransientElement) iter.next();
            buf.append(var.getSourceText());
            if (iter.hasNext()) {
                formatElementPart(MetadataElement.COMMA, buf);
            }
        }
        if (useSemicolon) buf.append(';');
        return buf.toString();
    }

    public void getDiff(List diff) {
        ASTProvider parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();

        if (isChanged(CHANGED_IS_FINAL)) {
            ASTree node = null;
            String text = null;
            if (children[0] == null) {
                if (isFinal()) {
                    node = children[1];
                    text = "final "; // NOI18N
                }
            } else {
                if (!isFinal()) {
                    node = children[0];
                    text = "";
                }
            }
            if (node != null) {
                int start = parser.getToken(node.getFirstToken()).getStartOffset();
                int end = parser.getToken(children[1].getFirstToken()).getStartOffset();
                diff.add(new DiffElement(start, end, text));
            }
        }
        getChildDiff(diff, parser, children[1], (MetadataElement) getTypeName(), CHANGED_TYPE);
        getCollectionDiff(diff, parser, CHANGED_VARIABLES, children[2],
            ASTreeTypes.VARIABLE_DECLARATORS, getVariables(), 
            parser.getToken(children[2].getLastToken()).getEndOffset(), formatElementPart(MetadataElement.COMMA));
    }
    
    void setData(List variables, boolean isFinal, TypeReference typeName) {
        this.variables = createChildrenList("variables", variables, CHANGED_VARIABLES); // NOI18N
        this.isFinal = isFinal;
        changeChild(null, typeName);
        this.typeName = typeName;
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChildren(variables);
            deleteChild(typeName);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement)  {
        if (childrenInited) {
            List vars=getVariables();
            int index=vars.indexOf(oldElement);
            
            if (index!=-1) {
                if (newElement==null)
                    vars.remove(index);
                else
                    vars.set(index, newElement);
            } else if (oldElement.equals(typeName)) {
                setTypeName((TypeReference)newElement);
            }
        }
    }

//    /**
//     * Returns the value of reference type.
//     * @return Value of reference type.
//     */
//    public Type getType() {
//        if (isChanged(CHANGED_TYPE))
//            return type;
//        else {
//            return (Type) getParser().getSemanticInfo(getASTree());
//        }
//    }

    /**
     * Sets the value of reference type. See {@link #getType} for description 
     * on the reference.
     * @param newValue New value to be set.
     */
//    public void setType(Type newValue) {
//        this.type = newValue;
//        objectChanged(CHANGED_TYPE);
////        Type oldType = getType();
////        if (oldType != null && newValue != null && oldType.equals(newValue))
////            return;
////
////        IsOfTypeImpl isOfTypeImpl = (IsOfTypeImpl)(((JavaModelPackage) refImmediatePackage()).getIsOfType());
////
////        if (oldType != null) {
////            isOfTypeImpl.remove(oldType, this);
////        }
////        if (newValue != null) {
////            isOfTypeImpl.add(newValue, this);
////        }
//    }
    
//    public void _setType(Type newValue) {
//        //objectChanged(CHANGED_TYPE);
//        type = newValue;
//    }
}
... 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.