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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.jmiimpl.javamodel;

import org.netbeans.jmi.javamodel.Initializer;
import org.netbeans.jmi.javamodel.StatementBlock;
import org.netbeans.lib.java.parser.ASTree;
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.FeatureInfo;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.netbeans.modules.javacore.JMManager;

/**
 *
 * @author  Martin Matula
 * @author  Pavel Flaska
 */
public abstract class InitializerImpl extends BehavioralFeatureImpl implements Initializer {
    private static final ElementInfo DEFAULT_INFO = new FeatureInfo(null, FeatureInfo.STATIC_INITIALIZER_TYPE, null, 0, null);
    
    /** Creates a new instance of InitializerImpl */
    public InitializerImpl(StorableObject s) {
        super(s);
    }

    protected ElementInfo getDefaultInfo() {
        return DEFAULT_INFO;
    }

    protected void matchName(ElementInfo info) {
        // do not match name
    }

    public String getName() {
        return null;
    }

    public void setName(String name) {
        // initializer does not have a name
        throw new UnsupportedOperationException();
    }

    protected ASTree getBodyAST() {
        return getASTree().getSubTrees()[1];
    }

    public Collection getReferences() {
        return Collections.EMPTY_LIST;
    }

    protected void initChildren() {
        childrenInited = true;
        StatementBlock body = retrieveBody();
        if (bodyInited) {
            JMManager.getTransactionMutex().addBFeatureToInitQueue(this);
        }
    }

    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        // we have element which is new or changed and moved.
        // todo (#pf): it is possible to leave formatting and comments section
        // as they are in the original element if the element is regenerated.
        // Because it is only rare case, we do not solve it now.    }
        StringBuffer buf = new StringBuffer();
        String indent = getIndentation();
        buf.append('\n');
        buf.append(indent);
        int modifiers = getSourceModifiers();
        if ((modifiers & ~Modifier.STATIC) != modifiers) {
            buf.append(Modifier.toString(modifiers));
        }
        generateBody(buf);
        return buf.toString();
    }

    public void getDiff(List diffList) {
        ASTProvider parser = getParser();
        ASTree tree = getASTree();

        // modifier print
        if (isChanged(CHANGED_MODIFIERS)) {
            boolean isStatic = (getSourceModifiers() & Modifier.STATIC) == Modifier.STATIC ? true : false;
            int startOffset = parser.getToken(tree.getFirstToken()).getStartOffset();
            if (isStatic) {
                // add static modifier
                String mods = Modifier.toString(getSourceModifiers());
                mods += ' ';
                diffList.add(new DiffElement(startOffset, startOffset, mods));
            }
            else {
                // delete static modifier
                int endOffset = parser.getToken(tree.getFirstToken()+1).getStartOffset();
                diffList.add(new DiffElement(startOffset, endOffset, ""));
            }
        }
        // body print
        createBodyDiffs(diffList);
    }
}
... 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.