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.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jmi.reflect.ConstraintViolationException;
import javax.jmi.reflect.RefFeatured;
import org.netbeans.jmi.javamodel.ClassDefinition;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.EnumConstant;
import org.netbeans.jmi.javamodel.InitialValue;
import org.netbeans.jmi.javamodel.JavaDoc;
import org.netbeans.jmi.javamodel.Resource;
import org.netbeans.jmi.javamodel.Type;
import org.netbeans.jmi.javamodel.TypeReference;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;
import org.netbeans.lib.java.parser.ParserTokens;
import org.netbeans.lib.java.parser.Token;
import org.netbeans.mdr.handlers.BaseObjectHandler;
import org.netbeans.mdr.persistence.StorageException;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.ClassIndex;
import org.netbeans.modules.javacore.JMManager;
import org.netbeans.modules.javacore.parser.ASTProvider;
import org.netbeans.modules.javacore.parser.ElementInfo;
import org.netbeans.modules.javacore.parser.FeatureInfo;
import org.openide.ErrorManager;

/** Implements object representing enum constant.
 * 
 * @author Martin Matula
 */
public abstract class EnumConstantImpl extends FeatureImpl implements EnumConstant {
    private static final ElementInfo DEFAULT_INFO = new FeatureInfo(null, FeatureInfo.ENUM_CONSTANT_TYPE, null, 0, null);

    RefFeatured parent; // used for hard-referencing parent
    
    // attribute values
    ClassDefinition body;
    InitialValue initialValue;
    String initialValueText;
    
    private boolean initValueInited = false;


    /** Creates a new instance of EnumConstantImpl */
    public EnumConstantImpl(StorableObject s) {
        super(s);
    }

    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }
    
    public void setModifiers(int modifiers) {
        throw new ConstraintViolationException(null, null, "Cannot set modifiers of enum constant."); // NOI18N
    }
    
    public int getModifiers() {
        return Modifier.FINAL | Modifier.PUBLIC | Modifier.STATIC;
    }
    
    public boolean isFinal() {
        return true;
    }
    
    public void setFinal(boolean isFinal) {
        setModifiers(0);
    }

    /**
     * Returns the value of reference type.
     * @return Value of reference type.
     */
    public Type getType() {
        return (Type) refImmediateComposite();
    }

    /**
     * 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) {
        throw new ConstraintViolationException(null, null, "Cannot change enum constant type."); // NOI18N
    }

    /**
     * Find referenced resources using ClassIndex.findResourcesForIdentifier().
     * Modifiers are considered to reduce number of resources
     */
    Resource[] findReferencedResources() {
        return ClassIndex.findResourcesForIdentifier(getName());
    }

    public List getChildren() {
        List list = new ArrayList(2);
        if (isChanged(CHANGED_INITIAL_VALUE) && initialValueText != null) {
            throw new IllegalStateException("Cannot get children when the initial value text has been changed."); // NOI18N
        }
        addIfNotNull(list, getInitialValue());
        addIfNotNull(list, getBody());
        return list;
    }

    protected void parentChanged() {
        parent = refImmediateComposite();
        if (parent == null || ((BaseObjectHandler) parent)._getDelegate() instanceof DeferredObject) {
            if (!(_getDelegate() instanceof DeferredObject)) mutate(true); // make object transient
            parent = null;
        } else {
            if (_getDelegate() instanceof DeferredObject) mutate(false); // make object persistent
        }
    }

    protected void initChildren() {
        if (initValueInited) {
            JMManager.getTransactionMutex().addBFeatureToInitQueue(this);
        } else {
            childrenInited = false;
            initInitValue();
        }
        childrenInited = true;
    }
    
    public void initInitValue() {
        initValueInited = false;
        FeatureInfo initValInfo = (FeatureInfo) getElementInfo();
        if (initValInfo != null) {
            initValInfo.doAttribution(this);
            ASTree tree = getASTree();
            if (tree != null) {
                ASTree[] parts = tree.getSubTrees();
                initialValue = (InitialValue) initOrCreate(initialValue, parts[PARAMETERS]);
                if (parts[2] == null) {
                    body = null;
                } else {
                    body = (ClassDefinition) getParser().getSemanticInfo(parts[CLASS_DEFINITION], this);
                    changeChild(null, body);
                }
            }
        }
        initValueInited = true;
    }

    private static final int NAME = 0;
    private static final int PARAMETERS = 1;
    private static final int CLASS_DEFINITION = 2;

    protected void matchModifiers(ElementInfo info) {
        // do not match modifiers
    }
    
    /** The method has to make sure that the AST infos of children are also updated.
     */
    protected void matchElementInfo(ElementInfo newInfo) {
        super.matchElementInfo(newInfo);
        resetChildren();
    }

    protected void resetChildren() {
        if (initialValue != null) {
            InitialValue temp = initialValue;
            initialValue = null;
            changeChild(temp, null);
            temp.refDelete();
        }

        if (body != null) {
            ClassDefinition temp = body;
            body = null;
            changeChild(temp, null);
            temp.refDelete();
        }
        initValueInited = false;
        childrenInited = false;
    }

    public void replaceChild(Element oldElement, Element newElement) {
        if (childrenInited) {
            if (oldElement.equals(initialValue)) {
                setInitialValue((InitialValue) newElement);
                return;
            }
            if (oldElement.equals(body)) {
                setBody((ClassDefinition) newElement);
                return;
            }
        }
        super.replaceChild(oldElement, newElement);
    }

    protected void setData(List annotations, String javadocText, JavaDoc javadoc, InitialValue initialValue, String initialValueText, ClassDefinition body) {
        super.setData(annotations, javadocText, javadoc);
        if (initialValueText == null) {
            changeChild(null, initialValue);
            this.initialValue = initialValue;
        } else {
            if (initialValue != null) {
                throw new ConstraintViolationException(null, null, "Cannot set both initialValue and initialValueText."); // NOI18N
            }
            this.initialValueText = initialValueText;
        }
        changeChild(null, body);
        this.body = body;
        initValueInited = true;
        childrenInited = true;
    }

    public String getSourceText() {
        // if and only if the element is not part of the group.
        // Otherwise we want to generate all the source text.
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;

        // we have element which is new or changed and moved.
        StringBuffer buf = new StringBuffer();
        generateNewJavaDoc(buf);
        buf.append(getName());
        if (initialValueText != null) {
            buf.append('(').append(initialValueText).append(')');
        } else if (initialValue != null) {
            buf.append('(').append(((MetadataElement) initialValue).getSourceText()).append(')');
        }
        if (body != null) {
            buf.append(' ').append(((MetadataElement) body).getSourceText());
        }
        return buf.toString();
    }

    public void getDiff(List diffList) {
        // element was present in the original source code
        ASTProvider parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();

        // javadoc print
        replaceJavaDoc(diffList);
        if (isChanged(CHANGED_NAME)) {
            replaceNode(diffList, parser, children[NAME], getName(), 0, null);
        }
        if (isChanged(CHANGED_INITIAL_VALUE)) {
            int startOffset = 0, endOffset = 0;
            String text = initialValue == null ? initialValueText :
                ((MetadataElement) getInitialValue()).getSourceText();
            Token lpar = parser.getToken(children[0].getLastToken() + 1);
            // there was an initial value in original source code.
            if (lpar.getType() == ParserTokens.L_PAR) {
                startOffset = lpar.getStartOffset();
                Token endToken = parser.getToken(children[1] != null ? children[1].getLastToken() + 1 : children[0].getLastToken() + 2);
                endOffset = endToken.getEndOffset();
            }
            // there was no initial value in original source
            else {
                startOffset = parser.getToken(children[0].getLastToken()).getEndOffset();
                // we are inserting new text, offsets contain the same value
                endOffset = startOffset;
            }
            diffList.add(new DiffElement(startOffset, endOffset, text == null ? "" : "(" + text + ")")); // NOI18N
        } else if (initialValue != null && ((MetadataElement) initialValue).isChanged()) {
            ((MetadataElement) initialValue).getDiff(diffList);
        }
        int startToken = children[PARAMETERS] == null ? children[0].getLastToken() : children[PARAMETERS].getLastToken() + 1;
        getChildDiff(diffList, parser, children[CLASS_DEFINITION], (MetadataElement) getBody(), CHANGED_ENUM_CONST_BODY, parser.getToken(startToken).getEndOffset(), " "); // NOI18N
    }

    public void setBody(ClassDefinition newValue) {
        if (!childrenInited) {
            initChildren();
        }
        objectChanged(CHANGED_ENUM_CONST_BODY);
        changeChild(body, newValue);
        body = newValue;
    }

    public ClassDefinition getBody() {
        if (!childrenInited) {
            initChildren();
        }
        return body;
    }

    public InitialValue getInitialValue() {
        if (isChanged(CHANGED_INITIAL_VALUE) && initialValueText != null) {
            throw new ConstraintViolationException(null, null, "Cannot ask for initial value after the initial value text was changed."); // NOI18N
        }
        if (!childrenInited) {
            initChildren();
        }
        return initialValue;
    }

    public void setInitialValue(InitialValue newValue) {
        if (!childrenInited) {
            initChildren();
        }
        changeChild(initialValue, newValue);
        initialValue = newValue;
        initialValueText = null;
        objectChanged(CHANGED_INITIAL_VALUE);
    }
    
    public java.lang.String getInitialValueText(){
        if (isChanged(CHANGED_INITIAL_VALUE)) {
            if (initialValue != null) {
                throw new ConstraintViolationException(null, null, "Cannot ask for initial value text after the initial value was changed."); // NOI18N
            }
            return initialValueText;
        } else {
            return extractInitialValueText();
        }
    }

    public void setInitialValueText(java.lang.String newValue){
        if (!childrenInited) {
            initChildren();
        }
        if (initialValue != null) {
            changeChild(initialValue, null);
        }
        objectChanged(CHANGED_INITIAL_VALUE);
        initialValueText = newValue;
        initialValue = null;
    }

    private String extractInitialValueText() {
        ASTProvider parser = getParser();
        if (parser == null)
            return null;
        ASTree initValue = getASTree().getSubTrees()[PARAMETERS];
        if (initValue == null)
            return null;
        return parser.getText(initValue);
    }

    public int getDimCount() {
        return 0;
    }

    public void setDimCount(int dimCount) {
        throw new ConstraintViolationException(null, null, "Cannot change enum constant dimCount."); // NOI18N
    }

    public TypeReference getTypeName() {
        return null;
    }

    public void setTypeName(TypeReference typeName) {
        throw new ConstraintViolationException(null, null, "Cannot change enum constant type name."); // NOI18N
    }
}
... 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.