public CodeInsertionDialog(Shell shell, IType type) throws JavaModelException { super(shell); insertPositions = new ArrayList<IJavaElement>(); fLabels = new ArrayList<String>(); System.out.println(type.getElementName() + type.getElementType()); IJavaElement[] members = type.getChildren(); insertPositions.add(members.length > 0 ? members[0] : null); // first insertPositions.add(null); // last fLabels.add(CodeInsertionDialog.firstElement); fLabels.add(CodeInsertionDialog.secondElement); for (int i = 0; i < members.length; i++) { IJavaElement curr = members[i]; String methodLabel = JavaElementLabels.getElementLabel(curr, JavaElementLabels.M_PARAMETER_TYPES); // System.out.println(MessageFormat.format(afterElement, methodLabel)); fLabels.add(MessageFormat.format(afterElement, methodLabel)); insertPositions.add(findSibling(curr, members)); } insertPositions.add(null); // null indicate we want to insert into the last position }
/** * This method asks the specified <code>IType</code> if it has a main method, if not it recurses * through all of its children When recursing we only care about child <code>IType</code>s that * are static. * * @param type the <code>IType</code> to inspect for a main method * @return true if a main method was found in specified <code>IType</code>, false otherwise * @throws CoreException * @since 3.3 */ private boolean hasMainInChildren(IType type) throws CoreException { if (type.isClass() & Flags.isStatic(type.getFlags())) { if (hasMainMethod(type)) { return true; } else { IJavaElement[] children = type.getChildren(); for (int i = 0; i < children.length; i++) { if (children[i].getElementType() == IJavaElement.TYPE) { return hasMainInChildren((IType) children[i]); } } } } return false; }
/** * Determines is the java element contains a main method. * * @param element the element to check for the method * @return true if the method is found in the element, false otherwise */ private boolean hasMain(IJavaElement element) { try { IType type = getType(element); if (type != null && type.exists()) { if (hasMainMethod(type)) { return true; } // failed to find in public type, check static inner types IJavaElement[] children = type.getChildren(); for (int i = 0; i < children.length; i++) { if (hasMainInChildren(getType(children[i]))) { return true; } } } } catch (JavaModelException e) { } catch (CoreException ce) { } return false; }