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.List;
import java.util.ListIterator;
import javax.jmi.reflect.ConstraintViolationException;
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.Token;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.JMManager;
import org.netbeans.modules.javacore.parser.*;
import org.openide.util.Utilities;


/** Implementation of Field object instance interface.
 *
 * @author Vladimir Hudec
 * @author Pavel Flaska
 */
public abstract class FieldImpl extends FeatureImpl implements Field {
    private static final ElementInfo DEFAULT_INFO = new FieldInfo(null, FieldInfo.FIELD_TYPE, null, 0, null, FieldInfo.SINGLE_FIELD_INDEX, null);

    protected String initialValueText;
    protected InitialValue initialValue;
    private boolean initValueInited = false;
    boolean elementsInited = false;
    private TypeReference typeName = null;
    private int dimCount = 0;

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

    public String toString() {
        return "field " + getName(); // NOI18N
    }

    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }

    public List getChildren() {
        List list = new ArrayList(3);
        addIfNotNull(list, getTypeName());
        list.addAll(super.getChildren());
        if (!isChanged(CHANGED_INITIAL_VALUE) || initialValueText == null) {
            addIfNotNull(list, getInitialValue());
        }
        return list;
    }

    public List getInitedChildren() {
        List list = new ArrayList();
        if (elementsInited) {
            addIfNotNull(list, typeName);
        }
        if (initValueInited && (!isChanged(CHANGED_INITIAL_VALUE) || initialValueText == null)) {
            addIfNotNull(list, initialValue);
        }
        return list;
    }

    protected void initASTElements() {
        elementsInited = false;
        if (!childrenInited) {
            initChildren();
        }
        FieldInfo info = (FieldInfo) getElementInfo();
        ASTree tree = info.getTypeAST(this);
        typeName = (TypeReference) initOrCreate(typeName, tree);
        elementsInited = true;
    }

    protected void initChildren() {
        childrenInited = false;
        super.initChildren();
        childrenInited = true;
        if (elementsInited) {
            initASTElements();
        }
        if (initValueInited) {
            JMManager.getTransactionMutex().addBFeatureToInitQueue(this);
        }
    }
    
    protected void resetChildren() {
        if (childrenInited) {
            resetASTElements();
            super.resetChildren();
        }
    }
    
    protected void setData(List annotations, int modifiers, String javadocText, JavaDoc javadoc, boolean isFinal, TypeReference typeName, int dimCount, InitialValue initialValue, String initialValueText) {
        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, typeName);
        this.typeName = typeName;
        initValueInited = true;
        elementsInited = true;
    }
    
    public void initInitValue() {
        initValueInited = false;
        if (!childrenInited) {
            initChildren();
        }
        FieldInfo initValInfo = (FieldInfo) getElementInfo();
        if (initValInfo != null) {
            initValInfo.doAttribution(this);
            initialValue = (InitialValue) initOrCreate(initialValue, extractInitialValue());
        }
        initValueInited = true;
    }
    
    protected void matchPersistent(ElementInfo info) {
        super.matchPersistent(info);
        boolean inFieldGroup = refImmediateComposite() instanceof FieldGroup;
        FieldInfo newInfo = (FieldInfo) info;
        
        if (newInfo.modifiers != getSourceModifiers()) {
            if (inFieldGroup) {
                super_setModifiers(newInfo.modifiers);
            } else {
                setModifiers(newInfo.modifiers);
            }
        }
        
        if (!isPersisted()) {
            setPersisted(true);
            persist();
            setTypeRef(newInfo.type);
        } else {
            if (!Utilities.compareObjects(newInfo.type, getTypeRef())) {
                if (refImmediateComposite() instanceof FieldGroup) {
                    setTypeRef(newInfo.type);
                } else {
                    setType(resolveType(newInfo.type));
                }
            }
        }
    }
    
    protected void matchModifiers(ElementInfo info) {
    }

    /** The method has to make sure that the AST infos of children are also updated.
     */
    protected void matchElementInfo(ElementInfo newInfo) {
        super.matchElementInfo(newInfo);
        resetASTElements();
    }

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

            if (typeName != null) {
                TypeReference temp = typeName;
                typeName = null;
                changeChild(temp, null);
                temp.refDelete();
            }
            initValueInited = false;
            elementsInited = false;
        }
    }

    // Interfaces inherited from Variable, StructuralElement

    /**
     * Returns the value of reference type.
     * @return Value of reference type.
     */
    public Type getType() {
        MetadataElement parent = (MetadataElement) refImmediateComposite();
        if (parent instanceof FieldGroup && parent.isChanged()) {
            FieldGroup group = (FieldGroup) parent;
            Type t=group.getType();
            int dims=getDimCount();
            
            if (dims>0) {
                int i;
                JavaModelPackage extent = (JavaModelPackage) refImmediatePackage();
                ArrayClass arrClass=extent.getArray();
                
                for(i=0;i
... 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.