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.ArrayList;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.Token;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;
import org.netbeans.modules.javacore.parser.ElementInfo;
import org.netbeans.modules.javacore.parser.ParameterInfo;
import org.netbeans.modules.javacore.parser.TypeRef;
import org.openide.util.Utilities;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Implementation of Parameter object instance interface.
 *
 * @author Vladimir Hudec
 * @author Pavel Flaska
 */
public abstract class ParameterImpl extends SemiPersistentElement implements Parameter {
    private static final ElementInfo DEFAULT_INFO = new ParameterInfo(null, ParameterInfo.PARAMETER_TYPE, null, false, null, false);
    
    protected TypeReference typeName = null;
    protected int dimCount = 0;
    
    protected ParameterImpl(StorableObject s) {
        super(s);
    }
    
    /** Should be overriden by elements that have persistent attributes.
     * They should implement this method to compare values in newly passed AST info with
     * values of the persistent attributes and if the values do not match, they should be
     * updated and proper events should be fired.
     */
    protected void matchPersistent(ElementInfo info) {
        super.matchPersistent(info);
        ParameterInfo pinfo = (ParameterInfo) info;
        
        if (pinfo.isFinal != isFinal()) {
            setFinal(pinfo.isFinal);
        }
        if (pinfo.isVarArg != isVarArg()) {
            setVarArg(pinfo.isVarArg);
        }
        
        if (!isPersisted()) {
            setPersisted(true);
            persist();
            setTypeRef(pinfo.type);
        } else {
            if (!Utilities.compareObjects(pinfo.type, getTypeRef())) {
                setType(resolveType(pinfo.type));
            }    
        }
    }
    
    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }
    
    protected void resetChildren() {
        if (typeName != null) {
            TypeReference temp = typeName;
            typeName = null;
            temp.refDelete();
        }
        childrenInited = false;
    }
    
    /**
     * Returns the value of reference type.
     * @return Value of reference type.
     */
    public Type getType() {
        return resolveType(getTypeRef());
    }

    /**
     * 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) {
        TypeRef tr = typeToTypeRef(newValue);
        _setTypeName((TypeReference) typeRefToTypeReference(tr), tr);
    }
    
    public TypeReference getTypeName() {
        if (!childrenInited) {
            initChildren();
        }
        return typeName;
    }

    public void setTypeName(TypeReference typeName) {
        _setTypeName(typeName, typeReferenceToTypeRef(typeName));
    }
    
    private void _setTypeName(TypeReference typeName, TypeRef typeRef) {
        objectChanged(CHANGED_TYPE);
        if (childrenInited) {
            changeChild(getTypeName(), typeName);
            this.typeName = typeName;
        }
        setTypeRef(typeRef);
    }
    
    /**
     * Set the value of isFinal attribute. See {@link #isFinal} for description 
     * on the attribute.
     *
     * @param newValue New value to be set.
     */
    public void setFinal(boolean newValue) {
        objectChanged(CHANGED_IS_FINAL);
        super_setFinal(newValue);
    }

    protected abstract void super_setFinal(boolean newValue);

    /**
     * Set the value of isVarArg attribute. See {@link #isVarArg} for description 
     * on the attribute.
     *
     * @param newValue New value to be set.
     */
    public void setVarArg(boolean newValue) {
        objectChanged(CHANGED_IS_VARARG);
        super_setVarArg(newValue);
    }
    
    protected abstract void super_setVarArg(boolean newValue);
    
    protected ASTree getPartTree(ElementPartKind part) {
        if (ElementPartKindEnum.NAME.equals(part)) {
            return getASTree().getSubTrees()[VARIABLE_DECLARATOR_ID].getSubTrees()[0];
        }
        throw new IllegalArgumentException("Invalid part for this element: " + part); // NOI18N
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        
        StringBuffer buf = new StringBuffer();
        if (isFinal())
            buf.append("final "); // NOI18N
        buf.append(((MetadataElement) getTypeName()).getSourceText());
         if (isVarArg())
            buf.append("..."); // NOI18N
        buf.append(' ');
        buf.append(getName());
        return buf.toString();
    }
    
    public void getDiff(List diffList) {
        // parameter exist
        ASTProvider parser = getParser();
        ASTree[] children = getASTree().getSubTrees();
        if (isChanged(CHANGED_IS_FINAL)) {
            int startOffset, endOffset = parser.getToken(children[TYPE].getFirstToken()).getStartOffset();
            if (isFinal()) {
                if (children[FINAL_OPT] == null) {
                    startOffset = endOffset;
                    diffList.add(new DiffElement(startOffset, endOffset, "final ")); // NOI18N
                }
            }
            else {
                if (children[FINAL_OPT] != null) {
                    startOffset = parser.getToken(children[FINAL_OPT].getFirstToken()).getStartOffset(); 
                    diffList.add(new DiffElement(startOffset, endOffset, ""));
                }
            }
        }
        // type print
        getChildDiff(diffList, parser, children[TYPE], (MetadataElement) getTypeName(), CHANGED_TYPE);
        if (isChanged(CHANGED_IS_VARARG)) {
            replaceNode(diffList, parser, children[VARARG_OPT], isVarArg() ? "..." : "", parser.getToken(children[TYPE].getLastToken()).getEndOffset(), ""); // NOI18N
        }
        // name print
        if (isChanged(CHANGED_NAME)) {
            ASTree[] varDeclaratorIdChildren = children[VARIABLE_DECLARATOR_ID].getSubTrees();
            Token identifier = (Token) varDeclaratorIdChildren[0]; // identifier
            int startOffset = identifier.getStartOffset();
            int endOffset = identifier.getEndOffset();
            diffList.add(new DiffElement(startOffset, endOffset, getName()));
        }
    }
    
    private static final int FINAL_OPT = 0;
    private static final int TYPE = 1;
    private static final int VARARG_OPT = 2;
    private static final int VARIABLE_DECLARATOR_ID = 3;

    public void replaceChild(Element oldElement, Element newElement) {
        if (childrenInited && oldElement.equals(typeName)) {
            setTypeName((TypeReference) newElement);
            return;
        }
        super.replaceChild(oldElement, newElement);
    }

    public int getDimCount() {
        if (isChanged(CHANGED_DIM_COUNT)) {
            return dimCount;
        } else {
            ASTree dims = getASTree().getSubTrees()[1];
            if (dims == null) {
                return 0;
            } else {
                return (dims.getLastToken() - dims.getFirstToken() + 1) / 2;
            }
        }
    }
    
    public void setDimCount(int dimCount) {
        objectChanged(CHANGED_DIM_COUNT);
        this.dimCount = dimCount;
    }
    
    public List getChildren() {
        List list = new ArrayList(1);
        addIfNotNull(list, getTypeName());
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ParameterInfo info = (ParameterInfo) getElementInfo();
        ASTree tree = info.getTypeAST(this);
        typeName = (TypeReference) initOrCreate(typeName, tree);
        childrenInited = true;
    }
    
    protected void setData(List annotations, TypeReference typeName, int dimCount) {
        // todo: annotations
        changeChild(null, typeName);
        this.typeName = typeName;
        
        this.dimCount = dimCount;
    }
    
    protected void setTypeRef(TypeRef type) {
        _getDelegate().setSlot1(type);
    }
    
    public TypeRef getTypeRef() {
        return (TypeRef) _getDelegate().getSlot1();
    }
    
    public boolean isPersisted() {
        return _getDelegate().getSlot2() != null;
    }
    
    public void setPersisted(boolean persisted) {
        _getDelegate().setSlot2(persisted ? "" : null);
    }
}
... 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.