org.eclipse.jdt.internal.core.PackageDeclaration.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.jdt.internal.core.PackageDeclaration.java

Source

/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core;

import org.eclipse.jdt.core.*;

/**
 * @see IPackageDeclaration
 */

public class PackageDeclaration extends SourceRefElement implements IPackageDeclaration {

    String name;

    protected PackageDeclaration(CompilationUnit parent, String name) {
        super(parent);
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof PackageDeclaration))
            return false;
        return super.equals(o);
    }

    @Override
    public String getElementName() {
        return this.name;
    }

    /**
     * @see IJavaElement
     */
    @Override
    public int getElementType() {
        return PACKAGE_DECLARATION;
    }

    /**
     * @see JavaElement#getHandleMemento()
     */
    @Override
    protected char getHandleMementoDelimiter() {
        return JavaElement.JEM_PACKAGEDECLARATION;
    }

    /**
     * @see IPackageDeclaration#getNameRange()
     */
    @Override
    public ISourceRange getNameRange() throws JavaModelException {
        AnnotatableInfo info = (AnnotatableInfo) getElementInfo();
        return info.getNameRange();
    }

    @Override
    public IJavaElement getPrimaryElement(boolean checkOwner) {
        CompilationUnit cu = (CompilationUnit) getAncestor(COMPILATION_UNIT);
        if (checkOwner && cu.isPrimary())
            return this;
        return cu.getPackageDeclaration(this.name);
    }

    /**
     * @private Debugging purposes
     */
    @Override
    protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) {
        buffer.append(tabString(tab));
        buffer.append("package "); //$NON-NLS-1$
        toStringName(buffer);
        if (info == null) {
            buffer.append(" (not open)"); //$NON-NLS-1$
        }
    }
}