Java Code Examples for org.eclipse.jdt.core.JavaCore#removeJavaLikeExtension()

The following examples show how to use org.eclipse.jdt.core.JavaCore#removeJavaLikeExtension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
	if (isNewNameOk(destination, cu.getElementName())) {
		return null;
	}
	if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination)) {
		return null;
	}

	int resourceType= cu.getResource().getType();
	String newName= computeNewName(cu.getElementName(), resourceType);

	while (true) {
		if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
			fAutoGeneratedNewNames.add(newName);
			return JavaCore.removeJavaLikeExtension(newName);
		} else {
			newName= computeNewName(newName, resourceType);
		}
	}
}
 
Example 2
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
	if (isNewNameOk(destination, cu.getElementName()))
		return null;
	if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination))
		return null;
	int i= 1;
	while (true) {
		String newName;
		if (i == 1)
			newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOf1, cu.getElementName()); // Don't use BasicElementLabels! No RTL!
		else
			newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOfMore, new String[] { String.valueOf(i), cu.getElementName()}); // Don't use BasicElementLabels! No RTL!
		if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
			fAutoGeneratedNewNames.add(newName);
			return JavaCore.removeJavaLikeExtension(newName);
		}
		i++;
	}
}
 
Example 3
Source File: TypeProposalUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
static boolean isImplicitImport(String qualifier, ICompilationUnit cu) {
	if ("java.lang".equals(qualifier)) { //$NON-NLS-1$
		return true;
	}
	String packageName = cu.getParent().getElementName();
	if (qualifier.equals(packageName)) {
		return true;
	}
	String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
	String mainTypeName = concatenateName(packageName, typeName);
	return qualifier.equals(mainTypeName);
}
 
Example 4
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean willRenameCU() {
	String name = JavaCore.removeJavaLikeExtension(fType.getCompilationUnit().getElementName());
	if (!(Checks.isTopLevel(fType) && name.equals(fType.getElementName()))) {
		return false;
	}
	if (!checkNewPathValidity().isOK()) {
		return false;
	}
	if (!Checks.checkCompilationUnitNewName(fType.getCompilationUnit(), getNewElementName()).isOK()) {
		return false;
	}
	return true;
}
 
Example 5
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean willRenameCU() {
	String name = JavaCore.removeJavaLikeExtension(fType.getCompilationUnit().getElementName());
	if (! (Checks.isTopLevel(fType) && name.equals(fType.getElementName())))
		return false;
	if (! checkNewPathValidity().isOK())
		return false;
	if (! Checks.checkCompilationUnitNewName(fType.getCompilationUnit(), getNewElementName()).isOK())
		return false;
	return true;
}
 
Example 6
Source File: RenameCuWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected RenameInputWizardPage createInputPage(String message, String initialSetting) {
	return new RenameTypeWizardInputPage(message, IJavaHelpContextIds.RENAME_CU_WIZARD_PAGE, true, initialSetting) {
		@Override
		protected RefactoringStatus validateTextField(String text) {
			return validateNewName(text);
		}
		@Override
		protected String getNewName(INameUpdating nameUpdating) {
			String result= nameUpdating.getNewElementName();
			// If renaming a CU we have to remove the java file extension
			return JavaCore.removeJavaLikeExtension(result);
		}
	};
}
 
Example 7
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected String resolve(TemplateContext context) {
	ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
	if (unit == null)
		return null;
	return JavaCore.removeJavaLikeExtension(unit.getElementName());
}
 
Example 8
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isImplicitImport(String qualifier, ICompilationUnit cu) {
	if ("java.lang".equals(qualifier)) {  //$NON-NLS-1$
		return true;
	}
	String packageName= cu.getParent().getElementName();
	if (qualifier.equals(packageName)) {
		return true;
	}
	String typeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
	String mainTypeName= JavaModelUtil.concatenateName(packageName, typeName);
	return qualifier.equals(mainTypeName);
}
 
Example 9
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isImplicitImport(String qualifier) {
	if (JAVA_LANG.equals(qualifier)) {
		return true;
	}
	ICompilationUnit cu= this.compilationUnit;
	String packageName= cu.getParent().getElementName();
	if (qualifier.equals(packageName)) {
		return true;
	}
	String mainTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
	if (packageName.length() == 0) {
		return qualifier.equals(mainTypeName);
	}
	return qualifier.equals(packageName +'.' + mainTypeName);
}
 
Example 10
Source File: ImportRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Not API, package visibility as accessed from an anonymous type
 */
/* package */ final int findInImports(String qualifier, String name, int kind) {
	boolean allowAmbiguity=  (kind == ImportRewriteContext.KIND_STATIC_METHOD) || (name.length() == 1 && name.charAt(0) == '*');
	List imports= this.existingImports;
	char prefix= (kind == ImportRewriteContext.KIND_TYPE) ? NORMAL_PREFIX : STATIC_PREFIX;

	for (int i= imports.size() - 1; i >= 0 ; i--) {
		String curr= (String) imports.get(i);
		int res= compareImport(prefix, qualifier, name, curr);
		if (res != ImportRewriteContext.RES_NAME_UNKNOWN) {
			if (!allowAmbiguity || res == ImportRewriteContext.RES_NAME_FOUND) {
				if (prefix != STATIC_PREFIX) {
					return res;
				}
				Object currKind = this.importsKindMap.get(curr.substring(1));
				if (currKind != null && currKind.equals(this.importsKindMap.get(qualifier + '.' + name))) {
					return res;
				}
			}
		}
	}
	if (this.filterImplicitImports && this.useContextToFilterImplicitImports) {
		String fPackageName= this.compilationUnit.getParent().getElementName();
		String mainTypeSimpleName= JavaCore.removeJavaLikeExtension(this.compilationUnit.getElementName());
		String fMainTypeName= Util.concatenateName(fPackageName, mainTypeSimpleName, '.');
		if (kind == ImportRewriteContext.KIND_TYPE
				&& (qualifier.equals(fPackageName)
						|| fMainTypeName.equals(Util.concatenateName(qualifier, name, '.'))))
			return ImportRewriteContext.RES_NAME_FOUND;
	}
	return ImportRewriteContext.RES_NAME_UNKNOWN;
}
 
Example 11
Source File: ReorgCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocationCore problem,
		Collection<ChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked = cu.getResource().isLinked();

	IJavaProject javaProject= cu.getJavaProject();
	String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

	CompilationUnit root= context.getASTRoot();

	ASTNode coveredNode= problem.getCoveredNode(root);
	if (!(coveredNode instanceof SimpleName)) {
		return;
	}

	ASTNode parentType= coveredNode.getParent();
	if (!(parentType instanceof AbstractTypeDeclaration)) {
		return;
	}

	String currTypeName= ((SimpleName) coveredNode).getIdentifier();
	String newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());


	boolean hasOtherPublicTypeBefore = false;

	boolean found = false;
	List<AbstractTypeDeclaration> types= root.types();
	for (int i= 0; i < types.size(); i++) {
		AbstractTypeDeclaration curr= types.get(i);
		if (parentType != curr) {
			if (newTypeName.equals(curr.getName().getIdentifier())) {
				return;
			}
			if (!found && Modifier.isPublic(curr.getModifiers())) {
				hasOtherPublicTypeBefore = true;
			}
		} else {
			found = true;
		}
	}

	if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
		proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
	}

	if (!hasOtherPublicTypeBefore && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) {
		String newCUName = JavaModelUtil.getRenamedCUName(cu, currTypeName);
		ICompilationUnit newCU = ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
		if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
			RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newCUName);

			// rename CU
			String label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
			proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, change, IProposalRelevance.RENAME_CU));
		}
	}

}
 
Example 12
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked= cu.getResource().isLinked();

	IJavaProject javaProject= cu.getJavaProject();
	String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

	CompilationUnit root= context.getASTRoot();

	ASTNode coveredNode= problem.getCoveredNode(root);
	if (!(coveredNode instanceof SimpleName))
		return;

	ASTNode parentType= coveredNode.getParent();
	if (!(parentType instanceof AbstractTypeDeclaration))
		return;

	String currTypeName= ((SimpleName) coveredNode).getIdentifier();
	String newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());

	boolean hasOtherPublicTypeBefore= false;

	boolean found= false;
	List<AbstractTypeDeclaration> types= root.types();
	for (int i= 0; i < types.size(); i++) {
		AbstractTypeDeclaration curr= types.get(i);
		if (parentType != curr) {
			if (newTypeName.equals(curr.getName().getIdentifier())) {
				return;
			}
			if (!found && Modifier.isPublic(curr.getModifiers())) {
				hasOtherPublicTypeBefore= true;
			}
		} else {
			found= true;
		}
	}
	if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
		proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
	}

	if (!hasOtherPublicTypeBefore) {
		String newCUName= JavaModelUtil.getRenamedCUName(cu, currTypeName);
		ICompilationUnit newCU= ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
		if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
			RenameCompilationUnitChange change= new RenameCompilationUnitChange(cu, newCUName);

			// rename CU
			String label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
			proposals.add(new ChangeCorrectionProposal(label, change, IProposalRelevance.RENAME_CU, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME)));
		}
	}
}