Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#getElementName()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#getElementName() . 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: NamedMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected String getKey(IType type, boolean forceOpen) throws JavaModelException {
	StringBuffer key = new StringBuffer();
	key.append('L');
	String packageName = type.getPackageFragment().getElementName();
	key.append(packageName.replace('.', '/'));
	if (packageName.length() > 0)
		key.append('/');
	String typeQualifiedName = type.getTypeQualifiedName('$');
	ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);
	if (cu != null) {
		String cuName = cu.getElementName();
		String mainTypeName = cuName.substring(0, cuName.lastIndexOf('.'));
		int end = typeQualifiedName.indexOf('$');
		if (end == -1)
			end = typeQualifiedName.length();
		String topLevelTypeName = typeQualifiedName.substring(0, end);
		if (!mainTypeName.equals(topLevelTypeName)) {
			key.append(mainTypeName);
			key.append('~');
		}
	}
	key.append(typeQualifiedName);
	key.append(';');
	return key.toString();
}
 
Example 2
Source File: DeltaConverter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 * @deprecated This method is not used anymore.
 */
@Deprecated
protected String getExpectedPrimaryTypeNameFor(ICompilationUnit cu) {
	String fileName = cu.getElementName();
	String typeName = fileName.substring(0, fileName.lastIndexOf('.'));
	IPackageFragment pkg = (IPackageFragment) cu.getParent();
	if (!pkg.isDefaultPackage()) {
		typeName = pkg.getElementName() + '.' + typeName;
	}
	return typeName;
}
 
Example 3
Source File: GdtJavaProblem.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private static String getFileNameFromASTNode(ASTNode node) {
  ICompilationUnit compilationUnit = JavaASTUtils.getCompilationUnit(node);
  if (compilationUnit != null) {
    return compilationUnit.getElementName();
  }
  // We may have an unattached AST (does not belong to the Java Model)
  return "";
}
 
Example 4
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Compute a new name for a compilation unit, given the name of the new main type.
 * This query tries to maintain the existing extension (e.g. ".java").
 *
 * @param cu a compilation unit
 * @param newMainName the new name of the cu's main type (without extension)
 * @return the new name for the compilation unit
 */
public static String getRenamedCUName(ICompilationUnit cu, String newMainName) {
	String oldName = cu.getElementName();
	int i = oldName.lastIndexOf('.');
	if (i != -1) {
		return newMainName + oldName.substring(i);
	} else {
		return newMainName;
	}
}
 
Example 5
Source File: JavaBuilderState.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
private String getSimplePrimaryTypeName(ICompilationUnit compilationUnit) {
	String elementName = compilationUnit.getElementName();
	return elementName.substring(0, elementName.lastIndexOf("."));
}
 
Example 6
Source File: RenameCompilationUnitChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public RenameCompilationUnitChange(ICompilationUnit unit, String newName) {
	this(unit.getResource().getFullPath(), unit.getElementName(), newName, IResource.NULL_STAMP);
	Assert.isTrue(!unit.isReadOnly(), "compilation unit must not be read-only"); //$NON-NLS-1$
}
 
Example 7
Source File: RenameCompilationUnitChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public RenameCompilationUnitChange(ICompilationUnit unit, String newName) {
	this(unit.getResource().getFullPath(), unit.getElementName(), newName, IResource.NULL_STAMP);
	Assert.isTrue(!unit.isReadOnly(), "compilation unit must not be read-only"); //$NON-NLS-1$
}
 
Example 8
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected String resolve(TemplateContext context) {
	ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();

	return (unit == null) ? null : unit.getElementName();
}
 
Example 9
Source File: AssistCompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public AssistCompilationUnit(ICompilationUnit compilationUnit, WorkingCopyOwner owner, Map bindingCache, Map infoCache) {
	super((PackageFragment)compilationUnit.getParent(), compilationUnit.getElementName(), owner);
	this.bindingCache = bindingCache;
	this.infoCache = infoCache;
}
 
Example 10
Source File: JavaUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
public static boolean isClassImplement(IProject project, String interfaceTocheck) throws JavaModelException{
		IJavaProject jp = JavaCore.create(project);
		try {
			IPackageFragment[] packageFragments = jp.getPackageFragments();
			for (IPackageFragment fragment : packageFragments) {
				IJavaElement[] children = fragment.getChildren();
				for (IJavaElement element : children) {
						ICompilationUnit[] compilationUnits = fragment.getCompilationUnits();
						for (ICompilationUnit unit : compilationUnits) {
							String a = unit.getElementName();
							IType[] types = unit.getTypes();
							for (IType type : types) {
								String[] superInterfaceNames = type.getSuperInterfaceNames();
								if(superInterfaceNames != null){
									for (String interfaceName : superInterfaceNames) {
										if(interfaceName.equals(interfaceTocheck)){
											return true;
										}
									}
								}
							}
						}
				}
			}
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		
//		IType findType = getJavaITypeForClass(jp, fullyQualifiedClassName);
//		if (findType!=null && findType.getSuperclassName()!=null){
//			String[][] resolveType = findType.resolveType(findType.getSuperclassName());
//			if (resolveType!=null){
//				String fullyQualifiedSuperClassName=(resolveType[0][0]).toString()+"."+(resolveType[0][1]).toString();
//				boolean result = isClassImplement(project, fullyQualifiedSuperClassName,classNameToCheck);
//				if (result){
//					return true;
//				}
//			}
//		}
	
	return false;
		
	}
 
Example 11
Source File: ReplaceTypeCodeWithStateStrategyInputPage.java    From JDeodorant with MIT License 4 votes vote down vote up
public ReplaceTypeCodeWithStateStrategyInputPage(ReplaceTypeCodeWithStateStrategy refactoring) {
	super("State/Strategy Type Names");
	this.refactoring = refactoring;
	ICompilationUnit sourceCompilationUnit = (ICompilationUnit)refactoring.getSourceCompilationUnit().getJavaElement();
	this.parentPackage = (IPackageFragment)sourceCompilationUnit.getParent();
	this.parentPackageClassNames = new ArrayList<String>();
	try {
		for(ICompilationUnit compilationUnit : parentPackage.getCompilationUnits()) {
			String className = compilationUnit.getElementName();
			parentPackageClassNames.add(className.substring(0, className.indexOf(".java")));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	TypeVisitor typeVisitor = new TypeVisitor();
	refactoring.getSourceCompilationUnit().accept(typeVisitor);
	Set<ITypeBinding> typeBindings = typeVisitor.getTypeBindings();
	for(ITypeBinding typeBinding : typeBindings) {
		if(!parentPackageClassNames.contains(typeBinding.getName()) && !typeBinding.isNested()) {
			parentPackageClassNames.add(typeBinding.getName());
		}
	}
	this.javaLangClassNames = new ArrayList<String>();
	this.javaLangClassNames.add("Boolean");
	this.javaLangClassNames.add("Byte");
	this.javaLangClassNames.add("Character");
	this.javaLangClassNames.add("Class");
	this.javaLangClassNames.add("Double");
	this.javaLangClassNames.add("Enum");
	this.javaLangClassNames.add("Error");
	this.javaLangClassNames.add("Exception");
	this.javaLangClassNames.add("Float");
	this.javaLangClassNames.add("Integer");
	this.javaLangClassNames.add("Long");
	this.javaLangClassNames.add("Math");
	this.javaLangClassNames.add("Number");
	this.javaLangClassNames.add("Object");
	this.javaLangClassNames.add("Package");
	this.javaLangClassNames.add("Process");
	this.javaLangClassNames.add("Runtime");
	this.javaLangClassNames.add("Short");
	this.javaLangClassNames.add("String");
	this.javaLangClassNames.add("StringBuffer");
	this.javaLangClassNames.add("StringBuilder");
	this.javaLangClassNames.add("System");
	this.javaLangClassNames.add("Thread");
	this.javaLangClassNames.add("Void");
	this.textMap = new LinkedHashMap<Text, SimpleName>();
	this.defaultNamingMap = new LinkedHashMap<Text, String>();
}
 
Example 12
Source File: ExtractClassInputPage.java    From JDeodorant with MIT License 4 votes vote down vote up
public ExtractClassInputPage(ExtractClassRefactoring refactoring) {
	super("Extracted Class Name");
	this.refactoring = refactoring;
	ICompilationUnit sourceCompilationUnit = (ICompilationUnit)refactoring.getSourceCompilationUnit().getJavaElement();
	this.parentPackage = (IPackageFragment)sourceCompilationUnit.getParent();
	this.parentPackageClassNames = new ArrayList<String>();
	try {
		for(ICompilationUnit compilationUnit : parentPackage.getCompilationUnits()) {
			String className = compilationUnit.getElementName();
			parentPackageClassNames.add(className.substring(0, className.indexOf(".java")));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	this.javaLangClassNames = new ArrayList<String>();
	this.javaLangClassNames.add("Boolean");
	this.javaLangClassNames.add("Byte");
	this.javaLangClassNames.add("Character");
	this.javaLangClassNames.add("Class");
	this.javaLangClassNames.add("Double");
	this.javaLangClassNames.add("Enum");
	this.javaLangClassNames.add("Error");
	this.javaLangClassNames.add("Exception");
	this.javaLangClassNames.add("Float");
	this.javaLangClassNames.add("Integer");
	this.javaLangClassNames.add("Long");
	this.javaLangClassNames.add("Math");
	this.javaLangClassNames.add("Number");
	this.javaLangClassNames.add("Object");
	this.javaLangClassNames.add("Package");
	this.javaLangClassNames.add("Process");
	this.javaLangClassNames.add("Runtime");
	this.javaLangClassNames.add("Short");
	this.javaLangClassNames.add("String");
	this.javaLangClassNames.add("StringBuffer");
	this.javaLangClassNames.add("StringBuilder");
	this.javaLangClassNames.add("System");
	this.javaLangClassNames.add("Thread");
	this.javaLangClassNames.add("Void");
	this.textMap = new LinkedHashMap<Text, String>();
	this.defaultNamingMap = new LinkedHashMap<Text, String>();
}
 
Example 13
Source File: TestUtils.java    From CogniCrypt with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * This method returns the Path to File in a certain DeveloperProject
 *
 * @param project
 * @param packageName
 * @param cu
 * @return
 * @throws CoreException
 */
private static String getFilePathInProject(final DeveloperProject project, final String packageName, final ICompilationUnit cu) throws CoreException {
	final String srcPath = project.getProjectPath() + Constants.innerFileSeparator + project.getSourcePath();
	final String cuPath = srcPath + Constants.innerFileSeparator + packageName + Constants.innerFileSeparator + cu.getElementName();
	return cuPath;
}