use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class SelectionConverter method resolveEnclosingElement.
// public static IJavaElement getElementAtOffset(ITypeRoot input, ITextSelection selection) throws JavaModelException {
// if (input instanceof ICompilationUnit) {
// JavaModelUtil.reconcile((ICompilationUnit)input);
// }
// IJavaElement ref = input.getElementAt(selection.getOffset());
// if (ref == null)
// return input;
// return ref;
// }
//
// public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
// ITypeRoot input = getInput(editor);
// if (input != null)
// return resolveEnclosingElement(input, selection);
// return null;
// }
//
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
IJavaElement atOffset = null;
if (input instanceof ICompilationUnit) {
ICompilationUnit cunit = (ICompilationUnit) input;
JavaModelUtil.reconcile(cunit);
atOffset = cunit.getElementAt(selection.getOffset());
} else if (input instanceof IClassFile) {
IClassFile cfile = (IClassFile) input;
atOffset = cfile.getElementAt(selection.getOffset());
} else {
return null;
}
if (atOffset == null) {
return input;
} else {
int selectionEnd = selection.getOffset() + selection.getLength();
IJavaElement result = atOffset;
if (atOffset instanceof ISourceReference) {
ISourceRange range = ((ISourceReference) atOffset).getSourceRange();
while (range.getOffset() + range.getLength() < selectionEnd) {
result = result.getParent();
if (!(result instanceof ISourceReference)) {
result = input;
break;
}
range = ((ISourceReference) result).getSourceRange();
}
}
return result;
}
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaElementToDtoConverter method addCompilationUnitsAndClassFiles.
private void addCompilationUnitsAndClassFiles(Object parent, List<CompilationUnit> compilationUnits, List<ClassFile> classFiles) throws JavaModelException {
Set<Object> childrens = this.childrens.get(parent);
for (Object children : childrens) {
if (children instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) children;
CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
compilationUnit.setElementName(unit.getElementName());
compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
compilationUnit.setLabel(JavaElementLabels.getElementLabel(unit, JavaElementLabels.ALL_DEFAULT));
compilationUnit.setImports(getImports(children));
compilationUnit.setTypes(getTypes(children));
compilationUnits.add(compilationUnit);
} else if (children instanceof IClassFile) {
ClassFile classFile = DtoFactory.newDto(ClassFile.class);
IClassFile clazz = (IClassFile) children;
classFile.setHandleIdentifier(clazz.getHandleIdentifier());
classFile.setElementName(clazz.getElementName());
classFile.setPath(clazz.getType().getFullyQualifiedName());
classFile.setLabel(JavaElementLabels.getElementLabel(clazz, JavaElementLabels.ALL_DEFAULT));
classFile.setProjectPath(clazz.getJavaProject().getPath().toOSString());
classFile.setType(getTypes(children).get(0));
classFiles.add(classFile);
}
}
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaNavigation method getCompilationUnitByPath.
/**
* Get the compilation unit representation of the java file.
*
* @param javaProject
* path to the project which is contained class file
* @param fqn
* fully qualified name of the class file
* @param isShowingInheritedMembers
* <code>true</code> iff inherited members are shown
* @return instance of {@link CompilationUnit}
* @throws JavaModelException
* when JavaModel has a failure
*/
public CompilationUnit getCompilationUnitByPath(IJavaProject javaProject, String fqn, boolean isShowingInheritedMembers) throws JavaModelException {
IType type = javaProject.findType(fqn);
CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
ITypeRoot unit;
if (type.isBinary()) {
unit = type.getClassFile();
compilationUnit.setPath(((IClassFile) unit).getType().getFullyQualifiedName());
} else {
unit = type.getCompilationUnit();
compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
}
compilationUnit.setElementName(unit.getElementName());
compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
compilationUnit.setLabel(org.eclipse.jdt.ui.JavaElementLabels.getElementLabel(unit, org.eclipse.jdt.ui.JavaElementLabels.ALL_DEFAULT));
List<Type> types = new ArrayList<>(1);
Type dtoType = convertToDTOType(type);
dtoType.setPrimary(true);
types.add(dtoType);
compilationUnit.setTypes(types);
if (isShowingInheritedMembers) {
compilationUnit.setSuperTypes(calculateSuperTypes(type));
}
return compilationUnit;
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaNavigation method getEntry.
public JarEntry getEntry(IJavaProject project, int rootId, String path) throws CoreException {
IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
if (root == null) {
return null;
}
if (path.startsWith("/")) {
JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
ZipFile jar = null;
try {
jar = jarPackageFragmentRoot.getJar();
ZipEntry entry = jar.getEntry(path.substring(1));
if (entry != null) {
JarEntry result = DtoFactory.getInstance().createDto(JarEntry.class);
result.setType(JarEntryType.FILE);
result.setPath(path);
result.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1));
return result;
}
} finally {
if (jar != null) {
JavaModelManager.getJavaModelManager().closeZipFile(jar);
}
}
Object[] resources = root.getNonJavaResources();
for (Object resource : resources) {
if (resource instanceof JarEntryFile) {
JarEntryFile file = (JarEntryFile) resource;
if (file.getFullPath().toOSString().equals(path)) {
return getJarEntryResource(file);
}
}
if (resource instanceof JarEntryDirectory) {
JarEntryDirectory directory = (JarEntryDirectory) resource;
JarEntryFile file = findJarFile(directory, path);
if (file != null) {
return getJarEntryResource(file);
}
}
}
} else {
//java class or file
IType type = project.findType(path);
if (type != null && type.isBinary()) {
IClassFile classFile = type.getClassFile();
return getJarClass(classFile);
}
}
return null;
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class ReplaceInvocationsRefactoring method resolveSourceProvider.
private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
final IMethod method = (IMethod) methodBinding.getJavaElement();
ITypeRoot typeRoot;
IDocument source;
CompilationUnit methodDeclarationAstRoot;
ICompilationUnit methodCu = (method).getCompilationUnit();
if (methodCu != null) {
typeRoot = methodCu;
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(methodCu);
parser.setFocalPosition(method.getNameRange().getOffset());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
MethodDeclaration methodDecl = (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
AST ast = compilationUnit.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Block newBody = ast.newBlock();
newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
rewrite.replace(methodDecl.getBody(), newBody, null);
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
for (int i = 0; i < parameters.size(); i++) {
SingleVariableDeclaration parameter = parameters.get(i);
rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
}
TextEdit textEdit = rewrite.rewriteAST();
Document document = new Document(methodCu.getBuffer().getContents());
try {
textEdit.apply(document);
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
source = document;
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(source.get(), methodCu, true, true, null);
} else {
IClassFile classFile = method.getClassFile();
//TODO: use source if available?
StubCreator stubCreator = new StubCreator(true) {
@Override
protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
if (currentMethod.equals(method)) {
fBuffer.append(fBody);
} else {
super.appendMethodBody(currentMethod);
}
}
/*
* @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
*/
@Override
protected void appendMethodParameterName(IMethod currentMethod, int index) {
if (currentMethod.equals(method)) {
fBuffer.append(fParameterNames[index]);
} else {
super.appendMethodParameterName(currentMethod, index);
}
}
};
String stub = stubCreator.createStub(classFile.getType(), null);
source = new Document(stub);
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(stub, classFile, true, true, null);
typeRoot = classFile;
}
ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
if (node instanceof MethodDeclaration) {
return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
} else {
status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
return null;
}
}
Aggregations