Example usage for org.eclipse.jdt.core IJavaElement getParent

List of usage examples for org.eclipse.jdt.core IJavaElement getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getParent.

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

Usage

From source file:coloredide.utils.EditorUtility.java

License:Open Source License

private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
    while (element != null) {
        if (element instanceof ICompilationUnit) {
            ICompilationUnit unit = ((ICompilationUnit) element).getPrimary();
            IResource resource = unit.getResource();
            if (resource instanceof IFile)
                return new FileEditorInput((IFile) resource);
        }/*  w w w.ja v  a  2 s. c  o m*/

        element = element.getParent();
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.PostActivityCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity./* w w  w .j  a  v a 2s  . c o  m*/
 *
 */
@Override
public void processNewType(IType newType) {
    try {
        String methodContent = "    /** Called when the activity is first created. */\n" + "    @Override\n"
                + "    public void onCreate(Bundle savedInstanceState) {\n"
                + "        super.onCreate(savedInstanceState);\n" + "\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(SdkConstants.CLASS_BUNDLE, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.PostReceiverCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity.//w  w w . ja  va  2s  .  c o  m
 *
 */
@Override
public void processNewType(IType newType) {
    try {
        String methodContent = "    @Override\n"
                + "    public void onReceive(Context context, Intent intent) {\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(SdkConstants.CLASS_CONTEXT, null /* sibling */,
                    new NullProgressMonitor());
            compilationUnit.createImport(SdkConstants.CLASS_INTENT, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // looks like the class already existed (this happens when the user check to create
        // inherited abstract methods).
    }
}

From source file:com.android.ide.eclipse.adt.SourceRevealer.java

License:Open Source License

/**
 * Reveal the source for given fully qualified method name.<br>
 *
 * The method should take care of the following scenarios:<ol>
 * <li> A search, either by filename/line number, or for fqmn might provide only 1 result.
 *    In such a case, just open that result. Give preference to the file name/line # search
 *    since that is the most accurate (gets to the line number). </li>
 * <li> The search might not provide any results. e.g, the method name may be of the form
 *    "com.x.y$1.methodName". Searches for methods within anonymous classes will fail. In
 *    such a case, if the fileName:lineNumber argument is available, a search for that
 *    should be made instead. </li>
 * <li> The search might provide multiple results. In such a case, the fileName/lineNumber
 *    values should be utilized to narrow down the results.</li>
 * </ol>//from   ww  w  .ja va  2 s .  c  om
 *
 * @param fqmn fully qualified method name
 * @param fileName file name in which the method is present, null if not known
 * @param lineNumber line number in the file which should be given focus, -1 if not known.
 *        Line numbers begin at 1, not 0.
 * @param perspective perspective to switch to before the source is revealed, null to not
 *        switch perspectives
 */
@Override
public boolean revealMethod(String fqmn, String fileName, int lineNumber, String perspective) {
    // Search by filename:linenumber. If there is just one result for it, that would
    // be the correct match that is accurate to the line
    List<SearchMatch> fileMatches = Collections.emptyList();
    if (fileName != null && lineNumber >= 0) {
        fileMatches = searchForFile(fileName);
        if (fileMatches.size() == 1) {
            return revealLineMatch(fileMatches, fileName, lineNumber, perspective);
        }
    }

    List<SearchMatch> methodMatches = searchForMethod(fqmn);

    // if there is a unique method name match:
    //    1. if there are > 1 file name matches, try to see if they can be narrowed down
    //    2. if not, display the method match
    if (methodMatches.size() == 1) {
        if (fileMatches.size() > 0) {
            List<SearchMatch> filteredMatches = filterMatchByResource(fileMatches,
                    methodMatches.get(0).getResource());
            if (filteredMatches.size() == 1) {
                return revealLineMatch(filteredMatches, fileName, lineNumber, perspective);
            }
        } else if (fileName != null && lineNumber > 0) {
            // Couldn't find file match, but we have a filename and line number: attempt
            // to use this to pinpoint the location within the method
            IMethod method = (IMethod) methodMatches.get(0).getElement();
            IJavaElement element = method;
            while (element != null) {
                if (element instanceof ICompilationUnit) {
                    ICompilationUnit unit = ((ICompilationUnit) element).getPrimary();
                    IResource resource = unit.getResource();
                    if (resource instanceof IFile) {
                        IFile file = (IFile) resource;

                        try {
                            // See if the line number looks like it's inside the given method
                            ISourceRange sourceRange = method.getSourceRange();
                            IRegion region = AdtUtils.getRegionOfLine(file, lineNumber - 1);
                            // When fields are initialized with code, this logically belongs
                            // to the constructor, but the line numbers are outside of the
                            // constructor. In this case we'll trust the line number rather
                            // than the method range.
                            boolean isConstructor = fqmn.endsWith(CONSTRUCTOR_NAME);
                            if (isConstructor || region != null && region.getOffset() >= sourceRange.getOffset()
                                    && region.getOffset() < sourceRange.getOffset() + sourceRange.getLength()) {
                                // Yes: use the line number instead
                                if (perspective != null) {
                                    SourceRevealer.switchToPerspective(perspective);
                                }
                                return displayFile(file, lineNumber);
                            }

                        } catch (JavaModelException e) {
                            AdtPlugin.log(e, null);
                        }
                    }
                }
                element = element.getParent();
            }

        }

        return displayMethod((IMethod) methodMatches.get(0).getElement(), perspective);
    }

    // no matches for search by method, so search by filename
    if (methodMatches.size() == 0) {
        if (fileMatches.size() > 0) {
            return revealLineMatch(fileMatches, fileName, lineNumber, perspective);
        } else {
            // Last ditch effort: attempt to look up the class corresponding to the fqn
            // and jump to the line there
            if (fileMatches.isEmpty() && fqmn.indexOf('.') != -1) {
                String className = fqmn.substring(0, fqmn.lastIndexOf('.'));
                for (IJavaProject project : BaseProjectHelper.getAndroidProjects(null)) {
                    IType type;
                    try {
                        type = project.findType(className);
                        if (type != null && type.exists()) {
                            IResource resource = type.getResource();
                            if (resource instanceof IFile) {
                                if (perspective != null) {
                                    SourceRevealer.switchToPerspective(perspective);
                                }
                                return displayFile((IFile) resource, lineNumber);
                            }
                        }
                    } catch (JavaModelException e) {
                        AdtPlugin.log(e, null);
                    }
                }
            }

            return false;
        }
    }

    // multiple matches for search by method, narrow down by filename
    if (fileName != null) {
        return revealLineMatch(filterMatchByFileName(methodMatches, fileName), fileName, lineNumber,
                perspective);
    }

    // prompt the user
    SearchMatch match = getMatchToDisplay(methodMatches, fqmn);
    if (match == null) {
        return false;
    } else {
        return displayMethod((IMethod) match.getElement(), perspective);
    }
}

From source file:com.android.ide.eclipse.cheatsheets.actions.SetBreakpoint.java

License:Open Source License

/**
 * Prunes out all naming occurrences of anonymous inner types, since these types have no names
 * and cannot be derived visiting an AST (no positive type name matching while visiting ASTs)
 * @param type/*from w w w .  j a v a 2  s  .co m*/
 * @return the compiled type name from the given {@link IType} with all occurrences of anonymous inner types removed
 * @since 3.4
 */
private String pruneAnonymous(IType type) {
    StringBuffer buffer = new StringBuffer();
    IJavaElement parent = type;
    while (parent != null) {
        if (parent.getElementType() == IJavaElement.TYPE) {
            IType atype = (IType) parent;
            try {
                if (!atype.isAnonymous()) {
                    if (buffer.length() > 0) {
                        buffer.insert(0, '$');
                    }
                    buffer.insert(0, atype.getElementName());
                }
            } catch (JavaModelException jme) {
            }
        }
        parent = parent.getParent();
    }
    return buffer.toString();
}

From source file:com.android.ide.eclipse.editors.manifest.descriptors.PostActivityCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity./*from  w w  w.  j a  va2s .  c  o  m*/
 * 
 */
public void processNewType(IType newType) {
    try {
        String methodContent = "    /** Called when the activity is first created. */\n" + "    @Override\n"
                + "    public void onCreate(Bundle savedInstanceState) {\n"
                + "        super.onCreate(savedInstanceState);\n" + "\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(AndroidConstants.CLASS_BUNDLE, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.android.ide.eclipse.editors.manifest.descriptors.PostReceiverCreationAction.java

License:Open Source License

/**
 * Processes a newly created Activity./*from   w w w  .  jav  a2  s .co  m*/
 * 
 */
public void processNewType(IType newType) {
    try {
        String methodContent = "    @Override\n"
                + "    public void onReceive(Context context, Intent intent) {\n"
                + "        // TODO Auto-generated method stub\n" + "    }";
        newType.createMethod(methodContent, null /* sibling*/, false /* force */, new NullProgressMonitor());

        // we need to add the import for Bundle, so we need the compilation unit.
        // Since the type could be enclosed in other types, we loop till we find it.
        ICompilationUnit compilationUnit = null;
        IJavaElement element = newType;
        do {
            IJavaElement parentElement = element.getParent();
            if (parentElement != null) {
                if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    compilationUnit = (ICompilationUnit) parentElement;
                }

                element = parentElement;
            } else {
                break;
            }
        } while (compilationUnit == null);

        if (compilationUnit != null) {
            compilationUnit.createImport(AndroidConstants.CLASS_CONTEXT, null /* sibling */,
                    new NullProgressMonitor());
            compilationUnit.createImport(AndroidConstants.CLASS_INTENT, null /* sibling */,
                    new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // looks like the class already existed (this happens when the user check to create
        // inherited abstract methods).
    }
}

From source file:com.aqua.wikiwizard.ObjectsJavaModel.java

License:Apache License

/**
 * Returns the name of the object//from   ww  w  .  java  2 s .  c  om
 * @return
 *       String
 */
public String getObjectName() {
    String packageName = "";
    IJavaElement parent = javaObject.getParent();
    while (true) {
        if (parent == null) {
            break;
        }
        if (parent instanceof IPackageFragment) {
            packageName = ((IPackageFragment) parent).getElementName() + ".";
            break;
        }
        parent = parent.getParent();
    }
    return packageName + javaObject.getTypeQualifiedName();
}

From source file:com.aqua.wikiwizard.ObjectsJavaModel.java

License:Apache License

/**
 * Returns a name of the package this member belongs to
 * @param member/*from   ww  w.j  a v  a  2s  .c  o m*/
 *          Member for which the request is
 * @return
 *          String
 */
public static String getMemberPackageName(IMember member) {
    IJavaElement parent = member.getParent();
    while (parent != null) {
        if (parent instanceof IPackageFragment) {
            return parent.getElementName();
        }
        parent = parent.getParent();
    }
    return null;
}

From source file:com.aqua.wikiwizard.SystemObjectPage.java

License:Apache License

private static String getMemberName(IMember member) {
    String className = member.getElementName();
    String packageName = null;// w w w .j  a  v a  2  s  .com
    IJavaElement parent = member.getParent();
    while (parent != null) {
        if (parent instanceof IPackageFragment) {
            packageName = parent.getElementName();
            break;
        }
        parent = parent.getParent();
    }
    if (packageName == null) {
        return className;
    }

    // Build the name for easy sorting
    return className + " - " + packageName + "." + className;
}