KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.netbeans.jmi.javamodel.*;
21 import org.netbeans.mdr.storagemodel.StorableObject;
22 import org.netbeans.modules.javacore.parser.ElementInfo;
23 import org.netbeans.modules.javacore.parser.MethodInfo;
24 import javax.jmi.model.MofClass;
25 import javax.jmi.model.NameNotFoundException;
26 import javax.jmi.reflect.ConstraintViolationException;
27 import javax.jmi.reflect.RefObject;
28 import java.util.List JavaDoc;
29 import org.netbeans.lib.java.parser.ASTree;
30 import org.netbeans.modules.javacore.parser.ASTProvider;
31
32 /**
33  *
34  * @author Martin Matula
35  */

36 public abstract class ConstructorImpl extends CallableFeatureImpl implements Constructor {
37     private static final ElementInfo DEFAULT_INFO = new MethodInfo(null, MethodInfo.CONSTRUCTOR_TYPE, null, 0, null, null, null, null, null);
38
39     /** Creates a new instance of MethodImpl */
40     public ConstructorImpl(StorableObject s) {
41         super(s);
42     }
43
44     protected ElementInfo getDefaultInfo() {
45         return DEFAULT_INFO;
46     }
47
48     protected void matchName(ElementInfo info) {
49         // do not match name
50
}
51
52     public List JavaDoc getChildren() {
53         List JavaDoc list = super.getChildren();
54         list.addAll(0, getAnnotations());
55         return list;
56     }
57
58     public String JavaDoc getName() {
59         return null;
60     }
61
62     public void setName(String JavaDoc name) {
63         // cannot set name of the constructor
64
throw new UnsupportedOperationException JavaDoc("Cannot set name of a constructor."); // NOI18N
65
}
66
67     /**
68      * Returns the value of reference type.
69      * @return Value of reference type.
70      */

71     public org.netbeans.jmi.javamodel.Type getType() {
72         return (JavaClass) getDeclaringClass();
73     }
74
75     /**
76      * Sets the value of reference type. See {@link #getType} for description
77      * on the reference.
78      * @param newValue New value to be set.
79      */

80     public void setType(org.netbeans.jmi.javamodel.Type newValue) {
81         RefObject nameAttr = null;
82         try {
83             nameAttr = ((MofClass) refMetaObject()).lookupElementExtended("type"); // NOI18N
84
} catch (NameNotFoundException e) {
85             // ignore
86
}
87         throw new ConstraintViolationException(this, nameAttr, "Type is readonly."); // NOI18N
88
}
89
90     // .........................................................................
91
// printing and formatting fuctionality
92
// .........................................................................
93

94     /**
95      * Implementation of abstract method defined in CallableFeatureImpl.
96      * Appends to buffer parents name.
97      *
98      * @param buf buffer to append to
99      */

100     void generateTypeAndName(StringBuffer JavaDoc buf) {
101         ClassDefinition def = getDeclaringClass();
102         if (def instanceof JavaClass)
103             buf.append(((JavaClass) def).getSimpleName());
104         else
105             throw new UnsupportedOperationException JavaDoc("It is denied to add " + // NOI18N
106
"constructor to\nthe anonymous class!"); // NOI18N
107
formatElementPart(CALLABLE_IDENTIFIER, buf);
108     }
109
110     String JavaDoc getRawText() {
111         if (!elementsInited) {
112             initASTElements();
113         }
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         generateHeader(buf);
116         generateBody(buf);
117         return buf.toString();
118     }
119
120     public void getDiff(List JavaDoc diffList) {
121         MethodInfo astInfo = (MethodInfo) getElementInfo();
122         ASTProvider parser = getParser();
123         ASTree tree = getASTree();
124         ASTree[] children = tree.getSubTrees();
125
126         // javadoc print
127
replaceJavaDoc(diffList);
128         // modifier print
129
ASTree type = children[2];
130         if (isChanged(CHANGED_MODIFIERS) || isChanged(CHANGED_ANNOTATION)) {
131             diffModifiers(diffList, type == null ? children[3] : type, parser);
132         } else if (children[0] != null) {
133             getCollectionDiff(diffList, parser, CHANGED_ANNOTATION, astInfo.annotations, getAnnotations(), parser.getToken(children[0].getLastToken()).getEndOffset(), " "); // NOI18N
134
}
135         getTypeParamsDiff(diffList);
136         // name
137
ASTree declarator = tree.getSubTrees()[3];
138         if (isChanged(CHANGED_NAME)) {
139             String JavaDoc newName = ((JavaClass) getDeclaringClass()).getSimpleName();
140             replaceNode(diffList, parser, declarator.getSubTrees()[0], newName, 0, null);
141         }
142         // parameters
143
String JavaDoc comma = formatElementPart(COMMA);
144         int endOffset = parser.getToken(declarator.getLastToken()).getStartOffset();
145         getCollectionDiff(diffList, parser, CHANGED_PARAMETERS,
146             astInfo.parameters, getParameters(), endOffset, comma);
147         // exceptions
148
int startOffset = parser.getToken(declarator.getLastToken()).getEndOffset();
149         getCollectionDiff(diffList, parser, CHANGED_THROWS, tree.getSubTrees()[4],
150                 getExceptionNames(), startOffset, comma, formatElementPart(THROWS_KEYWORD));
151         // body print
152
createBodyDiffs(diffList);
153     }
154     
155     public Element duplicate(JavaModelPackage targetExtent) {
156         StatementBlock body;
157         String JavaDoc bodyText;
158         
159         if (isChanged(CHANGED_BODY) && this.bodyText != null) {
160             body = null;
161             bodyText = this.bodyText;
162         } else {
163             body = (StatementBlock) duplicateElement(getBody(), targetExtent);
164             bodyText = null;
165         }
166         
167         return targetExtent.getConstructor().createConstructor(
168                 null,
169                 duplicateList(getAnnotations(), targetExtent),
170                 getModifiers(),
171                 null,
172                 (JavaDoc) duplicateElement(getJavadoc(), targetExtent),
173                 body,
174                 bodyText,
175                 duplicateList(getTypeParameters(), targetExtent),
176                 duplicateList(getParameters(), targetExtent),
177                 duplicateList(getExceptionNames(), targetExtent)
178             );
179     }
180 }
181
Popular Tags