List of usage examples for org.eclipse.jdt.core ITypeRoot getHandleIdentifier
String getHandleIdentifier();
From source file:org.eclipse.ajdt.core.model.AJProjectModelFacade.java
License:Open Source License
/** * Find the java element corresponding to the handle. We know that it exists in * as a class file in a binary folder, but it may actually be * a source file in a different project//from www . j av a 2s. co m */ private IJavaElement findElementInBinaryFolder(HandleInfo handleInfo, IClassFile classFile) throws JavaModelException { IJavaElement candidate = classFile; // we have a class file that is not in a jar. // can we find this as a source file in some project? IPath path = classFile.getPath(); IJavaProject otherProject = JavaCore.create(project).getJavaModel().getJavaProject(path.segment(0)); ITypeRoot typeRoot = classFile; if (otherProject.exists()) { IType type = otherProject.findType(handleInfo.sourceTypeQualName()); typeRoot = type.getTypeRoot(); if (typeRoot instanceof ICompilationUnit) { AJCompilationUnit newUnit = CompilationUnitTools .convertToAJCompilationUnit((ICompilationUnit) typeRoot); typeRoot = newUnit != null ? newUnit : typeRoot; } } if (handleInfo.isType) { switch (typeRoot.getElementType()) { case IJavaElement.CLASS_FILE: candidate = ((IClassFile) typeRoot).getType(); break; case IJavaElement.COMPILATION_UNIT: candidate = CompilationUnitTools.findType((ICompilationUnit) typeRoot, classFile.getType().getElementName(), true); break; default: // shouldn't happen break; } } else if (!handleInfo.isFile && !handleInfo.isType) { // program element will exist only if coming from aspect path IProgramElement ipe = getProgramElement(handleInfo.origAJHandle); if (ipe != IHierarchy.NO_STRUCTURE) { candidate = typeRoot.getElementAt(offsetFromLine(typeRoot, ipe.getSourceLocation())); } else { String newHandle = typeRoot.getHandleIdentifier() + handleInfo.restHandle; candidate = AspectJCore.create(newHandle); } } return candidate; }
From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java
License:Open Source License
/** * Get the compilation unit representation of the java file. * * @param javaProject/*from w w w . j a v a 2 s . c om*/ * 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; }
From source file:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java
License:Open Source License
/** * Returns the {@link IJavaElement} at the given offset. If no {@link IJavaElement} is selected, the innermost * enclosing {@link IJavaElement} is returned (e.g., the declaring method or type). If both selection resolutions * fail, {@link Optional#absent()} is returned. *//*from ww w . j a va2s . c om*/ public static Optional<IJavaElement> resolveJavaElementFromTypeRootInEditor(final ITypeRoot root, final int offset) { ensureIsNotNull(root); try { if (isInvalidSelection(root, offset)) { return absent(); } // try resolve elements at current offset final IJavaElement[] elements = root.codeSelect(offset, 0); if (elements.length > 0) { // return java element under cursor/selection start return of(elements[0]); } else { // XXX MB: decided against selection changes because these // frequent changes were too disturbing return absent(); // ignore that for a while: // // if no java element has been selected, return the innermost // Java element enclosing a given offset. // // This might evaluate to null. // IJavaElement enclosingElement = root.getElementAt(offset); // if (enclosingElement == null) { // // selection occurred in empty space somewhere before the // type declaration. // // return type-root then. // enclosingElement = root; // } // return of(enclosingElement); } } catch (final Exception e) { // actually, these can happen when using snipmatch's in-editor completion. // fractions of seconds seem potentially to lead to this exception, thus, we swallow them here. if (!isInvalidSelection(root, offset)) { log(ERROR_FAILED_TO_RESOLVE_SELECTION, root.getHandleIdentifier(), offset, e); } return absent(); } }