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

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

Introduction

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

Prototype

int FIELD

To view the source code for org.eclipse.jdt.core IJavaElement FIELD.

Click Source Link

Document

Constant representing a field.

Usage

From source file:de.loskutov.bco.views.BytecodeOutlineView.java

License:Open Source License

/**
 * @param childEl//  w  ww . j a  va  2 s  . c  om
 * @return return null if type is not known or bytecode is not written or cannot be
 * found
 */
private DecompiledClass decompileBytecode(IJavaElement childEl) {
    // check here for inner classes too
    IJavaElement type = JdtUtils.getEnclosingType(childEl);
    if (type == null) {
        type = javaInput;
    }
    if (type == null) {
        return null;
    }
    InputStream is = JdtUtils.createInputStream(type);
    if (is == null) {
        return null;
    }
    DecompiledClass decompiledClass = null;
    int available = 0;
    try {
        ClassLoader cl = null;
        if (modes.get(BCOConstants.F_SHOW_ANALYZER)) {
            cl = JdtUtils.getClassLoader(type);
        }

        String fieldName = null;
        String methodName = null;
        /*
         * find out, which name we should use for selected element
         */
        if (modes.get(BCOConstants.F_SHOW_ONLY_SELECTED_ELEMENT) && childEl != null) {
            if (childEl.getElementType() == IJavaElement.FIELD) {
                fieldName = childEl.getElementName();
            } else {
                methodName = JdtUtils.getMethodSignature(childEl);
            }
        }
        available = is.available();
        decompiledClass = DecompilerHelper.getDecompiledClass(is,
                new DecompilerOptions(fieldName, methodName, modes, cl));
    } catch (Exception e) {
        try {
            // check if compilation unit is ok - then this is the user problem
            if (type.isStructureKnown()) {
                BytecodeOutlinePlugin.error("Cannot decompile: " + type, e);
            } else {
                BytecodeOutlinePlugin.log(e, IStatus.ERROR);
            }
        } catch (JavaModelException e1) {
            // this is compilation problem - don't show the message
            BytecodeOutlinePlugin.log(e1, IStatus.WARNING);
        }
    } catch (UnsupportedClassVersionError e) {
        BytecodeOutlinePlugin.error("Cannot decompile: " + type + ". Error was caused by attempt to "
                + "load a class compiled with the Java version which is not "
                + "supported by the current JVM. ", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            BytecodeOutlinePlugin.log(e, IStatus.WARNING);
        }
    }
    // remember class file size to show it later in UI
    if (decompiledClass != null) {
        decompiledClass.setClassSize(available);
    }
    return decompiledClass;
}

From source file:dynamicrefactoring.util.selection.SelectionInfo.java

License:Open Source License

/**
 * Obtiene el nombre completamente cualificado de los tipos de elemento 
 * descritos por la interfaz <code>IJavaElement</code>.
 * //from  www  .jav  a2s.  c om
 * @param type el valor codificado por IJavaElement para el tipo de elemento Java.
 * 
 * @return el nombre completamente cualificado del tipo equivalente.
 */
protected String decodeElementType(int type) {
    switch (type) {
    case IJavaElement.TYPE:
        return TYPE_NAME;
    case IJavaElement.FIELD:
        return FIELD_NAME;
    case IJavaElement.METHOD:
        return METHOD_NAME;
    case IJavaElement.TYPE_PARAMETER:
        return PARAMETER_NAME;
    case FORMAL_ARGUMENT:
        return FORMAL_ARGUMENT_NAME;
    default:
        return ""; //$NON-NLS-1$
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

void rename(String proj, String bid, String file, int start, int end, String name, String handle,
        String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual,
        boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw)
        throws BedrockException {
    FileData fd = file_map.get(file);/*  www.j a  v  a 2s  .c  o m*/
    ICompilationUnit icu;

    if (doedit) {
        // icu = fd.getDefaultUnit();
        icu = fd.getEditableUnit(bid);
    } else
        icu = fd.getEditableUnit(bid);

    IJavaElement[] elts;
    try {
        elts = icu.codeSelect(start, end - start);
    } catch (JavaModelException e) {
        throw new BedrockException("Bad location: " + e, e);
    }

    IJavaElement relt = null;
    for (IJavaElement ije : elts) {
        if (handle != null && !handle.equals(ije.getHandleIdentifier()))
            continue;
        if (name != null && !name.equals(ije.getElementName()))
            continue;
        relt = ije;
        break;
    }
    if (relt == null)
        throw new BedrockException("Item to rename not found");

    String id = null;
    switch (relt.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        id = IJavaRefactorings.RENAME_COMPILATION_UNIT;
        break;
    case IJavaElement.FIELD:
        IField ifld = (IField) relt;
        try {
            if (ifld.isEnumConstant())
                id = IJavaRefactorings.RENAME_ENUM_CONSTANT;
            else
                id = IJavaRefactorings.RENAME_FIELD;
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        id = IJavaRefactorings.RENAME_PACKAGE;
        break;
    case IJavaElement.LOCAL_VARIABLE:
        id = IJavaRefactorings.RENAME_LOCAL_VARIABLE;
        break;
    case IJavaElement.TYPE:
        id = IJavaRefactorings.RENAME_TYPE;
        break;
    case IJavaElement.TYPE_PARAMETER:
        id = IJavaRefactorings.RENAME_TYPE_PARAMETER;
        break;
    case IJavaElement.METHOD:
        id = IJavaRefactorings.RENAME_METHOD;
        break;
    case IJavaElement.ANNOTATION:
    case IJavaElement.CLASS_FILE:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.INITIALIZER:
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_DECLARATION:
        break;
    }
    if (id == null)
        throw new BedrockException("Invalid element type to rename");

    RenameJavaElementDescriptor renamer;

    RefactoringContribution rfc = RefactoringCore.getRefactoringContribution(id);
    if (rfc == null) {
        xw.begin("FAILURE");
        xw.field("TYPE", "SETUP");
        xw.textElement("ID", id);
        xw.end("FAILURE");
        renamer = new RenameJavaElementDescriptor(id);
    } else {
        renamer = (RenameJavaElementDescriptor) rfc.createDescriptor();
    }

    renamer.setJavaElement(relt);
    renamer.setKeepOriginal(keeporig);
    renamer.setNewName(newname);
    if (proj != null)
        renamer.setProject(proj);
    renamer.setRenameGetters(getters);
    renamer.setRenameSetters(setters);
    renamer.setUpdateHierarchy(dohier);
    renamer.setUpdateQualifiedNames(qual);
    renamer.setUpdateReferences(refs);
    renamer.setUpdateSimilarDeclarations(dosimilar);
    renamer.setUpdateTextualOccurrences(textocc);
    if (filespat != null)
        renamer.setFileNamePatterns(filespat);

    RefactoringStatus sts = renamer.validateDescriptor();
    if (!sts.isOK()) {
        xw.begin("FAILURE");
        xw.field("TYPE", "VALIDATE");
        BedrockUtil.outputStatus(sts, xw);
        xw.end("FAILURE");
        return;
    }

    try {
        Refactoring refactor = renamer.createRefactoring(sts);
        if (refactor == null) {
            xw.begin("FAILURE");
            xw.field("TYPE", "CREATE");
            xw.textElement("RENAMER", renamer.toString());
            xw.textElement("REFACTOR", renamer.toString());
            xw.textElement("STATUS", sts.toString());
            xw.end("FAILURE");
            return;
        }

        refactor.setValidationContext(null);

        // this seems to reset files from disk (mutliple times)
        sts = refactor.checkAllConditions(new NullProgressMonitor());
        if (!sts.isOK()) {
            xw.begin("FAILURE");
            xw.field("TYPE", "CHECK");
            BedrockUtil.outputStatus(sts, xw);
            xw.end("FAILURE");
            if (sts.hasFatalError())
                return;
        }
        BedrockPlugin.logD("RENAME: Refactoring checked");

        Change chng = refactor.createChange(new NullProgressMonitor());
        BedrockPlugin.logD("RENAME: Refactoring change created");

        if (doedit && chng != null) {
            chng.perform(new NullProgressMonitor());
        } else if (chng != null) {
            xw.begin("EDITS");
            BedrockUtil.outputChange(chng, xw);
            xw.end("EDITS");
        }
    } catch (CoreException e) {
        throw new BedrockException("Problem creating refactoring: " + e, e);
    }

    BedrockPlugin.logD("RENAME RESULT = " + xw.toString());
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

// This shouldn't be needed since edits in a window should also be made in the default
// buffer and hence in the actual compilation unit that would be reported

void getActiveElements(IJavaElement root, List<IJavaElement> rslt) {
    switch (root.getElementType()) {
    case IJavaElement.ANNOTATION:
    case IJavaElement.CLASS_FILE:
    case IJavaElement.FIELD:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.INITIALIZER:
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.METHOD:
    case IJavaElement.PACKAGE_DECLARATION:
    case IJavaElement.TYPE:
    case IJavaElement.TYPE_PARAMETER:
    default:/*  ww w.  j a v  a  2  s  .  c  o m*/
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) root;
        try {
            if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE && pfr.hasChildren()) {
                IJavaElement[] chld = pfr.getChildren();
                for (IJavaElement c : chld)
                    getActiveElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT:
        IParent par = (IParent) root;
        try {
            if (par.hasChildren()) {
                IJavaElement[] chld = par.getChildren();
                for (IJavaElement c : chld)
                    getActiveElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) root;
        IProject ip = cu.getJavaProject().getProject();
        File f = BedrockUtil.getFileForPath(cu.getPath(), ip);
        String fnm = f.getPath();
        FileData fd = file_map.get(fnm);
        if (fd == null)
            rslt.add(cu);
        else {
            rslt.add(fd.getSearchUnit());
        }
        break;
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

void getWorkingElements(IJavaElement root, List<ICompilationUnit> rslt) {
    switch (root.getElementType()) {
    case IJavaElement.ANNOTATION:
    case IJavaElement.CLASS_FILE:
    case IJavaElement.FIELD:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.INITIALIZER:
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.METHOD:
    case IJavaElement.PACKAGE_DECLARATION:
    case IJavaElement.TYPE:
    case IJavaElement.TYPE_PARAMETER:
    default:/*w w w.  ja  v  a  2 s  .  c om*/
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) root;
        try {
            if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE && pfr.hasChildren()) {
                IJavaElement[] chld = pfr.getChildren();
                for (IJavaElement c : chld)
                    getWorkingElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT:
        IParent par = (IParent) root;
        try {
            if (par.hasChildren()) {
                IJavaElement[] chld = par.getChildren();
                for (IJavaElement c : chld)
                    getWorkingElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) root;
        IProject ip = cu.getJavaProject().getProject();
        File f = BedrockUtil.getFileForPath(cu.getPath(), ip);
        String fnm = f.getPath();
        FileData fd = file_map.get(fnm);
        if (fd != null) {
            rslt.add(fd.getEditableUnit(null));
        }
        break;
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

void getCompilationElements(IJavaElement root, List<ICompilationUnit> rslt) {
    switch (root.getElementType()) {
    case IJavaElement.ANNOTATION:
    case IJavaElement.CLASS_FILE:
    case IJavaElement.FIELD:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.INITIALIZER:
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.METHOD:
    case IJavaElement.PACKAGE_DECLARATION:
    case IJavaElement.TYPE:
    case IJavaElement.TYPE_PARAMETER:
    default://from ww w  .j a va2  s.  c  o  m
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) root;
        try {
            if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE && pfr.hasChildren()) {
                IJavaElement[] chld = pfr.getChildren();
                for (IJavaElement c : chld)
                    getCompilationElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT:
        IParent par = (IParent) root;
        try {
            if (par.hasChildren()) {
                IJavaElement[] chld = par.getChildren();
                for (IJavaElement c : chld)
                    getCompilationElements(c, rslt);
            }
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) root;
        IProject ip = cu.getJavaProject().getProject();
        File f = BedrockUtil.getFileForPath(cu.getPath(), ip);
        String fnm = f.getPath();
        FileData fd = file_map.get(fnm);
        if (fd != null) {
            rslt.add(fd.getEditableUnit(null));
        } else
            rslt.add(cu);
        break;
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void handleFindAll(String proj, String file, int start, int end, boolean defs, boolean refs, boolean impls,
        boolean equiv, boolean exact, boolean system, boolean typeof, boolean ronly, boolean wonly,
        IvyXmlWriter xw) throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);
    IPath fp = new Path(file);

    int limit = 0;
    if (defs && refs)
        limit = IJavaSearchConstants.ALL_OCCURRENCES;
    else if (defs)
        limit = IJavaSearchConstants.DECLARATIONS;
    else if (refs)
        limit = IJavaSearchConstants.REFERENCES;
    int flimit = limit;
    if (ronly)/*www  .j  a v a2s  . c  om*/
        flimit = IJavaSearchConstants.READ_ACCESSES;
    else if (wonly)
        flimit = IJavaSearchConstants.WRITE_ACCESSES;

    int mrule = -1;
    if (equiv)
        mrule = SearchPattern.R_EQUIVALENT_MATCH;
    else if (exact)
        mrule = SearchPattern.R_EXACT_MATCH;

    SearchPattern pat = null;
    IJavaSearchScope scp = null;

    ICompilationUnit icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
    if (icu == null)
        throw new BedrockException("Compilation unit not found for " + fp);
    icu = getCompilationElement(icu);

    ICompilationUnit[] working = null;
    FindFilter filter = null;

    IJavaElement cls = null;
    char[] packagename = null;
    char[] typename = null;

    try {
        BedrockPlugin.logD("Getting search scopes");
        IJavaElement[] pelt;
        if (ijp != null)
            pelt = new IJavaElement[] { ijp };
        else
            pelt = getAllProjects();
        working = getWorkingElements(pelt);
        int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
        if (system)
            fg |= IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES;
        scp = SearchEngine.createJavaSearchScope(pelt, fg);

        BedrockPlugin.logD("Locating item to search for");
        IJavaElement[] elts = icu.codeSelect(start, end - start);

        if (typeof) {
            Set<IJavaElement> nelt = new LinkedHashSet<IJavaElement>();
            for (int i = 0; i < elts.length; ++i) {
                IType typ = null;
                String tnm = null;
                switch (elts[i].getElementType()) {
                case IJavaElement.FIELD:
                    tnm = ((IField) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.LOCAL_VARIABLE:
                    tnm = ((ILocalVariable) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.METHOD:
                    typ = ((IMethod) elts[i]).getDeclaringType();
                    break;
                default:
                    nelt.add(elts[i]);
                    break;
                }
                if (typ != null)
                    nelt.add(typ);
                else if (tnm != null && ijp != null) {
                    IJavaElement elt = ijp.findElement(tnm, null);
                    if (elt != null) {
                        nelt.add(elt);
                        typ = null;
                    } else {
                        while (tnm.startsWith("[")) {
                            String xtnm = tnm.substring(1);
                            if (xtnm == null)
                                break;
                            tnm = xtnm;
                        }
                        int ln = tnm.length();
                        String xtnm = tnm;
                        if (tnm.startsWith("L") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        } else if (tnm.startsWith("Q") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        }
                        if (xtnm != null)
                            tnm = xtnm;
                        int idx1 = tnm.lastIndexOf(".");
                        if (idx1 > 0) {
                            String pkgnm = tnm.substring(0, idx1);
                            xtnm = tnm.substring(idx1 + 1);
                            if (xtnm != null)
                                tnm = xtnm;
                            pkgnm = pkgnm.replace('$', '.');
                            packagename = pkgnm.toCharArray();
                        }
                        tnm = tnm.replace('$', '.');
                        typename = tnm.toCharArray();
                    }

                    if (typename != null) {
                        BedrockPlugin.logD("Handling type names");
                        FindTypeHandler fth = new FindTypeHandler(ijp);
                        SearchEngine se = new SearchEngine(working);
                        se.searchAllTypeNames(packagename, SearchPattern.R_EXACT_MATCH, typename,
                                SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scp, fth,
                                IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
                        nelt.addAll(fth.getFoundItems());
                    }
                }
            }
            IJavaElement[] nelts = new IJavaElement[nelt.size()];
            elts = nelt.toArray(nelts);
        }

        if (elts.length == 1 && !typeof) {
            xw.begin("SEARCHFOR");
            switch (elts[0].getElementType()) {
            case IJavaElement.FIELD:
                xw.field("TYPE", "Field");
                break;
            case IJavaElement.LOCAL_VARIABLE:
                xw.field("TYPE", "Local");
                break;
            case IJavaElement.METHOD:
                xw.field("TYPE", "Function");
                break;
            case IJavaElement.TYPE:
            case IJavaElement.TYPE_PARAMETER:
                xw.field("TYPE", "Class");
                cls = elts[0];
                break;
            }
            xw.text(elts[0].getElementName());
            xw.end("SEARCHFOR");
        }
        int etyp = -1;
        for (int i = 0; i < elts.length; ++i) {
            SearchPattern sp;
            int xlimit = limit;
            switch (elts[i].getElementType()) {
            case IJavaElement.FIELD:
            case IJavaElement.LOCAL_VARIABLE:
                xlimit = flimit;
                break;
            case IJavaElement.TYPE:
                if (impls)
                    xlimit = IJavaSearchConstants.IMPLEMENTORS;
                break;
            case IJavaElement.METHOD:
                if (impls)
                    xlimit |= IJavaSearchConstants.IGNORE_DECLARING_TYPE;
                break;
            }
            if (mrule < 0)
                sp = SearchPattern.createPattern(elts[i], xlimit);
            else
                sp = SearchPattern.createPattern(elts[i], xlimit, mrule);
            if (pat == null)
                pat = sp;
            else
                pat = SearchPattern.createOrPattern(pat, sp);
            if (etyp < 0)
                etyp = elts[i].getElementType();
        }

        if (etyp == IJavaElement.METHOD) {
            if (impls) {
                if (defs)
                    filter = new ImplementFilter(elts);
            } else if (defs && !refs)
                filter = new ClassFilter(elts);
        }

    } catch (JavaModelException e) {
        BedrockPlugin.logE("SEARCH PROBLEM: " + e);
        e.printStackTrace();
        throw new BedrockException("Can't find anything to search for", e);
    }

    if (pat == null) {
        BedrockPlugin.logW("Nothing to search for");
        return;
    }

    BedrockPlugin.logD("Setting up search");
    SearchEngine se = new SearchEngine(working);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, filter, false);

    BedrockPlugin.logD(
            "BEGIN SEARCH " + pat + " " + parts.length + " " + " " + scp + " :: COPIES: " + working.length);

    try {
        se.search(pat, parts, scp, fh, null);
    } catch (Throwable e) {
        throw new BedrockException("Problem doing find all search for " + pat + ": " + e, e);
    }

    if (cls != null && defs) { // need to add the actual class definition
        BedrockUtil.outputJavaElement(cls, false, xw);
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void getFullyQualifiedName(String proj, String file, int start, int end, IvyXmlWriter xw)
        throws BedrockException {
    String name = null;//from  w w  w . j a  va  2  s .  com
    String key = null;
    String sgn = null;
    String hdl = null;
    ICompilationUnit icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
    if (icu == null)
        throw new BedrockException("Compilation unit not found for " + file);
    icu = getCompilationElement(icu);

    try {
        IJavaElement[] elts = icu.codeSelect(start, end - start);
        for (int i = 0; i < elts.length && name == null; ++i) {
            switch (elts[i].getElementType()) {
            case IJavaElement.JAVA_PROJECT:
            case IJavaElement.JAVA_MODEL:
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            case IJavaElement.CLASS_FILE:
            case IJavaElement.PACKAGE_FRAGMENT:
            case IJavaElement.IMPORT_CONTAINER:
            case IJavaElement.IMPORT_DECLARATION:
            case IJavaElement.TYPE_PARAMETER:
            case IJavaElement.COMPILATION_UNIT:
            default:
                break;
            case IJavaElement.TYPE:
                IType typ = (IType) elts[i];
                name = typ.getFullyQualifiedName();
                key = typ.getKey();
                break;
            case IJavaElement.FIELD:
                IField fld = ((IField) elts[i]);
                name = fld.getDeclaringType().getFullyQualifiedName() + "." + fld.getElementName();
                key = fld.getKey();
                sgn = fld.getTypeSignature();
                break;
            case IJavaElement.METHOD:
                IMethod mthd = ((IMethod) elts[i]);
                name = mthd.getDeclaringType().getFullyQualifiedName() + "." + mthd.getElementName();
                key = mthd.getKey();
                sgn = mthd.getSignature();
                // TODO: might want to add signture here as well
                break;
            case IJavaElement.INITIALIZER:
                IInitializer init = ((IInitializer) elts[i]);
                name = init.getDeclaringType().getFullyQualifiedName() + ".<clinit>";
                break;
            case IJavaElement.PACKAGE_DECLARATION:
                name = ((IPackageDeclaration) elts[i]).getElementName();
                break;
            case IJavaElement.LOCAL_VARIABLE:
                ILocalVariable lcl = (ILocalVariable) elts[i];
                name = lcl.getHandleIdentifier();
                sgn = lcl.getTypeSignature();
                break;
            }
            hdl = elts[i].getHandleIdentifier();
        }
    } catch (CoreException e) {
        throw new BedrockException("Problem getting name", e);
    }

    if (name == null) {
        return;
        // throw new BedrockException("No identifier at location");
    }

    xw.begin("FULLYQUALIFIEDNAME");
    xw.field("NAME", name);
    if (key != null)
        xw.field("KEY", key);
    if (sgn != null)
        xw.field("TYPE", sgn);
    if (hdl != null)
        xw.field("HANDLE", hdl);
    xw.end();
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockRenamer.java

License:Open Source License

/********************************************************************************/

void rename(String proj, String bid, String file, int start, int end, String name, String handle,
        String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual,
        boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw)
        throws BedrockException {
    ICompilationUnit icu = our_plugin.getCompilationUnit(proj, file);

    IJavaElement[] elts;/*from w  w  w . j a v  a 2  s.co  m*/
    try {
        elts = icu.codeSelect(start, end - start);
    } catch (JavaModelException e) {
        throw new BedrockException("Bad location: " + e, e);
    }

    IJavaElement relt = null;
    for (IJavaElement ije : elts) {
        if (handle != null && !handle.equals(ije.getHandleIdentifier()))
            continue;
        if (name != null && !name.equals(ije.getElementName()))
            continue;
        relt = ije;
        break;
    }
    if (relt == null)
        throw new BedrockException("Item to rename not found");

    BedrockPlugin.logD("RENAME CHECK " + relt.getElementType() + " " + relt.getParent().getElementType());

    switch (relt.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        throw new BedrockException("Compilation unit renaming not supported yet");
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        throw new BedrockException("Package renaming not supported yet");
    case IJavaElement.FIELD:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        break;
    case IJavaElement.METHOD:
        IMethod mthd = (IMethod) relt;
        try {
            if (mthd.isConstructor())
                throw new BedrockException("Constructor renaming not supported yet");
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.TYPE:
        IJavaElement pelt = relt.getParent();
        if (pelt.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ITypeRoot xcu = (ITypeRoot) pelt;
            if (relt == xcu.findPrimaryType()) {
                throw new BedrockException("Compilation unit renaming based on type not supported yet");
            }
        }
        break;
    default:
        throw new BedrockException("Invalid element type to rename");
    }

    SearchPattern sp = SearchPattern.createPattern(relt, IJavaSearchConstants.ALL_OCCURRENCES,
            SearchPattern.R_EXACT_MATCH);

    List<ICompilationUnit> worku = new ArrayList<ICompilationUnit>();
    for (IJavaElement je : BedrockJava.getAllProjects()) {
        our_plugin.getWorkingElements(je, worku);
    }
    ICompilationUnit[] work = new ICompilationUnit[worku.size()];
    work = worku.toArray(work);

    int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    IJavaSearchScope scp = SearchEngine.createJavaSearchScope(work, fg);

    SearchEngine se = new SearchEngine(work);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, null);

    try {
        se.search(sp, parts, scp, fh, null);
    } catch (CoreException e) {
        throw new BedrockException("Problem doing find all search: " + e, e);
    }

    BedrockPlugin.logD("RENAME RESULT = " + xw.toString());
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java

License:Open Source License

private static void outputJavaElementImpl(IJavaElement elt, Set<String> files, boolean children,
        IvyXmlWriter xw) {/*from   w  ww.ja v  a  2  s.  co m*/
    if (elt == null)
        return;

    String close = null;

    switch (elt.getElementType()) {
    case IJavaElement.CLASS_FILE:
        return;
    case IJavaElement.PACKAGE_FRAGMENT:
        IOpenable opn = (IOpenable) elt;
        if (!opn.isOpen()) {
            try {
                opn.open(null);
            } catch (JavaModelException e) {
                BedrockPlugin.logE("Package framgent " + elt.getElementName() + " not open");
                return;
            }
        }
        try {
            outputNameDetails((IPackageFragment) elt, xw);
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) elt;
        try {
            if (!pfr.isOpen() && pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                pfr.open(null);
            }
        } catch (JavaModelException e) {
            return;
        }
        outputNameDetails(pfr, xw);
        break;
    case IJavaElement.JAVA_PROJECT:
        IJavaProject ijp = (IJavaProject) elt;
        outputNameDetails(ijp, xw);
        break;
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
    case IJavaElement.TYPE_PARAMETER:
    default:
        break;
    case IJavaElement.COMPILATION_UNIT:
        IProject ip = elt.getJavaProject().getProject();
        File f = getFileForPath(elt.getPath(), ip);
        if (files != null && !files.contains(f.getPath()) && !files.contains(f.getAbsolutePath())) {
            return;
        }
        xw.begin("FILE");
        xw.textElement("PATH", f.getAbsolutePath());
        String root = getRootForPath(elt.getPath(), ip);
        if (root != null)
            xw.textElement("PATHROOT", root);
        close = "FILE";
        break;
    case IJavaElement.TYPE:
        try {
            outputNameDetails((IType) elt, xw);
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.FIELD:
        try {
            outputNameDetails((IField) elt, xw);
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.METHOD:
        try {
            outputNameDetails((IMethod) elt, xw);
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.INITIALIZER:
        outputNameDetails((IInitializer) elt, xw);
        break;
    case IJavaElement.PACKAGE_DECLARATION:
        outputNameDetails((IPackageDeclaration) elt, xw);
        break;
    case IJavaElement.LOCAL_VARIABLE:
        outputNameDetails((ILocalVariable) elt, xw);
        break;
    }

    if (children && elt instanceof IParent) {
        try {
            for (IJavaElement c : ((IParent) elt).getChildren()) {
                outputJavaElementImpl(c, files, children, xw);
            }
        } catch (JavaModelException e) {
        }
    }

    if (close != null)
        xw.end(close);
}