use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class HandleFactory method getJarPkgFragmentRoot.
/**
* Returns the package fragment root that corresponds to the given jar path.
* See createOpenable(...) for the format of the jar path string.
* If not null, uses the given scope as a hint for getting Java project handles.
*/
private PackageFragmentRoot getJarPkgFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPathString, IJavaSearchScope scope) {
IPath jarPath = new Path(jarPathString);
Object target = JavaModel.getTarget(jarPath, false);
if (target instanceof IFile) {
// internal jar: is it on the classpath of its project?
// e.g. org.eclipse.swt.win32/ws/win32/swt.jar
// is NOT on the classpath of org.eclipse.swt.win32
IFile jarFile = (IFile) target;
JavaProject javaProject = (JavaProject) this.javaModel.getJavaProject(jarFile);
try {
IClasspathEntry entry = javaProject.getClasspathEntryFor(jarPath);
if (entry != null) {
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot(jarFile);
}
} catch (JavaModelException e) {
// ignore and try to find another project
}
}
// walk projects in the scope and find the first one that has the given jar path in its classpath
IJavaProject[] projects;
if (scope != null) {
if (scope instanceof AbstractJavaSearchScope) {
PackageFragmentRoot root = (PackageFragmentRoot) ((AbstractJavaSearchScope) scope).packageFragmentRoot(resourcePathString, jarSeparatorIndex, jarPathString);
if (root != null)
return root;
} else {
IPath[] enclosingProjectsAndJars = scope.enclosingProjectsAndJars();
int length = enclosingProjectsAndJars.length;
projects = new IJavaProject[length];
int index = 0;
for (int i = 0; i < length; i++) {
IPath path = enclosingProjectsAndJars[i];
if (path.segmentCount() == 1) {
projects[index++] = this.javaModel.getJavaProject(path.segment(0));
}
}
if (index < length) {
System.arraycopy(projects, 0, projects = new IJavaProject[index], 0, index);
}
PackageFragmentRoot root = getJarPkgFragmentRoot(jarPath, target, projects);
if (root != null) {
return root;
}
}
}
// not found in the scope, walk all projects
try {
projects = this.javaModel.getJavaProjects();
} catch (JavaModelException e) {
// java model is not accessible
return null;
}
return getJarPkgFragmentRoot(jarPath, target, projects);
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class Util method getUnresolvedJavaElement.
/**
* Return the java element corresponding to the given compiler binding.
*/
public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
JavaElement unresolvedJavaElement = getUnresolvedJavaElement(methodBinding.declaringClass, workingCopyOwner, bindingsToNodes);
if (unresolvedJavaElement == null || unresolvedJavaElement.getElementType() != IJavaElement.TYPE) {
return null;
}
IType declaringType = (IType) unresolvedJavaElement;
org.eclipse.jdt.internal.compiler.ast.ASTNode node = bindingsToNodes == null ? null : bindingsToNodes.get(methodBinding);
if (node != null && !declaringType.isBinary()) {
if (node instanceof AnnotationMethodDeclaration) {
// node is an AnnotationMethodDeclaration
AnnotationMethodDeclaration typeMemberDeclaration = (AnnotationMethodDeclaration) node;
// annotation type members don't have parameters
return (JavaElement) declaringType.getMethod(String.valueOf(typeMemberDeclaration.selector), CharOperation.NO_STRINGS);
} else {
// node is an MethodDeclaration
MethodDeclaration methodDeclaration = (MethodDeclaration) node;
Argument[] arguments = methodDeclaration.arguments;
String[] parameterSignatures;
if (arguments != null) {
parameterSignatures = new String[arguments.length];
for (int i = 0; i < arguments.length; i++) {
Argument argument = arguments[i];
TypeReference typeReference = argument.type;
int arrayDim = typeReference.dimensions();
String typeSig = Signature.createTypeSignature(CharOperation.concatWith(typeReference.getTypeName(), '.'), false);
if (arrayDim > 0) {
typeSig = Signature.createArraySignature(typeSig, arrayDim);
}
parameterSignatures[i] = typeSig;
}
} else {
parameterSignatures = CharOperation.NO_STRINGS;
}
return (JavaElement) declaringType.getMethod(String.valueOf(methodDeclaration.selector), parameterSignatures);
}
} else {
// case of method not in the created AST, or a binary method
org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = methodBinding.original();
String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
boolean isBinary = declaringType.isBinary();
ReferenceBinding enclosingType = original.declaringClass.enclosingType();
// Static inner types' constructors don't get receivers (https://bugs.eclipse.org/bugs/show_bug.cgi?id=388137)
boolean isInnerBinaryTypeConstructor = isBinary && original.isConstructor() && !original.declaringClass.isStatic() && enclosingType != null;
TypeBinding[] parameters = original.parameters;
int length = parameters == null ? 0 : parameters.length;
int declaringIndex = isInnerBinaryTypeConstructor ? 1 : 0;
String[] parameterSignatures = new String[declaringIndex + length];
if (isInnerBinaryTypeConstructor)
parameterSignatures[0] = new String(enclosingType.genericTypeSignature()).replace('/', '.');
for (int i = 0; i < length; i++) {
char[] signature = parameters[i].genericTypeSignature();
if (isBinary) {
signature = CharOperation.replaceOnCopy(signature, '/', '.');
} else {
signature = toUnresolvedTypeSignature(signature);
}
parameterSignatures[declaringIndex + i] = new String(signature);
}
IMethod result = declaringType.getMethod(selector, parameterSignatures);
if (isBinary)
return (JavaElement) result;
if (// if perfect match (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249567 )
result.exists())
return (JavaElement) result;
IMethod[] methods = null;
try {
methods = declaringType.getMethods();
} catch (JavaModelException e) {
// declaring type doesn't exist
return null;
}
IMethod[] candidates = Member.findMethods(result, methods);
if (candidates == null || candidates.length == 0)
return null;
return (JavaElement) candidates[0];
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class StubUtility method getBaseNameFromLocationInParent.
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
if (binding == null)
return null;
ITypeBinding[] parameterTypes = binding.getParameterTypes();
if (// beware of guessed method bindings
parameterTypes.length != arguments.size())
return null;
int index = arguments.indexOf(assignedExpression);
if (index == -1)
return null;
ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding();
if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
return null;
try {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
if (method.getOpenable().getBuffer() != null) {
// avoid dummy names and lookup from Javadoc
String[] parameterNames = method.getParameterNames();
if (index < parameterNames.length) {
return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
}
}
}
} catch (JavaModelException e) {
// ignore
}
return null;
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class StubUtility method suggestArgumentNamesWithProposals.
public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
int nParams = binding.getParameterTypes().length;
if (nParams > 0) {
try {
IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
if (method != null) {
String[] parameterNames = method.getParameterNames();
if (parameterNames.length == nParams) {
return suggestArgumentNamesWithProposals(project, parameterNames);
}
}
} catch (JavaModelException e) {
// ignore
e.printStackTrace();
}
}
String[][] names = new String[nParams][];
for (int i = 0; i < names.length; i++) {
//$NON-NLS-1$
names[i] = new String[] { "arg" + i };
}
return names;
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class StubUtility method getLineDelimiterUsed.
/**
* @param elem a Java element (doesn't have to exist)
* @return the existing or default line delimiter for the element
*/
public static String getLineDelimiterUsed(IJavaElement elem) {
IOpenable openable = elem.getOpenable();
if (openable instanceof ITypeRoot) {
try {
return openable.findRecommendedLineSeparator();
} catch (JavaModelException exception) {
// Use project setting
}
}
IJavaProject project = elem.getJavaProject();
return getProjectLineDelimiter(project.exists() ? project : null);
}
Aggregations