use of org.eclipse.jdt.core.ITypeRoot in project che by eclipse.
the class JavadocContentAccess2 method getHTMLContent.
/**
* Returns the Javadoc for a package which could be present in package.html, package-info.java
* or from an attached Javadoc.
*
* @param packageFragment the package which is requesting for the document
* @param urlPrefix
* @return the document content in HTML format or <code>null</code> if there is no associated
* Javadoc
* @throws CoreException if the Java element does not exists or an exception occurs while
* accessing the file containing the package Javadoc
* @since 3.9
*/
public static String getHTMLContent(IPackageFragment packageFragment, String urlPrefix) throws CoreException {
IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
//1==> Handle the case when the documentation is present in package-info.java or package-info.class file
ITypeRoot packageInfo;
boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY;
if (isBinary) {
packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
} else {
packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
}
if (packageInfo != null && packageInfo.exists()) {
String cuSource = packageInfo.getSource();
//the source can be null for some of the class files
if (cuSource != null) {
Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource);
if (packageJavadocNode != null) {
IJavaElement element;
if (isBinary) {
element = ((IClassFile) packageInfo).getType();
} else {
// parent is the IPackageFragment
element = packageInfo.getParent();
}
return new JavadocContentAccess2(element, packageJavadocNode, cuSource, urlPrefix).toHTML();
}
}
} else // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or
// coming from a jar file
{
Object[] nonJavaResources = packageFragment.getNonJavaResources();
// 2.1 ==>If the package.html file is present in the source or directly in the binary jar
for (Object nonJavaResource : nonJavaResources) {
if (nonJavaResource instanceof IFile) {
IFile iFile = (IFile) nonJavaResource;
if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) {
return getIFileContent(iFile);
}
}
}
// 2.2==>The file is present in a binary container
if (isBinary) {
for (Object nonJavaResource : nonJavaResources) {
// The content is from an external binary class folder
if (nonJavaResource instanceof IJarEntryResource) {
IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
String encoding = getSourceAttachmentEncoding(root);
if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName()) && jarEntryResource.isFile()) {
return getHTMLContent(jarEntryResource, encoding);
}
}
}
//2.3 ==>The file is present in the source attachment path.
String contents = getHTMLContentFromAttachedSource(root, packageFragment, urlPrefix);
if (contents != null)
return contents;
}
}
//3==> Handle the case when the documentation is coming from the attached Javadoc
if ((root.isArchive() || root.isExternal())) {
return packageFragment.getAttachedJavadoc(null);
}
return null;
}
use of org.eclipse.jdt.core.ITypeRoot in project flux by eclipse.
the class ContextSensitiveImportRewriteContext method findInContext.
@Override
public int findInContext(String qualifier, String name, int kind) {
IBinding[] declarationsInScope = getDeclarationsInScope();
for (int i = 0; i < declarationsInScope.length; i++) {
if (declarationsInScope[i] instanceof ITypeBinding) {
ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
if (isSameType(typeBinding, qualifier, name)) {
return RES_NAME_FOUND;
} else if (isConflicting(typeBinding, name)) {
return RES_NAME_CONFLICT;
}
} else if (declarationsInScope[i] != null) {
if (isConflicting(declarationsInScope[i], name)) {
return RES_NAME_CONFLICT;
}
}
}
Name[] names = getImportedNames();
for (int i = 0; i < names.length; i++) {
IBinding binding = names[i].resolveBinding();
if (binding instanceof ITypeBinding && !binding.isRecovered()) {
ITypeBinding typeBinding = (ITypeBinding) binding;
if (isConflictingType(typeBinding, qualifier, name)) {
return RES_NAME_CONFLICT;
}
}
}
List<AbstractTypeDeclaration> list = fCompilationUnit.types();
for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
AbstractTypeDeclaration type = iter.next();
ITypeBinding binding = type.resolveBinding();
if (binding != null) {
if (isSameType(binding, qualifier, name)) {
return RES_NAME_FOUND;
} else {
ITypeBinding decl = containingDeclaration(binding, qualifier, name);
while (decl != null && !decl.equals(binding)) {
int modifiers = decl.getModifiers();
if (Modifier.isPrivate(modifiers))
return RES_NAME_CONFLICT;
decl = decl.getDeclaringClass();
}
}
}
}
String[] addedImports = fImportRewrite.getAddedImports();
String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
for (int i = 0; i < addedImports.length; i++) {
String addedImport = addedImports[i];
if (qualifiedName.equals(addedImport)) {
return RES_NAME_FOUND;
} else {
if (isConflicting(name, addedImport))
return RES_NAME_CONFLICT;
}
}
if (qualifier.equals("java.lang")) {
//$NON-NLS-1$
//No explicit import statement required
ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
if (typeRoot != null) {
IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
try {
ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
for (int i = 0; i < compilationUnits.length; i++) {
ICompilationUnit cu = compilationUnits[i];
IType[] allTypes = cu.getAllTypes();
for (int j = 0; j < allTypes.length; j++) {
IType type = allTypes[j];
String packageTypeName = type.getFullyQualifiedName();
if (isConflicting(name, packageTypeName))
return RES_NAME_CONFLICT;
}
}
} catch (JavaModelException e) {
}
}
}
return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
use of org.eclipse.jdt.core.ITypeRoot in project tdi-studio-se by Talend.
the class OpenDeclarationAction method getMethodNameRange.
/**
* Gets the source range.
*
* @param editorInput The editor input
* @return The source range
* @throws JavaModelException
*/
private ISourceRange getMethodNameRange(IEditorInput editorInput) throws JavaModelException {
ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput);
LinkedList<ISourceRange> sourceRanges = new LinkedList<ISourceRange>();
if (typeRoot instanceof IClassFile) {
// class file
IType type = ((IClassFile) typeRoot).getType();
getMethodNameRange(type.getChildren(), 0, sourceRanges);
} else if (typeRoot instanceof ICompilationUnit) {
// java file
ICompilationUnit unit = (ICompilationUnit) typeRoot;
IType[] allTypes = unit.getAllTypes();
for (IType type : allTypes) {
getMethodNameRange(type.getChildren(), 0, sourceRanges);
}
} else {
return null;
}
if (sourceRanges.isEmpty()) {
return null;
}
return sourceRanges.getFirst();
}
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;
}
Aggregations