use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class Util method getJdkLevel.
/**
* Get the jdk level of this root.
* The value can be:
* <ul>
* <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
* <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
* </ul>
* Returns the jdk level
*/
public static long getJdkLevel(Object targetLibrary) {
try {
ClassFileReader reader = null;
if (targetLibrary instanceof IFolder) {
// only internal classfolders are allowed
IFile classFile = findFirstClassFile((IFolder) targetLibrary);
if (classFile != null)
reader = Util.newClassFileReader(classFile);
} else {
// root is a jar file or a zip file
ZipFile jar = null;
try {
IPath path = null;
if (targetLibrary instanceof IResource) {
path = ((IResource) targetLibrary).getFullPath();
} else if (targetLibrary instanceof File) {
File f = (File) targetLibrary;
if (!f.isDirectory()) {
path = new Path(((File) targetLibrary).getPath());
}
}
if (path != null) {
jar = JavaModelManager.getJavaModelManager().getZipFile(path);
for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
ZipEntry member = (ZipEntry) e.nextElement();
String entryName = member.getName();
if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
reader = ClassFileReader.read(jar, entryName);
break;
}
}
}
} catch (CoreException e) {
// ignore
} finally {
JavaModelManager.getJavaModelManager().closeZipFile(jar);
}
}
if (reader != null) {
return reader.getVersion();
}
} catch (CoreException e) {
// ignore
} catch (ClassFormatException e) {
// ignore
} catch (IOException e) {
// ignore
}
return 0;
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class HandleFactory method getPkgFragmentRoot.
/**
* Returns the package fragment root that contains the given resource path.
*/
private PackageFragmentRoot getPkgFragmentRoot(String pathString) {
IPath path = new Path(pathString);
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (int i = 0, max = projects.length; i < max; i++) {
try {
IProject project = projects[i];
if (!project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID))
continue;
IJavaProject javaProject = this.javaModel.getJavaProject(project);
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
for (int j = 0, rootCount = roots.length; j < rootCount; j++) {
PackageFragmentRoot root = (PackageFragmentRoot) roots[j];
if (root.internalPath().isPrefixOf(path) && !Util.isExcluded(path, root.fullInclusionPatternChars(), root.fullExclusionPatternChars(), false)) {
return root;
}
}
} catch (CoreException e) {
// CoreException from hasNature - should not happen since we check that the project is accessible
// JavaModelException from getPackageFragmentRoots - a problem occured while accessing project: nothing we can do, ignore
}
}
return null;
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class StubUtility method getMethodComment.
/*
* Don't use this method directly, use CodeGeneration.
* This method should work with all AST levels.
* @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
*/
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
if (decl.isConstructor()) {
templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
} else if (needsTarget) {
if (delegate)
templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
else
templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
}
Template template = getCodeTemplate(templateName, cu.getJavaProject());
if (template == null) {
return null;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
context.setCompilationUnitVariables(cu);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
if (!decl.isConstructor()) {
context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
}
if (needsTarget) {
if (delegate)
context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
else
context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
}
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
if (buffer == null)
return null;
String str = buffer.getString();
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
// look if Javadoc tags have to be added
TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
if (position == null) {
return str;
}
IDocument textBuffer = new Document(str);
List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
String[] typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
TypeParameter elem = typeParams.get(i);
typeParamNames[i] = elem.getName().getIdentifier();
}
List<SingleVariableDeclaration> params = decl.parameters();
String[] paramNames = new String[params.size()];
for (int i = 0; i < paramNames.length; i++) {
SingleVariableDeclaration elem = params.get(i);
paramNames[i] = elem.getName().getIdentifier();
}
String[] exceptionNames = getExceptionNames(decl);
String returnType = null;
if (!decl.isConstructor()) {
returnType = ASTNodes.asString(getReturnType(decl));
}
int[] tagOffsets = position.getOffsets();
for (int i = tagOffsets.length - 1; i >= 0; i--) {
// from last to first
try {
insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
} catch (BadLocationException e) {
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
}
}
return textBuffer.get();
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class StubUtility method evaluateTemplate.
private static String evaluateTemplate(CodeTemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
if (buffer == null)
return null;
String str = fixEmptyVariables(buffer, fullLineVariables);
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
return str;
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class StubUtility method getTypeComment.
/*
* Don't use this method directly, use CodeGeneration.
* @see org.eclipse.jdt.ui.CodeGeneration#getTypeComment(ICompilationUnit, String, String[], String)
*/
public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String[] typeParameterNames, String lineDelim) throws CoreException {
Template template = getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, cu.getJavaProject());
if (template == null) {
return null;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
context.setCompilationUnitVariables(cu);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, Signature.getQualifier(typeQualifiedName));
context.setVariable(CodeTemplateContextType.TYPENAME, Signature.getSimpleName(typeQualifiedName));
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
String str = buffer.getString();
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
// look if Javadoc tags have to be added
TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
if (position == null) {
return str;
}
IDocument document = new Document(str);
int[] tagOffsets = position.getOffsets();
for (int i = tagOffsets.length - 1; i >= 0; i--) {
// from last to first
try {
insertTag(document, tagOffsets[i], position.getLength(), EMPTY, EMPTY, null, typeParameterNames, false, lineDelim);
} catch (BadLocationException e) {
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
}
}
return document.get();
}
Aggregations