use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class JavaElementToDtoConverter method getTypes.
private List<Type> getTypes(Object parent) throws JavaModelException {
List<Type> result = new ArrayList<>();
Set<Object> objects = childrens.get(parent);
if (objects == null) {
return result;
}
for (Object object : objects) {
if (object instanceof IType) {
IType type = (IType) object;
Type dtoType = DtoFactory.newDto(Type.class);
dtoType.setElementName(type.getElementName());
dtoType.setLabel(JavaElementLabels.getElementLabel(type, JavaElementLabels.ALL_DEFAULT));
dtoType.setHandleIdentifier(type.getHandleIdentifier());
dtoType.setFlags(type.getFlags());
dtoType.setTypes(getTypes(object));
dtoType.setFields(getFields(object));
dtoType.setMethods(getMethods(object));
dtoType.setInitializers(getInitializes(object));
if (parent instanceof ITypeRoot) {
IType primaryType = ((ITypeRoot) parent).findPrimaryType();
dtoType.setPrimary(type.equals(primaryType));
} else {
dtoType.setPrimary(false);
}
result.add(dtoType);
}
}
return result;
}
use of org.eclipse.jdt.core.ITypeRoot 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.ITypeRoot 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;
}
}
use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class IndexSelector method getQualifiedNames.
private char[][][] getQualifiedNames(ObjectVector types) {
final int size = types.size;
char[][][] focusQualifiedNames = null;
IJavaElement javaElement = this.pattern.focus;
int index = 0;
while (javaElement != null && !(javaElement instanceof ITypeRoot)) {
javaElement = javaElement.getParent();
}
if (javaElement != null) {
IType primaryType = ((ITypeRoot) javaElement).findPrimaryType();
if (primaryType != null) {
focusQualifiedNames = new char[size + 1][][];
focusQualifiedNames[index++] = CharOperation.splitOn('.', primaryType.getFullyQualifiedName().toCharArray());
}
}
if (focusQualifiedNames == null) {
focusQualifiedNames = new char[size][][];
}
for (int i = 0; i < size; i++) {
focusQualifiedNames[index++] = CharOperation.splitOn('.', ((IType) (types.elementAt(i))).getFullyQualifiedName().toCharArray());
}
return focusQualifiedNames.length == 0 ? null : ReferenceCollection.internQualifiedNames(focusQualifiedNames, true);
}
use of org.eclipse.jdt.core.ITypeRoot in project bndtools by bndtools.
the class NewTypeWizardPage method initTypePage.
/**
* Initializes all fields provided by the page with a given selection.
*
* @param elem
* the selection used to initialize this page or <code>
* null</code> if no selection was available
*/
protected void initTypePage(IJavaElement elem) {
//$NON-NLS-1$
String initSuperclass = "java.lang.Object";
ArrayList<String> initSuperinterfaces = new ArrayList<String>(5);
IJavaProject project = null;
IPackageFragment pack = null;
IType enclosingType = null;
if (elem != null) {
// evaluate the enclosing type
project = elem.getJavaProject();
pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
if (typeInCU != null) {
if (typeInCU.getCompilationUnit() != null) {
enclosingType = typeInCU;
}
} else {
ITypeRoot cu = (ITypeRoot) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
if (cu != null) {
enclosingType = cu.findPrimaryType();
}
}
try {
IType type = null;
if (elem.getElementType() == IJavaElement.TYPE) {
type = (IType) elem;
if (type.exists()) {
String superName = SuperInterfaceSelectionDialog.getNameWithTypeParameters(type);
if (type.isInterface()) {
initSuperinterfaces.add(superName);
} else {
initSuperclass = superName;
}
}
}
} catch (JavaModelException e) {
JavaPlugin.log(e);
// ignore this exception now
}
}
//$NON-NLS-1$
String typeName = "";
ITextSelection selection = getCurrentTextSelection();
if (selection != null) {
String text = selection.getText();
if (text != null && validateJavaTypeName(text, project).isOK()) {
typeName = text;
}
}
setPackageFragment(pack, true);
setEnclosingType(enclosingType, true);
setEnclosingTypeSelection(false, true);
setTypeName(typeName, true);
setSuperClass(initSuperclass, true);
setSuperInterfaces(initSuperinterfaces, true);
// from project or workspace
setAddComments(StubUtility.doAddComments(project), true);
}
Aggregations