KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ConstructorInvocationImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.*;
22 import javax.jmi.reflect.ConstraintViolationException;
23 import org.netbeans.jmi.javamodel.ConstructorInvocation;
24 import org.netbeans.jmi.javamodel.ConstructorInvocationClass;
25 import org.netbeans.jmi.javamodel.Element;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.jmi.javamodel.PrimaryExpression;
28 import org.netbeans.lib.java.parser.ASTree;
29 import org.netbeans.lib.java.parser.ASTreeTypes;
30 import org.netbeans.lib.java.parser.ParserTokens;
31 import org.netbeans.mdr.storagemodel.StorableObject;
32 import org.netbeans.modules.javacore.parser.ASTProvider;
33
34 /**
35  *
36  * @author Martin Matula
37  */

38 public abstract class ConstructorInvocationImpl extends InvocationImpl implements ConstructorInvocation {
39     private boolean hasSuper = false;
40     private PrimaryExpression parentClass;
41     
42     /** Creates a new instance of ConstructorInvocationImpl */
43     public ConstructorInvocationImpl(StorableObject o) {
44         super(o);
45     }
46     
47     public String JavaDoc getName() {
48         return null;
49     }
50     
51     public void setName(String JavaDoc name) {
52         throw new ConstraintViolationException(null, null, "Cannot change name of constructor invocation."); // NOI18N
53
}
54     
55     public void setParentClass(PrimaryExpression expression) {
56         objectChanged(CHANGED_PARENT_CLASS);
57         changeChild(getParentClass(), expression);
58         this.parentClass = expression;
59     }
60     
61     public PrimaryExpression getParentClass() {
62         if (!childrenInited) {
63             initChildren();
64         }
65         return parentClass;
66     }
67     
68     public boolean isHasSuper() {
69         if (isChanged(CHANGED_HAS_SUPER)) {
70             return hasSuper;
71         } else {
72             return getASTree().getSubTrees()[2].getType() != ParserTokens.THIS;
73         }
74     }
75     
76     public void setHasSuper(boolean hasSuper) {
77         objectChanged(CHANGED_HAS_SUPER);
78         this.hasSuper = hasSuper;
79     }
80     
81     public List getChildren() {
82         List list = new ArrayList(4);
83         addIfNotNull(list, getParentClass());
84         list.addAll(super.getChildren());
85         return list;
86     }
87     
88     protected void initChildren() {
89         childrenInited = false;
90         ASTree tree = getASTree();
91         if (tree != null) {
92             ASTree[] parts = tree.getSubTrees();
93             parentClass = (PrimaryExpression) initOrCreate(parentClass, parts[0]);
94             parameters = createChildrenList(parameters, "parameters", parts[3], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); // NOI18N
95
}
96         childrenInited = true;
97     }
98     
99     public String JavaDoc getSourceText() {
100         String JavaDoc origElem;
101         if ((origElem = checkChange()) != null)
102             return origElem;
103         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
104         boolean hasSuper = isHasSuper();
105         StatementImpl parentClass = (StatementImpl) getParentClass();
106         List params = getParameters();
107         buf.append('\n');
108         buf.append(getIndentation());
109         if (parentClass != null) {
110             buf.append(parentClass.getSourceText());
111             buf.append('.');
112         }
113         if (hasSuper) {
114             buf.append("super"); // NOI18N
115
} else {
116             buf.append("this"); // NOI18N
117
}
118         formatElementPart(PAR_OPEN_BRACKET, buf);
119         Iterator iter = params.iterator();
120         while (iter.hasNext()) {
121             StatementImpl par = (StatementImpl) iter.next();
122             buf.append(par.getSourceText());
123             if (iter.hasNext()) {
124                 formatElementPart(MetadataElement.COMMA, buf);
125             }
126         }
127         formatElementPart(PAR_CLOSE_BRACKET, buf);
128         buf.append(';');
129         return buf.toString();
130     }
131
132     public void getDiff(List diff) {
133         ASTProvider parser = getParser();
134         ASTree tree = getASTree();
135         ASTree[] children = tree.getSubTrees();
136
137         MetadataElement parentClass = (MetadataElement) getParentClass();
138         if ((isChanged(CHANGED_PARENT_CLASS) && children[0] != null && parentClass != null) || isChanged(CHANGED_CHILDREN)) {
139             getChildDiff(diff, parser, children[0], parentClass, CHANGED_PARENT_CLASS);
140         } else if (isChanged(CHANGED_PARENT_CLASS)) {
141             int endOffset = parser.getToken(children[2].getFirstToken()).getStartOffset();
142             if (children[0] == null) {
143                 diff.add(new DiffElement(endOffset, endOffset, parentClass.getSourceText().concat(".")));
144             } else {
145                 int startOffset = parser.getToken(children[0].getFirstToken()).getStartOffset();
146                 diff.add(new DiffElement(startOffset, endOffset, ""));
147             }
148         }
149
150         if (isChanged(CHANGED_HAS_SUPER)) {
151             replaceNode(diff, parser, children[2], isHasSuper() ? "super" : "this", 0, null);
152         }
153         
154         getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[3], ASTreeTypes.ARGUMENT_LIST, getParameters(), parser.getToken(tree.getLastToken() - 1).getStartOffset(), formatElementPart(MetadataElement.COMMA));
155     }
156     
157     void setData(boolean hasSuper, PrimaryExpression parentClass, List parameters) {
158         //if (name != null) throw new ConstraintViolationException(null, null, "Cannot set name of constructor invocation to a non-null value.");
159
setData(name, parameters);
160         changeChild(null, parentClass);
161         this.parentClass = parentClass;
162         this.hasSuper = hasSuper;
163     }
164
165     protected void _delete() {
166         // --- delete components -------------------------------------------
167
if (childrenInited) {
168             deleteChild(parentClass);
169         }
170         // --- delete links -----------------------------------------------
171
// no links to delete
172
// --- call super ---------------------------------------
173
super._delete();
174     }
175     
176     public void replaceChild(Element oldElement,Element newElement) {
177         if (childrenInited) {
178             if (oldElement.equals(parentClass)) {
179                 setParentClass((PrimaryExpression)newElement);
180             }
181         }
182     }
183     
184     public Element duplicate(JavaModelPackage targetExtent) {
185         return targetExtent.getConstructorInvocation().createConstructorInvocation(
186                 null,
187                 duplicateList(getParameters(), targetExtent),
188                 isHasSuper(),
189                 (PrimaryExpression) duplicateElement(getParentClass(), targetExtent)
190                );
191     }
192 }
193
Popular Tags