use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class ExtractConstantRefactoring method getExcludedVariableNames.
private String[] getExcludedVariableNames() {
if (fExcludedVariableNames == null) {
try {
IExpressionFragment expr = getSelectedExpression();
Collection<String> takenNames = new ScopeAnalyzer(fCuRewrite.getRoot()).getUsedVariableNames(expr.getStartPosition(), expr.getLength());
fExcludedVariableNames = takenNames.toArray(new String[takenNames.size()]);
} catch (JavaModelException e) {
fExcludedVariableNames = new String[0];
}
}
return fExcludedVariableNames;
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class JavaContext method addStaticImport.
/**
* Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
* either a static field or a static method or a '*' to import all static members of a type.
*
* @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
* name plus a '.*' suffix.
* @return returns either the simple member name if the import was successful or else the qualified name.
* @since 3.4
*/
public String addStaticImport(String qualifiedMemberName) {
if (isReadOnly())
return qualifiedMemberName;
ICompilationUnit cu = getCompilationUnit();
if (cu == null)
return qualifiedMemberName;
int memberOffset = qualifiedMemberName.lastIndexOf('.');
if (memberOffset == -1)
return qualifiedMemberName;
String typeName = qualifiedMemberName.substring(0, memberOffset);
String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
try {
boolean isField;
if ("*".equals(memberName)) {
//$NON-NLS-1$
isField = true;
} else {
IJavaProject javaProject = cu.getJavaProject();
IType type = javaProject.findType(typeName);
if (type == null)
return qualifiedMemberName;
IField field = type.getField(memberName);
if (field.exists()) {
isField = true;
} else if (hasMethod(type, memberName)) {
isField = false;
} else {
return qualifiedMemberName;
}
}
CompilationUnit root = getASTRoot(cu);
if (fImportRewrite == null) {
if (root == null) {
fImportRewrite = StubUtility.createImportRewrite(cu, true);
} else {
fImportRewrite = StubUtility.createImportRewrite(root, true);
}
}
ImportRewriteContext context;
if (root == null)
context = null;
else
context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
} catch (JavaModelException e) {
handleException(null, e);
return typeName;
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class MethodsSourcePositionComparator method compareInTheSameType.
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
try {
IMethod firstMethod = (IMethod) firstMethodBinding.getJavaElement();
IMethod secondMethod = (IMethod) secondMethodBinding.getJavaElement();
if (firstMethod == null || secondMethod == null) {
return 0;
}
ISourceRange firstSourceRange = firstMethod.getSourceRange();
ISourceRange secondSourceRange = secondMethod.getSourceRange();
if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
return firstMethod.getElementName().compareTo(secondMethod.getElementName());
} else {
return firstSourceRange.getOffset() - secondSourceRange.getOffset();
}
} catch (JavaModelException e) {
return 0;
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class JavaSearchScopeFactory method addJavaElements.
private void addJavaElements(Set<IJavaElement> javaElements, IWorkingSet workingSet) {
if (workingSet == null)
return;
if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
javaElements.addAll(Arrays.asList(projects));
} catch (JavaModelException e) {
JavaPlugin.log(e);
}
return;
}
IAdaptable[] elements = workingSet.getElements();
for (int i = 0; i < elements.length; i++) {
IJavaElement javaElement = (IJavaElement) elements[i].getAdapter(IJavaElement.class);
if (javaElement != null) {
addJavaElements(javaElements, javaElement);
continue;
}
IResource resource = (IResource) elements[i].getAdapter(IResource.class);
if (resource != null) {
addJavaElements(javaElements, resource);
}
// else we don't know what to do with it, ignore.
}
}
use of org.eclipse.jdt.core.JavaModelException in project tdi-studio-se by Talend.
the class AddPackageDialog method getPackageFragments.
/**
* Gets the package fragments.
*
* @param javaModel The java model
* @return The package fragments
*/
private Object[] getPackageFragments(IJavaModel javaModel) {
Set<String> packageElements = new HashSet<String>();
IJavaProject[] projects;
try {
projects = javaModel.getJavaProjects();
} catch (JavaModelException e) {
Activator.log(IStatus.ERROR, Messages.getJavaModelFailedMsg, e);
return new Object[0];
}
for (IJavaProject project : projects) {
if (EXTERNAL_PLUGIN_LIBRARIES.equals(project.getResource().getName())) {
continue;
}
IPackageFragmentRoot[] packageFragmentRoots;
try {
packageFragmentRoots = project.getPackageFragmentRoots();
} catch (JavaModelException e) {
continue;
}
for (IPackageFragmentRoot packageFragment : packageFragmentRoots) {
try {
addPackage(packageElements, packageFragment);
} catch (JavaModelException e) {
// do nothing
}
}
}
for (Object packageName : filteringPackages) {
packageElements.remove(packageName);
}
return packageElements.toArray(new String[0]);
}
Aggregations