Java Code Examples for org.eclipse.jdt.core.IType#getChildren()

The following examples show how to use org.eclipse.jdt.core.IType#getChildren() . 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
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ArrayList<IJavaElement> getSortedChildren(IType parent) throws JavaModelException {
	IJavaElement[] children= parent.getChildren();
	ArrayList<IJavaElement> sortedChildren= new ArrayList<IJavaElement>(Arrays.asList(children));
	Collections.sort(sortedChildren, new Comparator<IJavaElement>() {
		public int compare(IJavaElement e1, IJavaElement e2) {
			if (!(e1 instanceof ISourceReference))
				return 0;
			if (!(e2 instanceof ISourceReference))
				return 0;

			try {
				ISourceRange sr1= ((ISourceReference)e1).getSourceRange();
				ISourceRange sr2= ((ISourceReference)e2).getSourceRange();
				if (sr1 == null || sr2 == null)
					return 0;

				return sr1.getOffset() - sr2.getOffset();

			} catch (JavaModelException e) {
				return 0;
			}
		}
	});
	return sortedChildren;
}
 
Example 2
Source File: JavaBrowsingContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object[] getChildren(IType type) throws JavaModelException{
	IParent parent;
	if (type.isBinary())
		parent= type.getClassFile();
	else {
		parent= type.getCompilationUnit();
	}
	if (type.getDeclaringType() != null)
		return type.getChildren();

	// Add import declarations
	IJavaElement[] members= parent.getChildren();
	ArrayList<IJavaElement> tempResult= new ArrayList<IJavaElement>(members.length);
	for (int i= 0; i < members.length; i++)
		if ((members[i] instanceof IImportContainer))
			tempResult.add(members[i]);
	tempResult.addAll(Arrays.asList(type.getChildren()));
	return tempResult.toArray();
}
 
Example 3
Source File: CreateInitializerOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * By default the new initializer is positioned after the last existing initializer
 * declaration, or as the first member in the type if there are no
 * initializers.
 */
protected void initializeDefaultPosition() {
	IType parentElement = getType();
	try {
		IJavaElement[] elements = parentElement.getInitializers();
		if (elements != null && elements.length > 0) {
			this.numberOfInitializers = elements.length;
			createAfter(elements[elements.length - 1]);
		} else {
			elements = parentElement.getChildren();
			if (elements != null && elements.length > 0) {
				createBefore(elements[0]);
			}
		}
	} catch (JavaModelException e) {
		// type doesn't exist: ignore
	}
}
 
Example 4
Source File: CreateFieldOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * By default the new field is positioned after the last existing field
 * declaration, or as the first member in the type if there are no
 * field declarations.
 */
protected void initializeDefaultPosition() {
	IType parentElement = getType();
	try {
		IField[] fields = parentElement.getFields();
		if (fields != null && fields.length > 0) {
			final IField lastField = fields[fields.length - 1];
			if (parentElement.isEnum()) {
				IField field = lastField;
				if (!field.isEnumConstant()) {
					createAfter(lastField);
				}
			} else {
				createAfter(lastField);
			}
		} else {
			IJavaElement[] elements = parentElement.getChildren();
			if (elements != null && elements.length > 0) {
				createBefore(elements[0]);
			}
		}
	} catch (JavaModelException e) {
		// type doesn't exist: ignore
	}
}
 
Example 5
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private ArrayList<IJavaElement> getSortedChildren(IType parent) throws JavaModelException {
	IJavaElement[] children= parent.getChildren();
	ArrayList<IJavaElement> sortedChildren= new ArrayList<>(Arrays.asList(children));
	Collections.sort(sortedChildren, new Comparator<IJavaElement>() {
		@Override
		public int compare(IJavaElement e1, IJavaElement e2) {
			if (!(e1 instanceof ISourceReference)) {
				return 0;
			}
			if (!(e2 instanceof ISourceReference)) {
				return 0;
			}

			try {
				ISourceRange sr1= ((ISourceReference)e1).getSourceRange();
				ISourceRange sr2= ((ISourceReference)e2).getSourceRange();
				if (sr1 == null || sr2 == null) {
					return 0;
				}

				return sr1.getOffset() - sr2.getOffset();

			} catch (JavaModelException e) {
				return 0;
			}
		}
	});
	return sortedChildren;
}
 
Example 6
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void computeTypeRanges(List<FoldingRange> foldingRanges, IType unit, IScanner scanner) throws CoreException {
	ISourceRange typeRange = unit.getSourceRange();
	foldingRanges.add(new FoldingRange(scanner.getLineNumber(unit.getNameRange().getOffset()) - 1, scanner.getLineNumber(typeRange.getOffset() + typeRange.getLength()) - 1));
	IJavaElement[] children = unit.getChildren();
	for (IJavaElement c : children) {
		if (c instanceof IMethod) {
			computeMethodRanges(foldingRanges, (IMethod) c, scanner);
		} else if (c instanceof IType) {
			computeTypeRanges(foldingRanges, (IType) c, scanner);
		}
	}
}
 
Example 7
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object[] getElements(Object parent) {
	if (fTopLevelTypeOnly) {
		if (parent instanceof ITypeRoot) {
			try {
				IType type= ((ITypeRoot) parent).findPrimaryType();
				return type != null ? type.getChildren() : NO_CLASS;
			} catch (JavaModelException e) {
				JavaPlugin.log(e);
			}
		}
	}
	return getChildren(parent);
}
 
Example 8
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		final IJavaElement[] children= type.getChildren();
		for (int index= 0; index < children.length; index++) {
			final IMember child= (IMember) children[index];
			final int flags= child.getFlags();
			final boolean isPrivate= Flags.isPrivate(flags);
			final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
			final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
			if (child instanceof IType) {
				if (stub)
					appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
			} else if (child instanceof IField) {
				if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
					appendFieldDeclaration((IField) child);
			} else if (child instanceof IMethod) {
				final IMethod method= (IMethod) child;
				final String name= method.getElementName();
				if (method.getDeclaringType().isEnum()) {
					final int count= method.getNumberOfParameters();
					if (count == 0 && "values".equals(name)) //$NON-NLS-1$
						continue;
					if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					if (method.isConstructor())
						continue;
				}
				boolean skip= !stub || name.equals("<clinit>"); //$NON-NLS-1$
				if (method.isConstructor())
					skip= false;
				skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
				if (!skip)
					appendMethodDeclaration(method);
			}
			fBuffer.append("\n"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}