1 19 package org.netbeans.modules.java; 20 21 import java.lang.reflect.Modifier ; 22 import java.text.MessageFormat ; 23 import javax.swing.text.StyledDocument ; 24 import org.netbeans.jmi.javamodel.JavaClass; 25 import org.netbeans.jmi.javamodel.Method; 26 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 27 import org.openide.cookies.SourceCookie; 28 import org.openide.loaders.DataObject; 29 import org.openide.text.Line; 30 import org.openide.text.NbDocument; 31 import org.openide.util.NbBundle; 32 33 class OverrideAnnotation extends LineSetAnnotation { 34 35 36 private static final String OVERRIDE_ANNOTATION_TYPE = "org-netbeans-modules-java-override_annotation"; private static final String IMPLEMENT_ANNOTATION_TYPE = "org-netbeans-modules-java-implement_annotation"; 39 private Descriptor descriptor; 40 private int attachedToLine = -1; 41 42 private OverrideAnnotation (Descriptor descriptor) { 43 this.descriptor = descriptor; 44 } 45 46 47 public String getAnnotationType() { 48 return this.descriptor.isOverriding()? OVERRIDE_ANNOTATION_TYPE : IMPLEMENT_ANNOTATION_TYPE; 49 } 50 51 public String getShortDescription() { 52 String bundleKey = this.descriptor.isOverriding() ? "TXT_Overrides" : "TXT_Implements"; return MessageFormat.format(NbBundle.getMessage(OverrideAnnotation.class,bundleKey), 54 new Object [] {this.descriptor.getMethodName (), this.descriptor.getOriginalClassQName ()}); 55 } 56 57 public void attachToLineSet(Line.Set lines) { 58 attachToLineSet(lines, this.descriptor.getLine()); 59 } 60 61 private void attachToLineSet(Line.Set lines, int line) { 62 if (line < 0) return; 63 Line docline = lines.getCurrent(line); 64 Line.Part part=docline.createPart(0,0); 65 attach(part); 66 attachedToLine = line; 67 } 68 69 public void updateLine(Line.Set lines) { 70 int line = this.descriptor.getLine(); 71 if (line != attachedToLine) { 72 detach(); 73 attachToLineSet(lines, line); 74 } 75 } 76 77 public boolean equals (Object other) { 78 if (other instanceof OverrideAnnotation) { 79 OverrideAnnotation otherAnnotation = (OverrideAnnotation) other; 80 return this.descriptor.equals (otherAnnotation.descriptor); 81 } 82 return false; 83 } 84 85 Descriptor getDescriptor () { 86 return this.descriptor; 87 } 88 89 public static OverrideAnnotation forDescriptor (Descriptor desc) { 90 return new OverrideAnnotation (desc); 91 } 92 93 public static class Descriptor { 94 95 private final String overridenMethod; 96 private final String originalMethod; 97 private final boolean isImplementing; 98 99 public Descriptor (Method originalMethod, Method overridenMethod) { 100 this.originalMethod = originalMethod.refMofId(); 101 this.overridenMethod = overridenMethod.refMofId(); 102 JavaClass declaring = (JavaClass) originalMethod.getDeclaringClass(); 103 isImplementing = declaring != null && (declaring.isInterface() || 104 Modifier.isAbstract (originalMethod.getModifiers())); 105 } 106 107 public final Method getOriginalMethod () { 108 return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.originalMethod); 109 } 110 111 public final Method getOverridenMethod () { 112 return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.overridenMethod); 113 } 114 115 public final boolean isOverriding () { 116 return ! this.isImplementing(); 117 } 118 119 public final boolean isImplementing () { 120 return isImplementing; 121 } 122 123 public final String getMethodName () { 124 try { 125 return getOverridenMethod().getName(); 126 } catch (NullPointerException e) { 127 return ""; 128 } 129 } 130 131 public final String getOriginalClassQName () { 132 try { 133 return getOriginalMethod().getDeclaringClass().getName(); 134 } catch (NullPointerException e) { 135 return ""; 136 } 137 } 138 139 public final boolean equals (Object other) { 140 if (other instanceof OverrideAnnotation.Descriptor) { 141 OverrideAnnotation.Descriptor oa = (OverrideAnnotation.Descriptor) other; 142 return (this.originalMethod == null ? oa.originalMethod == null : this.originalMethod.equals (oa.originalMethod)) && 143 (this.overridenMethod == null ? oa.overridenMethod == null : this.overridenMethod.equals (oa.overridenMethod)); 144 } 145 return false; 146 } 147 148 public final int hashCode () { 149 return this.overridenMethod == null ? 0 : this.originalMethod.hashCode(); 150 } 151 152 153 final int getLine () { 154 Method overridenMethod = getOverridenMethod(); 155 if (overridenMethod == null) return -1; 156 DataObject dobj = JavaMetamodel.getManager().getDataObject(overridenMethod.getResource()); 157 if (dobj == null) return -1; 158 SourceCookie.Editor seditor = (SourceCookie.Editor) dobj.getCookie (SourceCookie.Editor.class); 159 if (seditor == null) return -1; 160 StyledDocument doc = seditor.getDocument(); 161 if (doc == null) return -1; 162 163 int offset = JavaMetamodel.getManager().getElementPosition(overridenMethod).getBegin().getOffset(); 164 return NbDocument.findLineNumber(doc,offset); 165 } 166 167 } 168 } 169 | Popular Tags |