alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.java;

import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import javax.swing.text.StyledDocument;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.openide.cookies.SourceCookie;
import org.openide.loaders.DataObject;
import org.openide.text.Line;
import org.openide.text.NbDocument;
import org.openide.util.NbBundle;

class OverrideAnnotation extends LineSetAnnotation {


    private static final String OVERRIDE_ANNOTATION_TYPE = "org-netbeans-modules-java-override_annotation"; // NOI18N
    private static final String IMPLEMENT_ANNOTATION_TYPE = "org-netbeans-modules-java-implement_annotation"; // NOI18N

    private Descriptor descriptor;


    private OverrideAnnotation (Descriptor descriptor) {
        this.descriptor = descriptor;
    }


    public String getAnnotationType() {
        return this.descriptor.isOverriding()? OVERRIDE_ANNOTATION_TYPE : IMPLEMENT_ANNOTATION_TYPE;
    }

    public String getShortDescription() {
        String bundleKey = this.descriptor.isOverriding() ? "TXT_Overrides" : "TXT_Implements";    //NOI18N
        return MessageFormat.format(NbBundle.getMessage(OverrideAnnotation.class,bundleKey),
                new Object[] {this.descriptor.getMethodName (), this.descriptor.getOriginalClassQName ()});
    }

    public void attachToLineSet(Line.Set lines) {
        int line = this.descriptor.getLine();
        if (line < 0) return;
        Line docline = lines.getCurrent(line);
        Line.Part part=docline.createPart(0,0);
        attach(part);
    }


    public boolean equals (Object other) {
        if (other instanceof OverrideAnnotation) {
            OverrideAnnotation otherAnnotation = (OverrideAnnotation) other;
            return this.descriptor.equals (otherAnnotation.descriptor);
        }
        return false;
    }


    Descriptor getDescriptor () {
        return this.descriptor;
    }


    public static OverrideAnnotation forDescriptor (Descriptor desc) {
        return new OverrideAnnotation (desc);
    }


    public static class Descriptor {

        private final String overridenMethod;
        private final String originalMethod;
        private final boolean isImplementing;

        public Descriptor (Method originalMethod, Method overridenMethod) {
            this.originalMethod = originalMethod.refMofId();
            this.overridenMethod = overridenMethod.refMofId();
            JavaClass declaring = (JavaClass) originalMethod.getDeclaringClass();
            isImplementing = declaring != null && (declaring.isInterface() ||
                    Modifier.isAbstract (originalMethod.getModifiers()));
        }

        public final Method getOriginalMethod () {
            return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.originalMethod);
        }

        public final Method getOverridenMethod () {
            return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.overridenMethod);
        }

        public final boolean isOverriding () {
            return ! this.isImplementing();
        }

        public final boolean isImplementing () {
            return isImplementing;
        }

        public final String getMethodName () {
            return getOverridenMethod().getName();
        }

        public final String getOriginalClassQName () {
            return getOriginalMethod().getDeclaringClass().getName();
        }

        public final boolean equals (Object other) {
            if (other instanceof OverrideAnnotation.Descriptor) {
                OverrideAnnotation.Descriptor oa = (OverrideAnnotation.Descriptor) other;
                return (this.originalMethod == null ? oa.originalMethod == null : this.originalMethod.equals (oa.originalMethod)) &&
                       (this.overridenMethod == null ? oa.overridenMethod == null : this.overridenMethod.equals (oa.overridenMethod));
            }
            return false;
        }

        public final int hashCode () {
            return this.overridenMethod == null ? 0 : this.originalMethod.hashCode();
        }

        /** returns -1 if the document is not accessible */
        final int getLine () {
            Method overridenMethod = getOverridenMethod();
            DataObject dobj = JavaMetamodel.getManager().getDataObject(overridenMethod.getResource());
            if (dobj == null) return -1;
            SourceCookie.Editor seditor = (SourceCookie.Editor) dobj.getCookie (SourceCookie.Editor.class);
            if (seditor == null) return -1;
            StyledDocument doc = seditor.getDocument();
            if (doc == null) return -1;

            int offset = JavaMetamodel.getManager().getElementPosition(overridenMethod).getBegin().getOffset();
            return NbDocument.findLineNumber(doc,offset);
        }

    }


}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.