KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.jmi.javamodel.Initializer;
22 import org.netbeans.jmi.javamodel.StatementBlock;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.mdr.storagemodel.StorableObject;
25 import org.netbeans.modules.javacore.parser.ASTProvider;
26 import org.netbeans.modules.javacore.parser.ElementInfo;
27 import org.netbeans.modules.javacore.parser.FeatureInfo;
28 import java.lang.reflect.Modifier JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import org.netbeans.jmi.javamodel.Element;
33 import org.netbeans.jmi.javamodel.JavaDoc;
34 import org.netbeans.jmi.javamodel.JavaModelPackage;
35 import org.netbeans.modules.javacore.JMManager;
36
37 /**
38  *
39  * @author Martin Matula
40  * @author Pavel Flaska
41  */

42 public abstract class InitializerImpl extends BehavioralFeatureImpl implements Initializer {
43     private static final ElementInfo DEFAULT_INFO = new FeatureInfo(null, FeatureInfo.STATIC_INITIALIZER_TYPE, null, 0, null);
44     
45     /** Creates a new instance of InitializerImpl */
46     public InitializerImpl(StorableObject s) {
47         super(s);
48     }
49
50     protected ElementInfo getDefaultInfo() {
51         return DEFAULT_INFO;
52     }
53
54     protected void matchName(ElementInfo info) {
55         // do not match name
56
}
57
58     public String JavaDoc getName() {
59         return null;
60     }
61
62     public void setName(String JavaDoc name) {
63         // initializer does not have a name
64
throw new UnsupportedOperationException JavaDoc();
65     }
66
67     protected ASTree getBodyAST() {
68         return getASTree().getSubTrees()[1];
69     }
70
71     public Collection JavaDoc getReferences() {
72         return Collections.EMPTY_LIST;
73     }
74
75     protected void initChildren() {
76         childrenInited = true;
77         StatementBlock body = retrieveBody();
78         if (bodyInited) {
79             JMManager.getTransactionMutex().addBFeatureToInitQueue(this);
80         }
81     }
82
83     public String JavaDoc getSourceText() {
84         String JavaDoc origElem;
85         if ((origElem = checkChange()) != null)
86             return origElem;
87         // we have element which is new or changed and moved.
88
// todo (#pf): it is possible to leave formatting and comments section
89
// as they are in the original element if the element is regenerated.
90
// Because it is only rare case, we do not solve it now. }
91
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         String JavaDoc indent = getIndentation();
93         buf.append('\n');
94         buf.append(indent);
95         int modifiers = getSourceModifiers();
96         if ((modifiers & ~Modifier.STATIC) != modifiers) {
97             buf.append(Modifier.toString(modifiers));
98         }
99         generateBody(buf);
100         return buf.toString();
101     }
102
103     public void getDiff(List JavaDoc diffList) {
104         ASTProvider parser = getParser();
105         ASTree tree = getASTree();
106
107         // modifier print
108
if (isChanged(CHANGED_MODIFIERS)) {
109             boolean isStatic = (getSourceModifiers() & Modifier.STATIC) == Modifier.STATIC ? true : false;
110             int startOffset = parser.getToken(tree.getFirstToken()).getStartOffset();
111             if (isStatic) {
112                 // add static modifier
113
String JavaDoc mods = Modifier.toString(getSourceModifiers());
114                 mods += ' ';
115                 diffList.add(new DiffElement(startOffset, startOffset, mods));
116             }
117             else {
118                 // delete static modifier
119
int endOffset = parser.getToken(tree.getFirstToken()+1).getStartOffset();
120                 diffList.add(new DiffElement(startOffset, endOffset, ""));
121             }
122         }
123         // body print
124
createBodyDiffs(diffList);
125     }
126     
127     public Element duplicate(JavaModelPackage targetExtent) {
128         StatementBlock body;
129         String JavaDoc bodyText;
130         
131         if (isChanged(CHANGED_BODY) && this.bodyText != null) {
132             body = null;
133             bodyText = this.bodyText;
134         } else {
135             body = (StatementBlock) duplicateElement(getBody(), targetExtent);
136             bodyText = null;
137         }
138         
139         return targetExtent.getInitializer().createInitializer(
140                 null,
141                 duplicateList(getAnnotations(), targetExtent),
142                 getModifiers(),
143                 null,
144                 (JavaDoc) duplicateElement(getJavadoc(), targetExtent),
145                 body,
146                 bodyText
147             );
148     }
149 }
150
Popular Tags