1 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 ; 29 import org.netbeans.lib.java.parser.ASTree; 30 import org.netbeans.modules.javacore.parser.ASTProvider; 31 32 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 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 } 51 52 public List getChildren() { 53 List list = super.getChildren(); 54 list.addAll(0, getAnnotations()); 55 return list; 56 } 57 58 public String getName() { 59 return null; 60 } 61 62 public void setName(String name) { 63 throw new UnsupportedOperationException ("Cannot set name of a constructor."); } 66 67 71 public org.netbeans.jmi.javamodel.Type getType() { 72 return (JavaClass) getDeclaringClass(); 73 } 74 75 80 public void setType(org.netbeans.jmi.javamodel.Type newValue) { 81 RefObject nameAttr = null; 82 try { 83 nameAttr = ((MofClass) refMetaObject()).lookupElementExtended("type"); } catch (NameNotFoundException e) { 85 } 87 throw new ConstraintViolationException(this, nameAttr, "Type is readonly."); } 89 90 94 100 void generateTypeAndName(StringBuffer buf) { 101 ClassDefinition def = getDeclaringClass(); 102 if (def instanceof JavaClass) 103 buf.append(((JavaClass) def).getSimpleName()); 104 else 105 throw new UnsupportedOperationException ("It is denied to add " + "constructor to\nthe anonymous class!"); formatElementPart(CALLABLE_IDENTIFIER, buf); 108 } 109 110 String getRawText() { 111 if (!elementsInited) { 112 initASTElements(); 113 } 114 StringBuffer buf = new StringBuffer (); 115 generateHeader(buf); 116 generateBody(buf); 117 return buf.toString(); 118 } 119 120 public void getDiff(List diffList) { 121 MethodInfo astInfo = (MethodInfo) getElementInfo(); 122 ASTProvider parser = getParser(); 123 ASTree tree = getASTree(); 124 ASTree[] children = tree.getSubTrees(); 125 126 replaceJavaDoc(diffList); 128 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(), " "); } 135 getTypeParamsDiff(diffList); 136 ASTree declarator = tree.getSubTrees()[3]; 138 if (isChanged(CHANGED_NAME)) { 139 String newName = ((JavaClass) getDeclaringClass()).getSimpleName(); 140 replaceNode(diffList, parser, declarator.getSubTrees()[0], newName, 0, null); 141 } 142 String comma = formatElementPart(COMMA); 144 int endOffset = parser.getToken(declarator.getLastToken()).getStartOffset(); 145 getCollectionDiff(diffList, parser, CHANGED_PARAMETERS, 146 astInfo.parameters, getParameters(), endOffset, comma); 147 int startOffset = parser.getToken(declarator.getLastToken()).getEndOffset(); 149 getCollectionDiff(diffList, parser, CHANGED_THROWS, tree.getSubTrees()[4], 150 getExceptionNames(), startOffset, comma, formatElementPart(THROWS_KEYWORD)); 151 createBodyDiffs(diffList); 153 } 154 155 public Element duplicate(JavaModelPackage targetExtent) { 156 StatementBlock body; 157 String 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 |