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:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.ClassFileExtractor.java

License:Open Source License

public void extractClassFile(IClassFile classFile) {
    // Verify that it's a top-level type, or a subtype of a top-level type
    //    try {/*  w  ww.  ja  va2s . c  o m*/
    //      IType declaring = classFile.getType();
    //      while (declaring != null) {
    //        if (declaring.isLocal() || declaring.isAnonymous()) {
    //          return;
    //        }
    //        declaring = declaring.getDeclaringType();
    //      }
    //    } catch (JavaModelException e) {
    //      logger.log(Level.SEVERE, "Error in extracting class file", e);
    //      return;
    //    }

    IJavaElement parent = classFile.getParent();
    while (true) {
        if (parent == null) {
            logger.log(Level.SEVERE, "Unable to find package for: " + classFile.getElementName());
            break;
        } else if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
            // Write the class file
            name = classFile.getElementName();
            path = parent.getElementName() + "." + name;
            fileWriter.writeFile(File.CLASS, name, null, path);

            try {
                if (classFile.getType().isAnonymous()) {
                    String fqn = classFile.getType().getFullyQualifiedName();
                    String containingFqn = fqn.substring(0, fqn.lastIndexOf('$'));
                    relationWriter.writeRelation(Relation.CONTAINS, containingFqn,
                            classFile.getType().getFullyQualifiedName(),
                            new Location(classFile.getType().getFullyQualifiedName(), path, null, null));
                } else {
                    relationWriter.writeRelation(Relation.CONTAINS, parent.getElementName(),
                            classFile.getType().getFullyQualifiedName(),
                            new Location(classFile.getType().getFullyQualifiedName(), path, null, null));
                    entityWriter.writeEntity(Entity.PACKAGE, parent.getElementName(), 0, null, null);
                }
            } catch (JavaModelException e) {
                logger.log(Level.SEVERE, "Error in extracting class file", e);
            }
            break;
        } else {
            logger.log(Level.SEVERE, classFile.getType().getFullyQualifiedName() + " should be top-level!");
            parent = parent.getParent();
        }
    }

    extractIType(classFile.getType());
    name = null;
}

From source file:edu.wpi.cs.jburge.SEURAT.actions.AssociateArtifactAction.java

License:Open Source License

/** Executes the association. We get our selected rationale item and 
 * associated it with the java element selected.
 * /*from w w  w.j a v  a2  s .co m*/
 */
public void run() {
    //Get our selected items. Send a message to the user to confirm that
    //this is the correct association.
    if (navigatorSelection != null) {
        ISelection selection = viewer.getSelection();
        obj = ((IStructuredSelection) selection).getFirstElement();
        if (obj instanceof TreeParent) {
            String assQ = "Associate '" + ((TreeParent) obj).getName() + "' with "
                    + navigatorSelection.getElementName() + "?";

            boolean selOk = showQuestion(assQ);
            //            showMessage(navigatorSelection.getHandleIdentifier());
            //            System.out.println(navigatorSelection.getHandleIdentifier());
            if (selOk) {
                cstart = 0;

                ourRes = null;
                try {

                    if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                        ourRes = navigatorSelection.getCorrespondingResource();
                    } else {
                        ourRes = navigatorSelection.getUnderlyingResource();
                        if (ourRes != null) {
                            //                        ***                        System.out.println("this one wasn't null?");
                        }
                        //find the enclosing class file
                        IJavaElement nextE = navigatorSelection.getParent();
                        while ((nextE != null) && (nextE.getElementType() != IJavaElement.COMPILATION_UNIT)) {
                            //                        ***                        System.out.println("Name = " + nextE.getElementName());
                            //                        ***                     System.out.println("Type = " + nextE.getElementType());
                            nextE = nextE.getParent();
                        }
                        try {
                            //                        ***                     System.out.println("getting our resource");
                            //                  ourRes = nextE.getUnderlyingResource();
                            ourRes = nextE.getCorrespondingResource();
                            ourRes = nextE.getResource();
                        } catch (JavaModelException ex) {
                            System.out.println("exception getting resource?");
                        }
                        //                     ***                     System.out.println("Final name = " + nextE.getElementName());
                        //                     ***                     System.out.println("Final type = " + nextE.getElementType());
                        if (ourRes == null) {
                            //see if we can get the element from the working copy
                            IJavaElement original = nextE.getPrimaryElement();
                            //                        Get working copy has been deprecated
                            //                        IJavaElement original = ((ICompilationUnit) ((ICompilationUnit) nextE).getWorkingCopy()).getOriginalElement();
                            ourRes = original.getCorrespondingResource();

                        }
                    }
                    //                  ourRes = navigatorSelection.getUnderlyingResource();
                    if (ourRes == null) {
                        //                     ***                     System.out.println("why would our resource be null?");
                    }
                    //                  ***                  System.out.println("FullPath = " + ourRes.getFullPath().toString());
                    //                  ***                  System.out.println("now checking file extension?");
                    if (ourRes.getFullPath().getFileExtension().compareTo("java") == 0) {
                        //                     ***                     System.out.println("creating our file?");
                        IJavaElement myJavaElement = JavaCore.create((IFile) ourRes);
                        //                     ***                  System.out.println("created an element?");
                        if (myJavaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                            //                        ***                     System.out.println("Compilation Unit");
                            ICompilationUnit myCompilationUnit = (ICompilationUnit) myJavaElement;

                            IType[] myTypes = myCompilationUnit.getTypes();
                            boolean found = false;
                            int i = 0;
                            while ((!found) && i < myTypes.length) {
                                //selected item was the class itself
                                if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                                    //                              ***                            System.out.println("found the class");
                                    if (myTypes[i].isClass()) {
                                        found = true;
                                        cstart = myTypes[i].getNameRange().getOffset();
                                    }
                                } else if (navigatorSelection.getElementType() == IJavaElement.FIELD) {
                                    //                              ***                            System.out.println("looking for types");
                                    IField[] myFields = myTypes[i].getFields();
                                    for (int j = 0; j < myFields.length; j++) {
                                        if (myFields[j].getElementName()
                                                .compareTo(navigatorSelection.getElementName()) == 0) {
                                            //                                    ***                               System.out.println("found a type");
                                            found = true;
                                            cstart = myFields[j].getNameRange().getOffset();
                                        }
                                    }

                                } else if (navigatorSelection.getElementType() == IJavaElement.METHOD) {
                                    //                              ***                            System.out.println("looking for a method");
                                    IMethod[] myMethods = myTypes[i].getMethods();
                                    for (int j = 0; j < myMethods.length; j++) {
                                        if (myMethods[j].getElementName()
                                                .compareTo(navigatorSelection.getElementName()) == 0) {
                                            //                                    ***                               System.out.println("found a method");
                                            found = true;
                                            cstart = myMethods[j].getNameRange().getOffset();
                                        }
                                    }
                                }
                                //don't forget to increment!
                                i++;
                            } //end while
                        } else {
                            //                        ***                      System.out.println("not a compilation unit?");
                            System.out.println(myJavaElement.getElementType());
                        }
                        //ok... now what type is our selected item? 
                        System.out.println("got the resource?");
                        if (ourRes == null) {
                            System.out.println("null resource???");
                        }
                    } else {
                        System.out.println("not a java file?");
                    }
                    //                  from the newsgroup - in a runnable?                  
                    ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
                        public void run(IProgressMonitor monitor) {
                            try {
                                //                           ***                  System.out.println("line number = " + new Integer(lineNumber).toString());
                                IMarker ratM = ourRes.createMarker("SEURAT.ratmarker");
                                String dbname = RationaleDB.getDbName();
                                String markD = "Alt: '" + ((TreeParent) obj).getName() + "'   Rationale DB: '"
                                        + dbname + "'";
                                ratM.setAttribute(IMarker.MESSAGE, markD);
                                ratM.setAttribute(IMarker.CHAR_START, cstart);
                                ratM.setAttribute(IMarker.CHAR_END, cstart + 1);
                                ratM.setAttribute(IMarker.SEVERITY, 0);
                                String artName = navigatorSelection.getElementName();
                                ratM.setAttribute("alternative", ((TreeParent) obj).getName());
                                SEURATResourcePropertiesManager.addPersistentProperty(ourRes, "Rat", "true");
                                RationaleDB d = RationaleDB.getHandle();
                                d.associateAlternative(((TreeParent) obj).getName(),
                                        navigatorSelection.getHandleIdentifier(), ourRes.getName(), artName,
                                        markD);

                            } catch (CoreException e) {
                                e.printStackTrace();
                            }
                        }
                    }, null);
                    //                  ***                  System.out.println("adding persistent property");

                    SEURATDecoratorManager.addSuccessResources(ourRes);
                    //                  ***                  System.out.println("added our property");  
                    // Refresh the label decorations... Change it to DemoDecoratorWithImageCaching if image caching should be used
                    //                  ((TreeParent) obj).setStatus(RationaleErrorLevel.ERROR);
                    viewer.update((TreeParent) obj, null);
                    SEURATLightWeightDecorator.getRatDecorator().refresh();
                    //                  ***                  System.out.println("refresh");

                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.out.println("an exception occured in AssociateArtifactAction");
                }

            } else
                System.out.println("selection rejected");
        }
    } else {
        System.out.println("No java element selected...");
    }
}

From source file:edu.wpi.cs.jburge.SEURAT.actions.RemoveRationaleAssociation.java

License:Open Source License

/**
 * This is when the action really runs//from  w  w w  . j a  va  2s . c o m
 * @param action - not used
 */
public void run(IAction action) {
    IJavaElement navigatorSelection;
    //      ***      System.out.println("removing association");
    if (selection.getFirstElement() instanceof IJavaElement) {
        //         ***         System.out.println("we are a java element");
        navigatorSelection = (IJavaElement) selection.getFirstElement();
        ourRes = null;

        boolean proceed = showQuestion("Do you want to delete all associations to this file?");
        if (!proceed) {
            return;
        }

        //find the associated resource that goes with the element
        try {
            if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                ourRes = navigatorSelection.getCorrespondingResource();

            } else {
                ourRes = navigatorSelection.getUnderlyingResource();
                if (ourRes != null) {
                    //                  ***                  System.out.println("this one wasn't null?");
                }
                //find the enclosing class file
                IJavaElement nextE = navigatorSelection.getParent();
                while ((nextE != null) && (nextE.getElementType() != IJavaElement.COMPILATION_UNIT)) {
                    //                  ***                  System.out.println("Name = " + nextE.getElementName());
                    //                  ***                  System.out.println("Type = " + nextE.getElementType());
                    nextE = nextE.getParent();
                }
                try {
                    //                  ***                  System.out.println("getting our resource");
                    //                  ourRes = nextE.getUnderlyingResource();
                    ourRes = nextE.getCorrespondingResource();
                    ourRes = nextE.getResource();
                } catch (JavaModelException ex) {
                    //                  ***                  System.out.println("exception getting resource?");
                }
                System.out.println("Final name = " + nextE.getElementName());
                System.out.println("Final type = " + nextE.getElementType());
                if (ourRes == null) {
                    //                  ***                  System.out.println("see if there's a working copy");
                    IJavaElement original = nextE.getPrimaryElement();
                    //see if we can get the element from the working copy
                    //                  IJavaElement original = ((IWorkingCopy) ((ICompilationUnit) nextE).getWorkingCopy()).getOriginalElement();
                    ourRes = original.getCorrespondingResource();
                }
            }
            //                  ourRes = navigatorSelection.getUnderlyingResource();
            if (ourRes == null) {
                //               ***               System.out.println("why would our resource be null?");
            }
            //            ***            System.out.println("FullPath = " + ourRes.getFullPath().toString());
            //            ***            System.out.println("now checking file extension?");
            if (ourRes.getFullPath().getFileExtension().compareTo("java") == 0) {
                //               ***               System.out.println("creating our file?");
                IJavaElement myJavaElement = JavaCore.create((IFile) ourRes);
                //               ***                  System.out.println("created an element?");
                if (myJavaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    //                  ***                  System.out.println("Compilation Unit");
                    ICompilationUnit myCompilationUnit = (ICompilationUnit) myJavaElement;

                    IType[] myTypes = myCompilationUnit.getTypes();
                    boolean found = false;
                    int i = 0;
                    while ((!found) && i < myTypes.length) {
                        //selected item was the class itself
                        if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                            //                        ***                     System.out.println("found the class");
                            if (myTypes[i].isClass()) {
                                found = true;
                            }
                        } else if (navigatorSelection.getElementType() == IJavaElement.FIELD) {
                            //                        ***                     System.out.println("looking for types");
                            IField[] myFields = myTypes[i].getFields();
                            for (int j = 0; j < myFields.length; j++) {
                                if (myFields[j].getElementName()
                                        .compareTo(navigatorSelection.getElementName()) == 0) {
                                    //                              ***                           System.out.println("found a type");
                                    found = true;
                                }
                            }

                        } else if (navigatorSelection.getElementType() == IJavaElement.METHOD) {
                            //                        ***                     System.out.println("looking for a method");
                            IMethod[] myMethods = myTypes[i].getMethods();
                            for (int j = 0; j < myMethods.length; j++) {
                                if (myMethods[j].getElementName()
                                        .compareTo(navigatorSelection.getElementName()) == 0) {
                                    //                              ***                           System.out.println("found a method");
                                    found = true;
                                }
                            }
                        }
                        //don't forget to increment!
                        i++;
                    } //end while

                } else {
                    //                  ***                  System.out.println("not a compilation unit?");
                    //                  ***                  System.out.println(myJavaElement.getElementType());
                }
                //ok... now what type is our selected item? 
                //               ***               System.out.println("got the resource?");
                if (ourRes == null) {
                    //                  ***                  System.out.println("null resource???");
                }
            } else {
                //               ***                  System.out.println("not a java file?");
            }
            //            from the newsgroup - in a runnable?                  
            ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
                public void run(IProgressMonitor monitor) {
                    try {
                        //                     ***                           System.out.println("removing our markers, etc.");
                        //                     ourResource.setPersistentProperty()
                        SEURATResourcePropertiesManager.addPersistentProperty(ourRes, "Rat", "false");
                        SEURATDecoratorManager.addSuccessResources(ourRes);
                        ourRes.deleteMarkers("SEURAT.ratmarker", true, IResource.DEPTH_INFINITE);
                        RationaleDB d = RationaleDB.getHandle();
                        d.removeAssociation(ourRes.getName());
                    } catch (CoreException e) {
                        e.printStackTrace();
                    }
                }
            }, null);

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("an exception occured in AssociateArtifactAction");
        }
    }

}

From source file:edu.wpi.cs.jburge.SEURAT.actions.RestoreAssociations.java

License:Open Source License

/**
 * This is where the action takes place when the restore is invoked from the SEURAT pull-down menu.
 *///from  w  w w.j av  a  2  s.  c om
public void run() {
    //find all our resources
    RationaleDB d = RationaleDB.getHandle();
    Vector<Association> ourResources = d.getAssociations();
    Iterator<Association> resI = ourResources.iterator();
    try {

        while (resI.hasNext()) {
            cstart = 0;
            ourAssoc = (Association) resI.next();
            String ourArtifact = ourAssoc.getArtifact();

            //System.out.println(ourArtifact);
            //System.out.println(ourAssoc.getResource());
            try {
                // We create the java element from its artifact that is stored in the DB
                // and then search through the resource to find out where the marker needs to be placed.
                IJavaElement ourEle = JavaCore.create(ourArtifact);
                //System.out.println(ourEle.getElementName() + " " + ourEle.getElementType());

                if (ourEle.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    ourRes = ourEle.getCorrespondingResource();
                } else {
                    ourRes = ourEle.getUnderlyingResource();
                    if (ourRes != null) {
                        //                     ***                        System.out.println("this one wasn't null?");
                    }
                    //find the enclosing class file
                    IJavaElement nextE = ourEle.getParent();
                    while ((nextE != null) && (nextE.getElementType() != IJavaElement.COMPILATION_UNIT)) {
                        //                     ***                        System.out.println("Name = " + nextE.getElementName());
                        //                     ***                     System.out.println("Type = " + nextE.getElementType());
                        nextE = nextE.getParent();
                    }
                    try {
                        //                     ***                     System.out.println("getting our resource");
                        //                  ourRes = nextE.getUnderlyingResource();
                        ourRes = nextE.getCorrespondingResource();
                        ourRes = nextE.getResource();
                    } catch (JavaModelException ex) {
                        System.out.println("exception getting resource?");
                    }
                    //                  ***                     System.out.println("Final name = " + nextE.getElementName());
                    //                  ***                     System.out.println("Final type = " + nextE.getElementType());
                    if (ourRes == null) {
                        //                     ***                        System.out.println("see if there's a working copy");
                        IJavaElement original = nextE.getPrimaryElement();
                        //see if we can get the element from the working copy
                        //                     IJavaElement original = ((IWorkingCopy) ((ICompilationUnit) nextE).getWorkingCopy()).getOriginalElement();
                        ourRes = original.getCorrespondingResource();
                    }
                }
                //                  ourRes = navigatorSelection.getUnderlyingResource();
                if (ourRes == null) {
                    //                  ***                     System.out.println("why would our resource be null?");
                }
                //               ***                  System.out.println("FullPath = " + ourRes.getFullPath().toString());
                //               ***                  System.out.println("now checking file extension?");
                if (ourRes.getFullPath().getFileExtension().compareTo("java") == 0) {
                    //                  ***                     System.out.println("creating our file?");
                    IJavaElement myJavaElement = JavaCore.create((IFile) ourRes);
                    //                  ***                  System.out.println("created an element?");
                    if (myJavaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                        //                     ***                     System.out.println("Compilation Unit");
                        ICompilationUnit myCompilationUnit = (ICompilationUnit) myJavaElement;

                        IType[] myTypes = myCompilationUnit.getTypes();
                        boolean found = false;
                        int i = 0;
                        while ((!found) && i < myTypes.length) {
                            //selected item was the class itself
                            if (ourEle.getElementType() == IJavaElement.COMPILATION_UNIT) {
                                //                           ***                            System.out.println("found the class");
                                if (myTypes[i].isClass()) {
                                    found = true;
                                    cstart = myTypes[i].getNameRange().getOffset();
                                }
                            } else if (ourEle.getElementType() == IJavaElement.FIELD) {
                                //                           ***                            System.out.println("looking for types");
                                IField[] myFields = myTypes[i].getFields();
                                for (int j = 0; j < myFields.length; j++) {
                                    if (myFields[j].getElementName().compareTo(ourEle.getElementName()) == 0) {
                                        //                                 ***                               System.out.println("found a type");
                                        found = true;
                                        cstart = myFields[j].getNameRange().getOffset();
                                    }
                                }

                            } else if (ourEle.getElementType() == IJavaElement.METHOD) {
                                //                           ***                            System.out.println("looking for a method");
                                IMethod[] myMethods = myTypes[i].getMethods();
                                for (int j = 0; j < myMethods.length; j++) {
                                    if (myMethods[j].getElementName().compareTo(ourEle.getElementName()) == 0) {
                                        //                                 ***                         System.out.println("found a method");
                                        found = true;
                                        cstart = myMethods[j].getNameRange().getOffset();
                                    }
                                }
                            }
                            //don't forget to increment!
                            i++;
                        } //end while

                    } else {
                        //                     ***                      System.out.println("not a compilation unit?");
                        System.out.println(myJavaElement.getElementType());
                    }
                    //ok... now what type is our selected item? 
                    System.out.println("got the resource?");
                    if (ourRes == null) {
                        //                     ***                     System.out.println("null resource???");
                    }
                } else {
                    //                  ***                     System.out.println("not a java file?");
                }
                //               from the newsgroup - in a runnable?                  
                ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
                    public void run(IProgressMonitor monitor) {
                        try {
                            //                        ***                  System.out.println("line number = " + new Integer(lineNumber).toString());
                            IMarker ratM = ourRes.createMarker("SEURAT.ratmarker");
                            String markD = ourAssoc.getMsg();
                            ratM.setAttribute(IMarker.MESSAGE, markD);
                            ratM.setAttribute(IMarker.CHAR_START, cstart);
                            ratM.setAttribute(IMarker.CHAR_END, cstart + 1);
                            ratM.setAttribute(IMarker.SEVERITY, 0);
                            System.out.println(cstart);
                            Alternative ourAlt = (Alternative) RationaleDB
                                    .getRationaleElement(ourAssoc.getAlt(), RationaleElementType.ALTERNATIVE);
                            ratM.setAttribute("alternative", ourAlt.getName());
                            SEURATResourcePropertiesManager.addPersistentProperty(ourRes, "Rat", "true");

                        } catch (CoreException e) {
                            e.printStackTrace();
                        }
                    }
                }, null);
                //               ***                  System.out.println("adding persistent property");

                SEURATDecoratorManager.addSuccessResources(ourRes);
                //               ***                  System.out.println("added our property");  
                // Refresh the label decorations... Change it to DemoDecoratorWithImageCaching if image caching should be used
                //               ((TreeParent) obj).setStatus(RationaleErrorLevel.ERROR);
                //              viewer.update((TreeParent) obj, null);
                //for some reason the next line is giving us an exception - ???
                //it doesn't seem to be needed - the decorator appears.
                //         SEURATLightWeightDecorator.getRatDecorator().refresh();
                //               ***                  System.out.println("refresh");

            } catch (Exception e) {
                System.out.println("couldn't create our element " + ourArtifact);
            }

        }
    } catch (Exception ex) {
        System.out.println("exception while trying to add associations");
    }

}

From source file:edu.wpi.cs.jburge.SEURAT.views.RationaleExplorer.java

License:Open Source License

/**
 * This method is used to associate the java file with alternative in the ratinale 
 * explore in the java direction/*ww  w.j  a v a  2 s. com*/
 */
public void associateAlternative(RationaleUpdateEvent e)

{
    //get the Java element selected in the Package Explorer from the RationaleUpdateEvent
    navigatorSelection = e.getIJavaElement();

    if (navigatorSelection != null) {

        ISelection selection = viewer.getSelection();

        obj = ((IStructuredSelection) selection).getFirstElement();

        //whether an alternative is selected?
        if (obj != null && ((TreeParent) obj).getType() == RationaleElementType.ALTERNATIVE) {
            alternativeName = ((TreeParent) obj).getName();
        }
        //if there is no alternative selected, provide a select items of alternative
        else {
            SelectItem selectItem = new SelectItem(ourDisplay, RationaleElementType.fromString("Alternative"));
            //DEBUG: Is this where the infinite loop is? (YQ)
            if (selectItem == null) {
                System.out.println("NULL selectItem at line 2098");
            }
            if (selectItem.getNewItem() == null) {
                System.out.println("NULL Pointer at line 2099 in RationaleExplorer.java Expect Crashes");
            }
            alternativeName = selectItem.getNewItem().getName();
        }

        String assQ = "Associate '" + alternativeName + "' with " + navigatorSelection.getElementName() + "?";

        boolean selOk = showQuestion(assQ);
        if (selOk) {
            cstart = 0;

            ourRes = null;
            try {

                if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    ourRes = navigatorSelection.getCorrespondingResource();
                } else {
                    ourRes = navigatorSelection.getUnderlyingResource();
                    if (ourRes != null) {
                        //                     ***                        System.out.println("this one wasn't null?");
                    }
                    //find the enclosing class file
                    IJavaElement nextE = navigatorSelection.getParent();
                    while ((nextE != null) && (nextE.getElementType() != IJavaElement.COMPILATION_UNIT)) {
                        //                     ***                        System.out.println("Name = " + nextE.getElementName());
                        //                     ***                     System.out.println("Type = " + nextE.getElementType());
                        nextE = nextE.getParent();
                    }
                    try {
                        //                     ***                     System.out.println("getting our resource");
                        //                  ourRes = nextE.getUnderlyingResource();
                        ourRes = nextE.getCorrespondingResource();
                        ourRes = nextE.getResource();
                    } catch (JavaModelException ex) {
                        System.out.println("exception getting resource?");
                    }
                    //                  ***                     System.out.println("Final name = " + nextE.getElementName());
                    //                  ***                     System.out.println("Final type = " + nextE.getElementType());
                    if (ourRes == null) {
                        //see if we can get the element from the working copy
                        IJavaElement original = nextE.getPrimaryElement();
                        //                     Get working copy has been deprecated
                        //                     IJavaElement original = ((ICompilationUnit) ((ICompilationUnit) nextE).getWorkingCopy()).getOriginalElement();
                        ourRes = original.getCorrespondingResource();

                    }
                }
                //                  ourRes = navigatorSelection.getUnderlyingResource();
                if (ourRes == null) {
                    //                  ***                     System.out.println("why would our resource be null?");
                }
                //               ***                  System.out.println("FullPath = " + ourRes.getFullPath().toString());
                //               ***                  System.out.println("now checking file extension?");
                if (ourRes.getFullPath().getFileExtension().compareTo("java") == 0) {
                    //                  ***                     System.out.println("creating our file?");
                    IJavaElement myJavaElement = JavaCore.create((IFile) ourRes);
                    //                  ***                  System.out.println("created an element?");
                    if (myJavaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                        //                     ***                     System.out.println("Compilation Unit");
                        ICompilationUnit myCompilationUnit = (ICompilationUnit) myJavaElement;

                        IType[] myTypes = myCompilationUnit.getTypes();
                        boolean found = false;
                        int i = 0;
                        while ((!found) && i < myTypes.length) {
                            //selected item was the class itself
                            if (navigatorSelection.getElementType() == IJavaElement.COMPILATION_UNIT) {
                                //                           ***                            System.out.println("found the class");
                                if (myTypes[i].isClass()) {
                                    found = true;
                                    cstart = myTypes[i].getNameRange().getOffset();
                                }
                            } else if (navigatorSelection.getElementType() == IJavaElement.FIELD) {
                                //                           ***                            System.out.println("looking for types");
                                IField[] myFields = myTypes[i].getFields();
                                for (int j = 0; j < myFields.length; j++) {
                                    if (myFields[j].getElementName()
                                            .compareTo(navigatorSelection.getElementName()) == 0) {
                                        //                                 ***                               System.out.println("found a type");
                                        found = true;
                                        cstart = myFields[j].getNameRange().getOffset();
                                    }
                                }

                            } else if (navigatorSelection.getElementType() == IJavaElement.METHOD) {
                                //                           ***                            System.out.println("looking for a method");
                                IMethod[] myMethods = myTypes[i].getMethods();
                                for (int j = 0; j < myMethods.length; j++) {
                                    if (myMethods[j].getElementName()
                                            .compareTo(navigatorSelection.getElementName()) == 0) {
                                        //                                 ***                               System.out.println("found a method");
                                        found = true;
                                        cstart = myMethods[j].getNameRange().getOffset();
                                    }
                                }
                            }
                            //don't forget to increment!
                            i++;
                        } //end while
                    } else {
                        //                     ***                      System.out.println("not a compilation unit?");
                        System.out.println(myJavaElement.getElementType());
                    }
                    //ok... now what type is our selected item? 
                    System.out.println("got the resource?");
                    if (ourRes == null) {
                        System.out.println("null resource???");
                    }
                } else {
                    System.out.println("not a java file?");
                }
                //               from the newsgroup - in a runnable?                  
                ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
                    public void run(IProgressMonitor monitor) {
                        try {
                            //                        ***                  System.out.println("line number = " + new Integer(lineNumber).toString());
                            IMarker ratM = ourRes.createMarker("SEURAT.ratmarker");
                            String dbname = RationaleDB.getDbName();
                            String markD = "Alt: '" + alternativeName + "'   Rationale DB: '" + dbname + "'";
                            ratM.setAttribute(IMarker.MESSAGE, markD);
                            ratM.setAttribute(IMarker.CHAR_START, cstart);
                            ratM.setAttribute(IMarker.CHAR_END, cstart + 1);
                            ratM.setAttribute(IMarker.SEVERITY, 0);
                            //                        ratM.setAttribute(IMarker.LINE_NUMBER, lineNumber);
                            String artName = navigatorSelection.getElementName();
                            ratM.setAttribute("alternative", alternativeName);
                            SEURATResourcePropertiesManager.addPersistentProperty(ourRes, "Rat", "true");
                            RationaleDB d = RationaleDB.getHandle();
                            d.associateAlternative(alternativeName, navigatorSelection.getHandleIdentifier(),
                                    ourRes.getName(), artName, markD);

                        } catch (CoreException e) {
                            e.printStackTrace();
                        }
                    }
                }, null);
                //               ***                  System.out.println("adding persistent property");

                SEURATDecoratorManager.addSuccessResources(ourRes);
                //               ***                  System.out.println("added our property");  
                // Refresh the label decorations... Change it to DemoDecoratorWithImageCaching if image caching should be used
                //               ((TreeParent) obj).setStatus(RationaleErrorLevel.ERROR);
                //Is this the inf loop? (YQ)
                if (obj == null) {
                    System.out.println("CRITICAL ERROR: Obj is null at RationaleExplorer.java, line 2280");
                }
                if (viewer == null) {
                    System.out.println("Viewer is null at RationaleExplorer.java line 2283");
                }
                viewer.update((TreeParent) obj, null);
                SEURATLightWeightDecorator.getRatDecorator().refresh();
                //               ***                  System.out.println("refresh");

            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("an exception occured in AssociateArtifactAction");
            }

        } else {
            System.out.println("selection rejected");
        }
    }

    else {
        System.out.println("No java element selected...");
    }

}

From source file:fede.workspace.eclipse.java.fields.MethodField.java

License:Apache License

/**
 * Gets the package.//from w w  w  . j a v  a  2  s. c o  m
 * 
 * @param t
 *            the t
 * 
 * @return the package
 */
private IPackageFragment getPackage(IType t) {
    IJavaElement je = t.getParent();
    while (je != null) {
        if (je instanceof IPackageFragment) {
            return (IPackageFragment) je;
        }
        je = je.getParent();
    }
    return null;
}

From source file:fede.workspace.eclipse.java.fields.MethodField.java

License:Apache License

/**
 * Gets the type.//from   www .j a  v a2  s.  c  om
 * 
 * @param m
 *            the m
 * 
 * @return the type
 */
private IType getType(IMethod m) {
    IJavaElement je = m.getParent();
    while (je != null) {
        if (je instanceof IType) {
            return (IType) je;
        }
        je = je.getParent();
    }
    return null;
}

From source file:iot.jcypher.eclipse.JCypherCompletionProposalComputer.java

License:Apache License

private List<ICompletionProposal> buildJCypherProposals(List<ICompletionProposal> proposals) {
    List<ICompletionProposal> ret = new ArrayList<>(proposals.size());
    List<String> classNames = new ArrayList<String>();
    List<String> classNames_ext = new ArrayList<String>();
    for (ICompletionProposal prop : proposals) {
        if (prop instanceof IJavaCompletionProposal) {
            if (prop instanceof JavaMethodCompletionProposal) {
                JavaMethodCompletionProposal jmcp = (JavaMethodCompletionProposal) prop;
                IJavaElement elem = jmcp.getJavaElement();
                IJavaElement classfile = elem.getParent().getParent();
                if (classfile.getElementType() == IJavaElement.CLASS_FILE
                        || classfile.getElementType() == IJavaElement.COMPILATION_UNIT) {
                    IJavaElement pkg = classfile.getParent();
                    if (JCypherPackages.addsToProposal(pkg.getElementName())) {
                        IJavaCompletionProposal delegate = (IJavaCompletionProposal) prop;
                        IJavaElement mthd = ((AbstractJavaCompletionProposal) delegate).getJavaElement();
                        IJavaElement clsfile = elem.getParent().getParent();
                        String className = clsfile.getElementName();
                        if (!classNames_ext.contains(className)) {
                            classNames_ext.add(className);
                            // trim className extension which can either be .class or .java
                            classNames.add(className.substring(0, className.indexOf('.')));
                        }//  www. j  ava2s.  co  m
                        // classNames always contains (class, superClass, superSuperClass, ..)
                        int relevance = ProposalOrderConfig.INSTANCE.getRelevance(classNames.get(0),
                                mthd.getElementName(), JCypherConstants.DEFAULT_RELEVANCE);
                        ret.add(new JCypherCompletionProposal((IJavaCompletionProposal) prop, relevance));
                    }
                }
            }
        }
    }
    if (ret.size() > 0) {
        ProposalOrderConfig.INSTANCE.addSeparators(classNames.get(0), ret, JCypherConstants.DEFAULT_RELEVANCE);
        ret.add(CompletionSeparator.INSTANCE);
    }
    return ret;
}

From source file:jd.ide.eclipse.editors.JDClassFileEditor.java

License:Open Source License

@SuppressWarnings("rawtypes")
protected void setupSourceMapper(IClassFile classFile) {
    try {/*ww  w .j a  v  a  2 s.c o  m*/
        // Search package fragment root and classPath
        IJavaElement packageFragment = classFile.getParent();
        IJavaElement packageFragmentRoot = packageFragment.getParent();

        if (packageFragmentRoot instanceof PackageFragmentRoot) {
            // Setup a new source mapper.
            PackageFragmentRoot root = (PackageFragmentRoot) packageFragmentRoot;

            // Location of the archive file containing classes.
            IPath basePath = root.getPath();
            File baseFile = basePath.makeAbsolute().toFile();

            if (!baseFile.exists()) {
                IResource resource = root.getCorrespondingResource();
                basePath = resource.getLocation();
                baseFile = basePath.makeAbsolute().toFile();
            }

            // Class path
            String classPath = classFile.getElementName();
            String packageName = packageFragment.getElementName();
            if ((packageName != null) && (packageName.length() > 0))
                classPath = packageName.replace('.', '/') + '/' + classPath;

            // Location of the archive file containing source.
            IPath sourcePath = root.getSourceAttachmentPath();
            if (sourcePath == null)
                sourcePath = basePath;

            // Location of the package fragment root within the zip 
            // (empty specifies the default root).
            IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
            String sourceRootPath;

            if (sourceAttachmentRootPath == null) {
                sourceRootPath = null;
            } else {
                sourceRootPath = sourceAttachmentRootPath.toString();
                if ((sourceRootPath != null) && (sourceRootPath.length() == 0))
                    sourceRootPath = null;
            }

            // Options
            Map options = root.getJavaProject().getOptions(true);

            root.setSourceMapper(new JDSourceMapper(baseFile, sourcePath, sourceRootPath, options));
        }
    } catch (CoreException e) {
        JavaDecompilerPlugin.getDefault().getLog()
                .log(new Status(Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID, 0, e.getMessage(), e));
    }
}

From source file:kieker.develop.rl.ui.wizards.RecordLangNewWizardPage.java

License:Apache License

/**
 * Constructor for RecordLangNewWizardPage.
 *
 * @param selection/*from w  w w .  j  av a2  s. c  om*/
 *            selected element in package explorer
 */
public RecordLangNewWizardPage(final ISelection selection) {
    super("wizardPage");
    this.setTitle("IRL File");
    this.setDescription(
            "This wizard creates a new file with *.irl extension that can be opened by the IRL editor.");
    this.selection = selection;

    if (this.selection instanceof TreeSelection) {
        final TreeSelection container = (TreeSelection) this.selection;

        final Object element = container.getFirstElement();
        if (element instanceof IPackageFragment) {
            this.sourcePackage = (IPackageFragment) element;
            IJavaElement parent = this.sourcePackage.getParent();
            while (!(parent instanceof IPackageFragmentRoot) && parent != null) {
                parent = parent.getParent();
            }
            if (parent != null) {
                this.sourceFolder = (IPackageFragmentRoot) parent;
            }
        } else if (element instanceof IPackageFragmentRoot) {
            this.sourceFolder = (IPackageFragmentRoot) element;
            this.sourcePackage = this.sourceFolder.getPackageFragment("");
        } else if (element instanceof IJavaProject) {
            this.sourceFolder = null;
            this.sourcePackage = null;
        } else {
            this.sourceFolder = null;
            this.sourcePackage = null;
        }
    }
}