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 org.netbeans.jmi.javamodel.*;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ElementInfo;
import org.netbeans.modules.javacore.parser.MethodInfo;
import javax.jmi.model.MofClass;
import javax.jmi.model.NameNotFoundException;
import javax.jmi.reflect.ConstraintViolationException;
import javax.jmi.reflect.RefObject;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.modules.javacore.parser.ASTProvider;

/**
 *
 * @author  Martin Matula
 */
public abstract class ConstructorImpl extends CallableFeatureImpl implements Constructor {
    private static final ElementInfo DEFAULT_INFO = new MethodInfo(null, MethodInfo.CONSTRUCTOR_TYPE, null, 0, null, null, null, null, null);

    /** Creates a new instance of MethodImpl */
    public ConstructorImpl(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) {
        // cannot set name of the constructor
        throw new UnsupportedOperationException("Cannot set name of a constructor."); // NOI18N
    }

    /**
     * Returns the value of reference type.
     * @return Value of reference type.
     */
    public org.netbeans.jmi.javamodel.Type getType() {
        return (JavaClass) getDeclaringClass();
    }

    /**
     * Sets the value of reference type. See {@link #getType} for description
     * on the reference.
     * @param newValue New value to be set.
     */
    public void setType(org.netbeans.jmi.javamodel.Type newValue) {
        RefObject nameAttr = null;
        try {
            nameAttr = ((MofClass) refMetaObject()).lookupElementExtended("type"); // NOI18N
        } catch (NameNotFoundException e) {
            // ignore
        }
        throw new ConstraintViolationException(this, nameAttr, "Type is readonly."); // NOI18N
    }

    // .........................................................................
    // printing and formatting fuctionality
    // .........................................................................

    /**
     * Implementation of abstract method defined in CallableFeatureImpl.
     * Appends to buffer parents name.
     *
     * @param  buf  buffer to append to
     */
    void generateTypeAndName(StringBuffer buf) {
        ClassDefinition def = getDeclaringClass();
        if (def instanceof JavaClass)
            buf.append(((JavaClass) def).getSimpleName());
        else
            throw new UnsupportedOperationException("It is denied to add " + // NOI18N
                "constructor to\nthe anonymous class!"); // NOI18N
        formatElementPart(CALLABLE_IDENTIFIER, buf);
    }

    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        if (!elementsInited) {
            initASTElements();
        }
        // 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();
        generateHeader(buf);
        generateBody(buf);
        return buf.toString();
    }

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

        // javadoc print
        replaceJavaDoc(diffList);
        // modifier print
        ASTree type = children[2];
        if (isChanged(CHANGED_MODIFIERS)) {
            diffModifiers(diffList, type == null ? children[3] : type, parser);
        }
        getTypeParamsDiff(diffList);
        // name
        ASTree declarator = tree.getSubTrees()[3];
        if (isChanged(CHANGED_NAME)) {
            String newName = ((JavaClass) getDeclaringClass()).getSimpleName();
            replaceNode(diffList, parser, declarator.getSubTrees()[0], newName, 0, null);
        }
        // parameters
        String comma = formatElementPart(COMMA);
        int endOffset = parser.getToken(declarator.getLastToken()).getStartOffset();
        getCollectionDiff(diffList, parser, CHANGED_PARAMETERS,
            astInfo.parameters, getParameters(), endOffset, comma);
        // exceptions
        int startOffset = parser.getToken(declarator.getLastToken()).getEndOffset();
        getCollectionDiff(diffList, parser, CHANGED_THROWS, tree.getSubTrees()[4],
                getExceptionNames(), startOffset, comma, formatElementPart(THROWS_KEYWORD));
        // body print
        createBodyDiffs(diffList);
    }
    
    protected ASTree getPartEndTree(ElementPartKind part) {
        if (ElementPartKindEnum.HEADER.equals(part)) {
            ASTree[] headerParts = getASTree().getSubTrees();
            for (int i = 3; true; i--) {
                ASTree result = headerParts[i];
                if (result != null) {
                    return result;
                }
            }
        }
        return super.getPartEndTree(part);
    }


}
... 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.