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 org.netbeans.jmi.javamodel.*;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.mdr.handlers.AttrListWrapper;
import org.netbeans.mdr.persistence.StorageException;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.*;
import org.openide.util.Utilities;
import org.openide.ErrorManager;
import javax.jmi.reflect.RefObject;
import java.util.*;
import org.openide.text.PositionBounds;

/**
 *
 * @author  Pavel Flaska
 * @author  Martin Matula
 */
public abstract class FieldGroupImpl extends FeatureImpl implements FieldGroup {
    public static final String FIELDS_ATTR = "fields";
    private static final ElementInfo DEFAULT_INFO = new FieldGroupInfo(null, FieldGroupInfo.FIELDGROUP_TYPE, 0, null, null, null);

    private TypeReference typeName;
    private LightAttrList fields;

    private boolean elementsInited = false;

    /** Creates a new instance of FieldGroupImpl */
    protected FieldGroupImpl(StorableObject s) {
        super(s);
    }

    public void setType(Type newValue) {
        TypeRef tr = typeToTypeRef(newValue);
        _setTypeName((TypeReference) typeRefToTypeReference(tr), tr);
    }

    public Type getType() {
        checkUpToDate();
        return resolveType(getTypeRef());
    }

    public TypeReference getTypeName() {
        if (!elementsInited) {
            initASTElements();
        }
        return typeName;
    }

    private void _setTypeName(TypeReference typeName, TypeRef typeRef) {
        objectChanged(CHANGED_TYPE);
        if (elementsInited) {
            changeChild(getTypeName(), typeName);
            this.typeName = typeName;
        }
        setTypeRef(typeRef);
    }

    public void setTypeName(TypeReference typeName) {
        _setTypeName(typeName, typeReferenceToTypeRef(typeName));
    }

    protected void resetChildren() {
        if (childrenInited) {
            resetASTElements();
            initChildren();
        }
    }

    public int getModifiers() {
        ClassDefinition cd = getDeclaringClass();
        int mods = super.getModifiers();
        if (cd instanceof JavaClass && ((JavaClass)cd).isInterface()) {            
            return mods | Modifier.STATIC | Modifier.FINAL;
        } else {
            return mods;
        }
    }
    
    protected void initASTElements() {
        elementsInited = false;
        if (!childrenInited) {
            initChildren();
        }
        ElementInfo info = getElementInfo();
        ASTree tree = info.getTypeAST(this);
        typeName = (TypeReference) initOrCreate(typeName, tree);
        elementsInited = true;
    }
    
    protected void matchPersistent(ElementInfo info) {
        super.matchPersistent(info);        
        FieldGroupInfo fgInfo = (FieldGroupInfo)info;
        
        if (!isPersisted()) {
            setPersisted(true);
            persist();
            setTypeRef(fgInfo.type);
            persistChildren(super_getFields(), fgInfo.fields);
        } else {
            if (!Utilities.compareObjects(fgInfo.type, getTypeRef())) {
                setTypeRef(fgInfo.type);
            }
            processMembers(getFields(), fgInfo.fields);
        }
    }
    
    protected void matchElementInfo(ElementInfo newInfo) {
        super.matchElementInfo(newInfo);
        resetASTElements();
    }
    
    public void replaceChild(Element oldElement, Element newElement) {
        if (isPersisted()) {
            if (replaceObject(getFields(), oldElement, newElement)) return;
        }
        if (elementsInited && oldElement.equals(typeName)) {
            setTypeName((MultipartId) newElement);
            return;
        }
        super.replaceChild(oldElement, newElement);
    }
    
    private void resetASTElements() {
        if (elementsInited) {
            if (typeName != null) {
                TypeReference temp = typeName;
                typeName = null;
                changeChild(temp, null);
                temp.refDelete();
            }
            elementsInited = false;
        }
    }

    protected List getInitedChildren() {
        List list = new ArrayList();
        if (childrenInited) {
            list.addAll(fields);
        }
        if (elementsInited) {
            addIfNotNull(list, typeName);
        }
        return list;
    }

    public List getFields() {
        if (fields == null) {
            checkUpToDate();
            fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), null, CHANGED_FEATURES);
        }
        return fields;
    }

    public List getChildren() {
        List result = new ArrayList(getFields().size() + 1);
        addIfNotNull(result, getTypeName());
        result.addAll(getFields());
        return result;
    }

    /**
     * Creates text representation of this object, e.g. 'public int a, b;' for
     * the group of two fields a and b with type int and modifier public.
     */
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;

        StringBuffer buf = new StringBuffer();
        buf.append('\n');
        generateNewJavaDoc(buf);
        buf.append(getIndentation());
        generateNewModifiers(buf);
        if (getTypeName() != null) {
            buf.append(((MetadataElement) getTypeName()).getSourceText());
        } else {
            buf.append(getType().getName());
        }
        buf.append(' '); // NOI18N

        for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) {
            FieldImpl f = (FieldImpl) fieldIt.next();
            buf.append(f.getSourceText());
            if (fieldIt.hasNext())
                formatElementPart(COMMA, buf);
            else
                break;
        }
        buf.append(';'); // NOI18N
        return buf.toString();
    }

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

        // javadoc print
        if (isChanged(CHANGED_JAVADOC)) {
            // todo (#pf): implement javaDoc change to diffList!
        }
        // modifier print
        if (isChanged(CHANGED_MODIFIERS)) {
            diffModifiers(diffList, children[TYPE], parser);
        }
        // type print
        getChildDiff(diffList, parser, children[TYPE], (MetadataElement) getTypeName(), CHANGED_TYPE);
        // name print

        getCollectionDiff(diffList, parser, CHANGED_FEATURES, ((FieldGroupInfo) getElementInfo()).fields,
                getFields(), parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(COMMA));
    }

    // useful constants
    private static final int TYPE = 1;

    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }

    private List getNakedFeatures() {
        try {
            return (List) ((StorableObject) _getDelegate()).getAttribute(FIELDS_ATTR); // NOI18N
        } catch (StorageException e) {
            throw (GeneralException) ErrorManager.getDefault().annotate(new GeneralException(e.getMessage()), e);
        }
    }

    protected abstract List super_getFields();

    public List getPersistentFeatures() {
        AttrListWrapper list = (AttrListWrapper) super_getFields();
        list.setAttrName(FIELDS_ATTR); // NOI18N
        return list;
    }

    protected void initChildren() {
        // initialization of fields requires writable lock
        boolean fail = true;
        _lock(true);
        try {
            childrenInited = false;
            fields = createChildrenList(fields, FIELDS_ATTR, (AttrListWrapper) super_getFields(), ((FieldGroupInfo) getElementInfo()).fields, CHANGED_FEATURES);
            childrenInited = true;

            if (elementsInited) {
                initASTElements();
            }
            fail = false;
        } finally {
            // I do not need to set featuresInitialized to false in
            // case fail == true, since rollback will follow, thus the
            // reset method that already does that will be invoked
            _unlock(fail);
        }
    }

    void setData(TypeReference typeName, List fields) {
        this.fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), fields, CHANGED_FEATURES);
        changeChild(null, typeName);
        this.typeName = typeName;
        elementsInited = true;
        childrenInited = true;
    }

    protected void _delete() {
        deleteChildren(FIELDS_ATTR, (AttrListWrapper) super_getFields());
        if (elementsInited) {
            deleteChild(getTypeName());
        }
        super._delete();
    }
}
... 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.