Java Code Examples for org.eclipse.jdt.core.IClassFile

The following examples show how to use org.eclipse.jdt.core.IClassFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
/**
 * Adds all of the openables defined within this package fragment to the
 * list.
 */
private void injectAllOpenablesForPackageFragment(
	IPackageFragment packFrag,
	ArrayList openables) {

	try {
		IPackageFragmentRoot root = (IPackageFragmentRoot) packFrag.getParent();
		int kind = root.getKind();
		if (kind != 0) {
			boolean isSourcePackageFragment = (kind == IPackageFragmentRoot.K_SOURCE);
			if (isSourcePackageFragment) {
				ICompilationUnit[] cus = packFrag.getCompilationUnits();
				for (int i = 0, length = cus.length; i < length; i++) {
					openables.add(cus[i]);
				}
			} else {
				IClassFile[] classFiles = packFrag.getClassFiles();
				for (int i = 0, length = classFiles.length; i < length; i++) {
					openables.add(classFiles[i]);
				}
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
}
 
Example #2
Source Project: lapse-plus   Author: OWASP   File: LapseView.java    License: GNU General Public License v3.0 6 votes vote down vote up
private CompilationUnit internalSetInput(IOpenable input) throws CoreException {
	IBuffer buffer = input.getBuffer();
	if (buffer == null) {
		JavaPlugin.logErrorMessage("Input has no buffer"); //$NON-NLS-1$
	}
	if (input instanceof ICompilationUnit) {
		fParser.setSource((ICompilationUnit) input);
	} else {
		fParser.setSource((IClassFile) input);
	}

	try {
		CompilationUnit root = (CompilationUnit) fParser.createAST(null);
		log("Recomputed the AST for " + buffer.getUnderlyingResource().getName());
						
		if (root == null) {
			JavaPlugin.logErrorMessage("Could not create AST"); //$NON-NLS-1$
		}

		return root;
	} catch (RuntimeException e) {
		JavaPlugin.logErrorMessage("Could not create AST:\n" + e.getMessage()); //$NON-NLS-1$
		return null;
	}
}
 
Example #3
Source Project: java-debug   Author: microsoft   File: JdtSourceLookUpProvider.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getSourceFileURI(String fullyQualifiedName, String sourcePath) {
    if (sourcePath == null) {
        return null;
    }

    Object sourceElement = JdtUtils.findSourceElement(sourcePath, getSourceContainers());
    if (sourceElement instanceof IResource) {
        return getFileURI((IResource) sourceElement);
    } else if (sourceElement instanceof IClassFile) {
        try {
            IClassFile file = (IClassFile) sourceElement;
            if (file.getBuffer() != null) {
                return getFileURI(file);
            }
        } catch (JavaModelException e) {
            // do nothing.
        }
    }
    return null;
}
 
Example #4
Source Project: java-debug   Author: microsoft   File: JdtSourceLookUpProvider.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private String getContents(IClassFile cf) {
    String source = null;
    if (cf != null) {
        try {
            IBuffer buffer = cf.getBuffer();
            if (buffer != null) {
                source = buffer.getContents();
            }
        } catch (JavaModelException e) {
            logger.log(Level.SEVERE, String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
        }
        if (source == null) {
            source = "";
        }
    }
    return source;
}
 
Example #5
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: JavaEditorBreadcrumb.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the input for the given element. The Java breadcrumb does not show some elements of
 * the model:
 * <ul>
 * 		<li><code>ITypeRoots</li>
 * 		<li><code>IPackageDeclaration</li>
 * 		<li><code>IImportContainer</li>
 * 		<li><code>IImportDeclaration</li>
 * </ul>
 *
 * @param element the potential input element
 * @return the element to use as input
 */
private IJavaElement getInput(IJavaElement element) {
	try {
		if (element instanceof IImportDeclaration)
			element= element.getParent();

		if (element instanceof IImportContainer)
			element= element.getParent();

		if (element instanceof IPackageDeclaration)
			element= element.getParent();

		if (element instanceof ICompilationUnit) {
			IType[] types= ((ICompilationUnit) element).getTypes();
			if (types.length > 0)
				element= types[0];
		}

		if (element instanceof IClassFile)
			element= ((IClassFile) element).getType();

		return element;
	} catch (JavaModelException e) {
		return null;
	}
}
 
Example #6
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: MembersView.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Answers if the given <code>element</code> is a valid
 * element for this part.
 *
 * @param 	element	the object to test
 * @return	<true> if the given element is a valid element
 */
@Override
protected boolean isValidElement(Object element) {
	if (element instanceof IMember)
		return super.isValidElement(((IMember)element).getDeclaringType());
	else if (element instanceof IImportDeclaration)
		return isValidElement(((IJavaElement)element).getParent());
	else if (element instanceof IImportContainer) {
		Object input= getViewer().getInput();
		if (input instanceof IJavaElement) {
			ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT);
			if (cu != null) {
				ICompilationUnit importContainerCu= (ICompilationUnit)((IJavaElement)element).getAncestor(IJavaElement.COMPILATION_UNIT);
				return cu.equals(importContainerCu);
			} else {
				IClassFile cf= (IClassFile)((IJavaElement)input).getAncestor(IJavaElement.CLASS_FILE);
				IClassFile importContainerCf= (IClassFile)((IJavaElement)element).getAncestor(IJavaElement.CLASS_FILE);
				return cf != null && cf.equals(importContainerCf);
			}
		}
	}
	return false;
}
 
Example #7
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: AbstractJavaSearchResult.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public IFile getFile(Object element) {
	if (element instanceof IJavaElement) {
		IJavaElement javaElement= (IJavaElement) element;
		ICompilationUnit cu= (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (cu != null) {
			return (IFile) cu.getResource();
		} else {
			IClassFile cf= (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE);
			if (cf != null)
				return (IFile) cf.getResource();
		}
		return null;
	}
	if (element instanceof IFile)
		return (IFile) element;
	return null;
}
 
Example #8
public static ResourceMapping create(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			return create((IType)element);
		case IJavaElement.COMPILATION_UNIT:
			return create((ICompilationUnit)element);
		case IJavaElement.CLASS_FILE:
			return create((IClassFile)element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return create((IPackageFragment)element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return create((IPackageFragmentRoot)element);
		case IJavaElement.JAVA_PROJECT:
			return create((IJavaProject)element);
		case IJavaElement.JAVA_MODEL:
			return create((IJavaModel)element);
		default:
			return null;
	}

}
 
Example #9
/**
 * Creates an annotation model derived from the given class file editor input.
 *
 * @param classFileEditorInput the editor input from which to query the annotations
 * @return the created annotation model
 * @exception CoreException if the editor input could not be accessed
 */
protected IAnnotationModel createClassFileAnnotationModel(IClassFileEditorInput classFileEditorInput) throws CoreException {
	IResource resource= null;
	IClassFile classFile= classFileEditorInput.getClassFile();

	IResourceLocator locator= (IResourceLocator) classFile.getAdapter(IResourceLocator.class);
	if (locator != null)
		resource= locator.getContainingResource(classFile);

	if (resource != null) {
		ClassFileMarkerAnnotationModel model= new ClassFileMarkerAnnotationModel(resource);
		model.setClassFile(classFile);
		return model;
	}

	return null;
}
 
Example #10
Source Project: eclipse.jdt.ls   Author: eclipse   File: CollectingSearchRequestor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static boolean isBinaryElement(Object element) throws JavaModelException {
	if (element instanceof IMember) {
		return ((IMember)element).isBinary();

	} else if (element instanceof ICompilationUnit) {
		return true;

	} else if (element instanceof IClassFile) {
		return false;

	} else if (element instanceof IPackageFragment) {
		return isBinaryElement(((IPackageFragment) element).getParent());

	} else if (element instanceof IPackageFragmentRoot) {
		return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_BINARY;

	}
	return false;

}
 
Example #11
Source Project: eclipse.jdt.ls   Author: eclipse   File: JavaElementLabelComposer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Appends the label for a class file. Considers the CF_* flags.
 *
 * @param classFile the element to render
 * @param flags the rendering flags. Flags with names starting with 'CF_' are considered.
 */
public void appendClassFileLabel(IClassFile classFile, long flags) {
	if (getFlag(flags, JavaElementLabels.CF_QUALIFIED)) {
		IPackageFragment pack= (IPackageFragment) classFile.getParent();
		if (!pack.isDefaultPackage()) {
			appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
			fBuilder.append('.');
		}
	}
	fBuilder.append(classFile.getElementName());

	if (getFlag(flags, JavaElementLabels.CF_POST_QUALIFIED)) {
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentLabel((IPackageFragment) classFile.getParent(), flags & QUALIFIER_FLAGS);
	}
}
 
Example #12
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: OccurrencesSearchResult.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
	//TODO same code in JavaSearchResult
	IEditorInput editorInput= editor.getEditorInput();
	if (editorInput instanceof IFileEditorInput)  {
		IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput;
		return computeContainedMatches(result, fileEditorInput.getFile());

	} else if (editorInput instanceof IClassFileEditorInput) {
		IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput;
		IClassFile classFile= classFileEditorInput.getClassFile();

		Object[] elements= getElements();
		if (elements.length == 0)
			return NO_MATCHES;
		//all matches from same file:
		JavaElementLine jel= (JavaElementLine) elements[0];
		if (jel.getJavaElement().equals(classFile))
			return collectMatches(elements);
	}
	return NO_MATCHES;
}
 
Example #13
Source Project: eclipse.jdt.ls   Author: eclipse   File: SourceContentProvider.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
	String source = null;
	try {
		IBuffer buffer = classFile.getBuffer();
		if (buffer == null) {
			ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
			if (projectsManager != null) {
				Optional<IBuildSupport> bs = projectsManager.getBuildSupport(classFile.getJavaProject().getProject());
				if (bs.isPresent()) {
					bs.get().discoverSource(classFile, monitor);
				}
			}
			buffer = classFile.getBuffer();
		}
		if (buffer != null) {
			source = buffer.getContents();
			JavaLanguageServerPlugin.logInfo("ClassFile contents request completed");
		}
	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("Exception getting java element ", e);
	}
	return source;
}
 
Example #14
Source Project: eclipse.jdt.ls   Author: eclipse   File: CallHierarchyHandler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets the location of the Java {@code element} based on the desired
 * {@code locationType}.
 */
private Location getLocation(IJavaElement element, LocationType locationType) throws JavaModelException {
	Assert.isNotNull(element, "element");
	Assert.isNotNull(locationType, "locationType");

	Location location = locationType.toLocation(element);
	if (location == null && element instanceof IType) {
		IType type = (IType) element;
		ICompilationUnit unit = (ICompilationUnit) type.getAncestor(COMPILATION_UNIT);
		IClassFile classFile = (IClassFile) type.getAncestor(CLASS_FILE);
		if (unit != null || (classFile != null && classFile.getSourceRange() != null)) {
			location = locationType.toLocation(type);
		}
	}
	if (location == null && element instanceof IMember && ((IMember) element).getClassFile() != null) {
		location = JDTUtils.toLocation(((IMember) element).getClassFile());
	}
	return location;
}
 
Example #15
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: JarPackageWizard.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private void addJavaElement(List<Object> selectedElements, IJavaElement je) {
	if (je.getElementType() == IJavaElement.COMPILATION_UNIT)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.CLASS_FILE)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.JAVA_PROJECT)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
		if (!isInArchiveOrExternal(je))
			selectedElements.add(je);
	} else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
		if (!isInArchiveOrExternal(je))
			selectedElements.add(je);
	} else {
		IOpenable openable= je.getOpenable();
		if (openable instanceof ICompilationUnit)
			selectedElements.add(((ICompilationUnit) openable).getPrimary());
		else if (openable instanceof IClassFile && !isInArchiveOrExternal(je))
			selectedElements.add(openable);
	}
}
 
Example #16
Source Project: eclipse.jdt.ls   Author: eclipse   File: NavigateToDefinitionHandler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static Location fixLocation(IJavaElement element, Location location, IJavaProject javaProject) {
	if (location == null) {
		return null;
	}
	if (!javaProject.equals(element.getJavaProject()) && element.getJavaProject().getProject().getName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) {
		// see issue at: https://github.com/eclipse/eclipse.jdt.ls/issues/842 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573
		// for jdk classes, jdt will reuse the java model by altering project to share the model between projects
		// so that sometimes the project for `element` is default project and the project is different from the project for `unit`
		// this fix is to replace the project name with non-default ones since default project should be transparent to users.
		if (location.getUri().contains(ProjectsManager.DEFAULT_PROJECT_NAME)) {
			String patched = StringUtils.replaceOnce(location.getUri(), ProjectsManager.DEFAULT_PROJECT_NAME, javaProject.getProject().getName());
			try {
				IClassFile cf = (IClassFile) JavaCore.create(JDTUtils.toURI(patched).getQuery());
				if (cf != null && cf.exists()) {
					location.setUri(patched);
				}
			} catch (Exception ex) {

			}
		}
	}
	return location;
}
 
Example #17
Source Project: eclipse.jdt.ls   Author: eclipse   File: MavenBuildSupportTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDownloadSources() throws Exception {
	File file = DependencyUtil.getSources("org.apache.commons", "commons-lang3", "3.5");
	FileUtils.deleteDirectory(file.getParentFile());
	boolean mavenDownloadSources = preferences.isMavenDownloadSources();
	try {
		preferences.setMavenDownloadSources(false);
		IProject project = importMavenProject("salut");
		waitForBackgroundJobs();
		assertTrue(!file.exists());
		IJavaProject javaProject = JavaCore.create(project);
		IType type = javaProject.findType("org.apache.commons.lang3.StringUtils");
		IClassFile classFile = ((BinaryType) type).getClassFile();
		assertNull(classFile.getBuffer());
		String source = new SourceContentProvider().getSource(classFile, new NullProgressMonitor());
		if (source == null) {
			JobHelpers.waitForDownloadSourcesJobs(JobHelpers.MAX_TIME_MILLIS);
			source = new SourceContentProvider().getSource(classFile, new NullProgressMonitor());
		}
		assertNotNull("Couldn't find source for " + type.getFullyQualifiedName() + "(" + file.getAbsolutePath() + (file.exists() ? " exists)" : " is missing)"), source);
	} finally {
		preferences.setMavenDownloadSources(mavenDownloadSources);
	}
}
 
Example #18
private IMember getMember(IStructuredSelection selection) {
	if (selection.size() != 1)
		return null;
	Object o= selection.getFirstElement();
	if (o instanceof IMember) {
		IMember member= (IMember)o;
		try {
			if (member.getNameRange() == null)
				return null;
		} catch (JavaModelException ex) {
			return null;
		}

		IClassFile file= member.getClassFile();
		if (file != null) {
			try {
				if (file.getSourceRange() != null)
					return member;
			} catch (JavaModelException e) {
				return null;
			}
		}
		return member;
	}
	return null;
}
 
Example #19
Source Project: eclipse.jdt.ls   Author: eclipse   File: HoverHandlerTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testHoverThrowable() throws Exception {
	String uriString = ClassFileUtil.getURI(project, "java.lang.Exception");
	IClassFile classFile = JDTUtils.resolveClassFile(uriString);
	String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor);
	IDocument document = new Document(contents);
	IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false);
	int offset = region.getOffset();
	int line = document.getLineOfOffset(offset);
	int character = offset - document.getLineOffset(line);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString);
	Position position = new Position(line, character);
	TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position);
	Hover hover = handler.hover(params, monitor);
	assertNotNull(hover);
	assertTrue("Unexpected hover ", !hover.getContents().getLeft().isEmpty());
}
 
Example #20
private Object[] getPackageContents(IPackageFragment fragment) throws JavaModelException {
	ISourceReference[] sourceRefs;
	if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
		sourceRefs= fragment.getCompilationUnits();
	}
	else {
		IClassFile[] classFiles= fragment.getClassFiles();
		List<IClassFile> topLevelClassFile= new ArrayList<IClassFile>();
		for (int i= 0; i < classFiles.length; i++) {
			IType type= classFiles[i].getType();
			if (type != null && type.getDeclaringType() == null && !type.isAnonymous() && !type.isLocal())
				topLevelClassFile.add(classFiles[i]);
		}
		sourceRefs= topLevelClassFile.toArray(new ISourceReference[topLevelClassFile.size()]);
	}

	Object[] result= new Object[0];
	for (int i= 0; i < sourceRefs.length; i++)
		result= concatenate(result, removeImportAndPackageDeclarations(getChildren(sourceRefs[i])));
	return concatenate(result, fragment.getNonJavaResources());
}
 
Example #21
Source Project: jd-eclipse   Author: java-decompiler   File: JDClassFileEditor.java    License: GNU General Public License v3.0 6 votes vote down vote up
protected static void cleanupBuffer(IClassFile file) {
	IBuffer buffer = BufferManager.getDefaultBufferManager().getBuffer(file);

	if (buffer != null) {
		try {
			// Remove the buffer
			Method method = BufferManager.class.getDeclaredMethod("removeBuffer", new Class[] {IBuffer.class});
			method.setAccessible(true);
			method.invoke(BufferManager.getDefaultBufferManager(), new Object[] {buffer});				
		} catch (Exception e) {
			JavaDecompilerPlugin.getDefault().getLog().log(new Status(
				Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID, 
				0, e.getMessage(), e));
		}
	}
}
 
Example #22
protected IType transplantHandle(IJavaElement parent, IType element) {
	switch (parent.getElementType()) {
		case IJavaElement.COMPILATION_UNIT:
			return ((ICompilationUnit) parent).getType(element.getElementName());
		case IJavaElement.CLASS_FILE:
			return ((IClassFile) parent).getType();
		case IJavaElement.METHOD:
			return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.FIELD:
			return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.INITIALIZER:
			return ((IInitializer) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.TYPE:
			return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example #23
@Override
public void run(ITextSelection selection) {
	if (!ActionUtil.isProcessable(fEditor))
		return;
	ITypeRoot editorInput= SelectionConverter.getInput(fEditor);
	if (!ActionUtil.isEditable(getShell(), editorInput))
		return;
	if (editorInput instanceof ICompilationUnit)
		run(selection.getOffset(), selection.getLength(), (ICompilationUnit) editorInput);
	else if (editorInput instanceof IClassFile)
		run(selection.getOffset(), selection.getLength(), (IClassFile) editorInput);
}
 
Example #24
Source Project: java-debug   Author: microsoft   File: CompletionProposalRequestor.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructor.
 * @param typeRoot ITypeRoot
 * @param offset within the source file
 */
public CompletionProposalRequestor(ITypeRoot typeRoot, int offset) {
    this.typeRoot = typeRoot;
    response = new CompletionResponse();
    response.setOffset(offset);
    if (typeRoot instanceof IClassFile)  {
        isTestCodeExcluded = true;
    } else {
        isTestCodeExcluded = !isTestSource(typeRoot.getJavaProject(), typeRoot);
    }
    setRequireExtendedContext(true);
}
 
Example #25
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: SourceReferenceUtil.java    License: Eclipse Public License 1.0 5 votes vote down vote up
public static ICompilationUnit getCompilationUnit(ISourceReference o){
	Assert.isTrue(! (o instanceof IClassFile));

	if (o instanceof ICompilationUnit)
		return (ICompilationUnit)o;
	if (o instanceof IJavaElement)
		return (ICompilationUnit) ((IJavaElement)o).getAncestor(IJavaElement.COMPILATION_UNIT);
	return null;
}
 
Example #26
Source Project: java-debug   Author: microsoft   File: JdtSourceLookUpProvider.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String getSourceContents(String uri) {
    if (uri == null) {
        throw new IllegalArgumentException("uri is null");
    }
    IClassFile cf = resolveClassFile(uri);
    return getContents(cf);
}
 
Example #27
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: LevelTreeContentProvider.java    License: Eclipse Public License 1.0 5 votes vote down vote up
public Object getParent(Object child) {
	Object possibleParent= internalGetParent(child);
	if (possibleParent instanceof IJavaElement) {
		IJavaElement javaElement= (IJavaElement) possibleParent;
		for (int j= fCurrentLevel; j < MAX_LEVEL + 1; j++) {
			for (int i= 0; i < JAVA_ELEMENT_TYPES[j].length; i++) {
				if (javaElement.getElementType() == JAVA_ELEMENT_TYPES[j][i]) {
					return null;
				}
			}
		}
	} else if (possibleParent instanceof IResource) {
		IResource resource= (IResource) possibleParent;
		for (int j= fCurrentLevel; j < MAX_LEVEL + 1; j++) {
			for (int i= 0; i < RESOURCE_TYPES[j].length; i++) {
				if (resource.getType() == RESOURCE_TYPES[j][i]) {
					return null;
				}
			}
		}
	}
	if (fCurrentLevel != LEVEL_FILE && child instanceof IType) {
		IType type= (IType) child;
		if (possibleParent instanceof ICompilationUnit
				|| possibleParent instanceof IClassFile)
			possibleParent= type.getPackageFragment();
	}
	return possibleParent;
}
 
Example #28
Source Project: xtext-eclipse   Author: eclipse   File: JavaBreakPointProvider.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private String getHandleId(final IJavaStratumLineBreakpoint breakpoint) throws CoreException {
	IClassFile classFile = getClassFile(breakpoint);
	if (classFile != null)
		return classFile.getType().getHandleIdentifier();
	ILocationInEclipseResource javaLocation = getJavaLocation(breakpoint);
	if (javaLocation == null)
		return null;
	IStorage javaResource = javaLocation.getPlatformResource();
	if (!(javaResource instanceof IFile))
		return null;
	ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create((IFile) javaResource);
	IJavaElement element = compilationUnit.getElementAt(javaLocation.getTextRegion().getOffset());
	return element == null ? null : element.getHandleIdentifier();
}
 
Example #29
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: JavadocView.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Object computeInput(IWorkbenchPart part, ISelection selection, IJavaElement input, IProgressMonitor monitor) {
	if (getControl() == null || input == null)
		return null;

	String javadocHtml;

	switch (input.getElementType()) {
		case IJavaElement.COMPILATION_UNIT:
			try {
				IType[] types= ((ICompilationUnit) input).getTypes();
				if (types.length == 0 && JavaModelUtil.isPackageInfo((ICompilationUnit) input)) {
					javadocHtml= getJavadocHtml(new IJavaElement[] { input.getParent() }, part, selection, monitor);
				} else {
					javadocHtml= getJavadocHtml(types, part, selection, monitor);
				}
			} catch (JavaModelException ex) {
				javadocHtml= null;
			}
			break;
		case IJavaElement.CLASS_FILE:
			if (JavaModelUtil.PACKAGE_INFO_CLASS.equals(input.getElementName()))
				javadocHtml= getJavadocHtml(new IJavaElement[] { input.getParent() }, part, selection, monitor);
			else
				javadocHtml= getJavadocHtml(new IJavaElement[] { ((IClassFile) input).getType() }, part, selection, monitor);
			break;
		default:
			javadocHtml= getJavadocHtml(new IJavaElement[] { input }, part, selection, monitor);
	}

	return javadocHtml;
}
 
Example #30
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: JavaStatusContextViewer.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private IPackageFragmentRoot getPackageFragmentRoot(IClassFile file) {

		IJavaElement element= file.getParent();
		while (element != null && element.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT)
			element= element.getParent();

		return (IPackageFragmentRoot) element;
	}