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.Iterator;
import org.netbeans.modules.javacore.parser.AnnotationInfo;
import org.netbeans.modules.javacore.parser.ElementInfo;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.jmi.javamodel.Annotation;
import java.util.List;
import org.netbeans.jmi.javamodel.AttributeValue;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;

/**
 * Represents an annotation usage.
 *
 * @author  Pavel Flaska
 * @author  Tomas Hurka
 */
public abstract class AnnotationImpl extends SemiPersistentElement implements Annotation {

    LightAttrList attributeValues;
    protected String name;

    private static final ElementInfo DEFAULT_INFO = new AnnotationInfo(null, AnnotationInfo.ANNOTATION_TYPE, null, null);
    
    private LightAttrList attributeValuesNames = null;

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

    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }
    
    protected void matchPersistent(ElementInfo info) {
        // name is not persistent -> do not match it for annotation!
    }

    public void setName(String name) {
        if (name == null) 
            throw new NullPointerException();
        objectChanged(CHANGED_NAME);
        this.name = name;
    }
    
    public String getName() {
        if (name == null) {
            return getElementInfo().name;
        } else {
            return name;
        }
    }
    
    public List getAttributeValues() {
        if (!childrenInited) {
            initChildren();
        }
        return attributeValues;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        
        StringBuffer buf = new StringBuffer();
        buf.append('@').append(getName());
        List attributes = getAttributeValues();
        if (!attributes.isEmpty()) {
            buf.append('(');
            Iterator aIt = attributes.iterator();
            buf.append(((AttributeValueImpl) aIt.next()).getSourceText());
            while (aIt.hasNext()) {
                formatElementPart(COMMA, buf);
                buf.append(((AttributeValueImpl) aIt.next()).getSourceText());
            }
            buf.append(')');
        };
        return buf.toString();
    }
    
    public void getDiff(List diff) {
        throw new UnsupportedOperationException("getDiff()"); // NOI18N
    }
    ////////////////////////////////////////////////////////////////////////////
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            attributeValues = createChildrenList(attributeValues, "attributeValues", // NOI18N
                    tree.getSubTrees()[1], ASTreeTypes.ELEMENT_VALUE_PAIRS, CHANGED_ANNOTATION, false);
        }
        childrenInited = true;
        super.initChildren();
    }

    protected void resetChildren() {
        if (childrenInited) {
            if (attributeValues != null) {
                attributeValues = createChildrenList("attributeValues", // NOI18N
                                                     attributeValues,
                                                     CHANGED_ANNOTATION);
            }
        }
    }
    
    protected void setData(String name, List attributeValues) {
        this.name = name;
        this.attributeValues = createChildrenList("attributeValues", attributeValues, CHANGED_ANNOTATION); // NOI18N
    }
    
    protected void _delete() {
        if (childrenInited) {
            deleteChildren(getAttributeValues());
        }
        super._delete();
    }
    
    protected int getBeginIndex() {
        // for the annotation, we do not consider leading whitespaces as a
        // part of annotation. All the whitespaces and new lines are part of
        // element, which owns annotation.
        return getParser().getToken(getASTree().getFirstToken()).getStartOffset();
    }

    protected void hardRefParent(boolean enabled) {
        // do not hardref parent - this object is transient
    }

    protected void parentChanged() {
        // do nothing on parentChanged - this object is transient
    }
}
... 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.