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

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

Introduction

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

Prototype

int INITIALIZER

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

Click Source Link

Document

Constant representing a stand-alone instance or class initializer.

Usage

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);/*from ww  w  .  j a  va 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:/*from  w ww . j  a  va 2s  .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:/*from   w ww. ja  v a2  s . co 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)
                    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 v a  2 s.co  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 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 .  c  o m
    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.BedrockUtil.java

License:Open Source License

private static void outputJavaElementImpl(IJavaElement elt, Set<String> files, boolean children,
        IvyXmlWriter xw) {/* ww w. j av  a2s. 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);
}

From source file:jp.littleforest.pathtools.handlers.CopyQualifiedNameHandler.java

License:Open Source License

protected String getQualifiedName(IJavaElement e) {
    String qn = "";
    switch (e.getElementType()) {
    case IJavaElement.ANNOTATION:
        break;/* w  w w.java  2  s . c  o  m*/
    case IJavaElement.CLASS_FILE:
        qn = getQualifiedClassName((IClassFile) e);
        break;
    case IJavaElement.COMPILATION_UNIT:
        qn = getQualifiedClassName((ICompilationUnit) e);
        break;
    case IJavaElement.FIELD:
        qn = getQualifiedFieldName((IField) e);
        break;
    case IJavaElement.IMPORT_CONTAINER:
        break;
    case IJavaElement.IMPORT_DECLARATION:
        break;
    case IJavaElement.INITIALIZER:
        break;
    case IJavaElement.JAVA_MODEL:
        break;
    case IJavaElement.JAVA_PROJECT:
        break;
    case IJavaElement.LOCAL_VARIABLE:
        break;
    case IJavaElement.METHOD:
        qn = getQualifiedMethodName((IMethod) e);
        break;
    case IJavaElement.PACKAGE_DECLARATION:
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        qn = getQualifiedPackageName(e);
        break;
    case IJavaElement.TYPE:
        qn = getQualifiedClassName((IType) e);
        break;
    case IJavaElement.TYPE_PARAMETER:
        break;
    default:
        break;
    }
    return qn;
}

From source file:net.sourceforge.c4jplugin.internal.ui.contracthierarchy.MethodsLabelProvider.java

License:Open Source License

private IType getDefiningType(Object element) throws JavaModelException {
    int kind = ((IJavaElement) element).getElementType();

    if (kind != IJavaElement.METHOD && kind != IJavaElement.FIELD && kind != IJavaElement.INITIALIZER) {
        return null;
    }/*w w  w. j  ava2  s  .c o m*/
    IType declaringType = ((IMember) element).getDeclaringType();
    if (kind != IJavaElement.METHOD) {
        return declaringType;
    }
    IContractHierarchy hierarchy = fHierarchy.getContractHierarchy();
    if (hierarchy == null) {
        return declaringType;
    }
    IMethod method = (IMethod) element;
    ConditionOverrideTester tester = new ConditionOverrideTester(declaringType, hierarchy);
    IMethod res = tester.findDeclaringMethod(method, true);
    if (res == null || method.equals(res)) {
        return declaringType;
    }
    return res.getDeclaringType();
}

From source file:net.sourceforge.metrics.ui.layeredpackagegraph.LayeredPackageTableView.java

License:Open Source License

/**
 * @param type/*  w  ww . ja va 2 s  . c o  m*/
 * @return true if acceptable type for metrics calculation
 */
private boolean canDoMetrics(IJavaElement element) {
    int type = element.getElementType();
    if (type == IJavaElement.CLASS_FILE) {
        return false;
    }
    if (type == IJavaElement.FIELD) {
        return false;
    }
    if (type == IJavaElement.IMPORT_CONTAINER) {
        return false;
    }
    if (type == IJavaElement.IMPORT_DECLARATION) {
        return false;
    }
    if (type == IJavaElement.INITIALIZER) {
        return false;
    }
    if (type == IJavaElement.PACKAGE_DECLARATION) {
        return false;
    }
    return true;
}

From source file:net.sourceforge.tagsea.java.JavaWaypointUtils.java

License:Open Source License

/**
 * Returns a string representation to the given java element for use within
 * Java Waypoints. Calling this method will return the string representation
 * that would be placed inside the "Java Element" attribute of a java
 * waypoint for the given element.//from  w ww. j  a v a2 s  .  c  o  m
 * @param element the element to get the string for.
 * @return the Java Waypoint representation of the element.
 */
public static String getStringForElement(IJavaElement element) {
    String result = "";
    switch (element.getElementType()) {
    case IJavaElement.TYPE:
        result = "T:" + getStringForType((IType) element);
        break;
    case IJavaElement.PACKAGE_DECLARATION:
        result = "P:" + getStringForPackage((IPackageDeclaration) element);
        break;
    case IJavaElement.METHOD:
        result = "M:" + getStringForMethod((IMethod) element);
        break;
    case IJavaElement.FIELD:
        result = "F:" + getStringForField((IField) element);
        break;
    case IJavaElement.LOCAL_VARIABLE:
        result = "V:" + getStringForVariable((ILocalVariable) element);
        break;
    case IJavaElement.INITIALIZER:
        result = "I:" + getStringForInitializer((IInitializer) element);
        break;
    default:
        return "";
    }
    return result;
}