org.eclipse.jdt.internal.core.CreatePackageDeclarationOperation Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2000, 2008 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.core.runtime.IStatus;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelStatus;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.core.util.Messages;
/**
* This operation adds/replaces a package declaration in an existing compilation unit.
* If the compilation unit already includes the specified package declaration,
* it is not generated (it does not generate duplicates).
*
*
Required Attributes:
* - Compilation unit element
*
- Package name
*
*/
public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
/**
* The name of the package declaration being created
*/
protected String name = null;
/**
* When executed, this operation will add a package declaration to the given compilation unit.
*/
public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) {
super(parentElement);
this.name= name;
}
@Override
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
return CompilationUnit.PACKAGE_PROPERTY;
}
@Override
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
//look for an existing package declaration
IJavaElement[] children = getCompilationUnit().getChildren();
for (int i = 0; i < children.length; i++) {
if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION && this.name.equals(children[i].getElementName())) {
//equivalent package declaration already exists
this.creationOccurred = false;
return null;
}
}
AST ast = this.cuAST.getAST();
PackageDeclaration pkgDeclaration = ast.newPackageDeclaration();
Name astName = ast.newName(this.name);
pkgDeclaration.setName(astName);
return pkgDeclaration;
}
/**
* Creates and returns the handle for the element this operation created.
*/
@Override
protected IJavaElement generateResultHandle() {
return getCompilationUnit().getPackageDeclaration(this.name);
}
/**
* @see CreateElementInCUOperation#getMainTaskName()
*/
@Override
public String getMainTaskName(){
return Messages.operation_createPackageProgress;
}
/**
* Sets the correct position for new package declaration:
* - before the first import
*
- if no imports, before the first type
*
- if no type - first thing in the CU
*
-
*/
@Override
protected void initializeDefaultPosition() {
try {
ICompilationUnit cu = getCompilationUnit();
IImportDeclaration[] imports = cu.getImports();
if (imports.length > 0) {
createBefore(imports[0]);
return;
}
IType[] types = cu.getTypes();
if (types.length > 0) {
createBefore(types[0]);
return;
}
} catch (JavaModelException e) {
// cu doesn't exist: ignore
}
}
/**
* Possible failures:
* - NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the operation
*
- INVALID_NAME - a name supplied to the operation was not a valid
* package declaration name.
*
* @see IJavaModelStatus
* @see JavaConventions
*/
@Override
public IJavaModelStatus verify() {
IJavaModelStatus status = super.verify();
if (!status.isOK()) {
return status;
}
IJavaProject project = getParentElement().getJavaProject();
if (JavaConventions.validatePackageName(this.name, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR) {
return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, this.name);
}
return JavaModelStatus.VERIFIED_OK;
}
}