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

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

Introduction

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

Prototype

int COMPILATION_UNIT

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

Click Source Link

Document

Constant representing a Java compilation unit.

Usage

From source file:org.eclipse.mylyn.java.ui.editor.MylarJavaOutlinePage.java

License:Open Source License

/**
 * Checks whether a given Java element is an inner type.
 * /*from  w  ww  .  j av a2  s .c  om*/
 * @param element the java element
 * @return <code>true</code> iff the given element is an inner type
 */
private boolean isInnerType(IJavaElement element) {

    if (element != null && element.getElementType() == IJavaElement.TYPE) {
        IType type = (IType) element;
        try {
            return type.isMember();
        } catch (JavaModelException e) {
            IJavaElement parent = type.getParent();
            if (parent != null) {
                int parentElementType = parent.getElementType();
                return (parentElementType != IJavaElement.COMPILATION_UNIT
                        && parentElementType != IJavaElement.CLASS_FILE);
            }
        }
    }

    return false;
}

From source file:org.eclipse.objectteams.otdt.core.OTModelManager.java

License:Open Source License

/**
 * Add the propriate Object Teams Model element for a given IType 
 *///from www . j a  va 2s. c o m
public IOTType addType(IType elem, int typeDeclFlags, String baseClassName, String baseClassAnchor,
        boolean isRoleFile) {
    IJavaElement parent = elem.getParent();
    IOTType result = null;

    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
    case IJavaElement.CLASS_FILE:
        if (isRoleFile) { //  could also be a teeam, which is handled inside the constructor
            if (elem.isBinary()) {
                MAPPING.addOTElement(result = new BinaryRoleType(elem, parent, typeDeclFlags, baseClassName,
                        baseClassAnchor));
            } else {
                MAPPING.addOTElement(
                        result = new RoleFileType(elem, parent, typeDeclFlags, baseClassName, baseClassAnchor));
            }
        } else if (TypeHelper.isTeam(typeDeclFlags)) {
            MAPPING.addOTElement(result = new OTType(IOTJavaElement.TEAM, elem, null, typeDeclFlags));
        }
        break;
    case IJavaElement.TYPE:
        IType encType = (IType) parent;
        IOTType otmParent = MAPPING.getOTElement(encType);

        result = maybeAddRoleType(elem, otmParent, typeDeclFlags, baseClassName, baseClassAnchor);
        break;
    //do nothing if anonymous type
    case IJavaElement.METHOD:
        break;
    case IJavaElement.INITIALIZER:
        break;
    case IJavaElement.FIELD:
        break;
    //TODO (jwl) Wether anonymous types are roles or not will be discoverable with 
    //           a future implementation (probably with the help of a newer version of the compiler)

    //             case IJavaElement.METHOD:
    //              IMethod encMethod   = (IMethod)parent;
    //              otmParent = MAPPING.getOTElement(encMethod.getDeclaringType());
    //              
    //              addRoleType(elem, otmParent, typeDeclFlags, baseClassName);
    //              break;
    //          case IJavaElement.INITIALIZER:
    //              IInitializer encInitializer   = (IInitializer)parent;
    //              otmParent = MAPPING.getOTElement(encInitializer.getDeclaringType());
    //              
    //              addRoleType(elem, otmParent, typeDeclFlags, baseClassName);
    //              break;
    //          case IJavaElement.FIELD:
    //              IField encField   = (IField)parent;
    //              otmParent = MAPPING.getOTElement(encField.getDeclaringType());
    //              
    //              addRoleType(elem, otmParent, typeDeclFlags, baseClassName);
    //              break;
    default:
        new Throwable("Warning: unexpected parent for OT element: " + parent).printStackTrace(); //$NON-NLS-1$
        break;
    }
    return result;
}

From source file:org.eclipse.objectteams.otdt.core.TypeHelper.java

License:Open Source License

/**
 * Find a role type within a given team. 
 * Respect inline roles and role files./*  ww  w  . j  ava  2s  .  com*/
 * 
 * @param teamType where to look
 * @param roleName what to look for
 * @return null in cases the type is not found
 */
public static IType findRoleType(IType teamType, String roleName) {
    IType roleType = teamType.getType(roleName);

    // inline roles exit here:
    if (roleType.exists())
        return roleType;

    // look for a team package:
    // TODO(SH): does not yet account for all kinds of nesting!
    IJavaElement parent = teamType.getParent();
    if (parent.getElementType() == IJavaElement.COMPILATION_UNIT)
        parent = parent.getParent();
    if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        IPackageFragment enclosingPackage = (IPackageFragment) parent;
        IPackageFragmentRoot root = (IPackageFragmentRoot) enclosingPackage.getParent();
        try {
            IPackageFragment teamPackage = root
                    .getPackageFragment(enclosingPackage.getElementName() + '.' + teamType.getElementName());
            if (teamPackage.exists()) {

                // found the team package, look for the role file:
                IJavaElement[] cus = teamPackage.getChildren();
                for (int i = 0; i < cus.length; i++) {
                    if (cus[i].getElementType() == IJavaElement.COMPILATION_UNIT) {
                        ICompilationUnit cu = (ICompilationUnit) cus[i];
                        IJavaElement[] children = cu.getChildren();
                        for (int j = 0; j < children.length; j++) {
                            if (children[j].getElementType() == IJavaElement.TYPE
                                    && children[j].getElementName().equals(roleName)) {
                                return (IType) children[j];
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException jme) {
            return null;
        }
    }
    return null;
}

From source file:org.eclipse.objectteams.otdt.internal.core.search.matching.ReferenceToTeamLocator.java

License:Open Source License

/**
 * A match was found, perform final check:
 * if a role was specified check the current compilation unit's first type against the role name.
 * If successful report as a type reference match regarding this first type.
 *///from  w w w.ja  va  2s. c o m
@Override
protected void matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element,
        int accuracy, MatchLocator locator) throws CoreException {
    if (locator.encloses(element)) {
        ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (cu != null) {
            IType[] types = cu.getTypes();
            if (types != null && types.length > 0) {
                // only now we have the info to check the role name:
                if (this.pattern.roleName != null
                        && !new String(this.pattern.roleName).equals(types[0].getElementName()))
                    return;

                int offset = importRef.sourceStart;
                int length = importRef.sourceEnd - offset + 1;
                this.match = locator.newTypeReferenceMatch(types[0], null/*binding*/, accuracy, offset, length,
                        importRef);
                if (this.match != null)
                    locator.report(this.match);
            }
        }
    }
}

From source file:org.eclipse.objectteams.otdt.internal.corext.RoleFileAdaptor.java

License:Open Source License

    /**
 * Fetch all names of base classes referenced from the given CU.
 * @param astRoot start searching packages from here.
 * @return list of simple base class names.
 *///from w  ww  .j a  va2s.c  o m
@SuppressWarnings("unchecked")
public static List<String> getRoFiBaseClassNames(CompilationUnit astRoot) {
   ArrayList<String> result = new ArrayList<String>();
   List<AbstractTypeDeclaration> types = astRoot.types();
   for (AbstractTypeDeclaration type : types) {
      if (type.isTeam()) {
         ITypeBinding typeBinding = type.resolveBinding();
         String teamName = ""; //$NON-NLS-1$
         if (typeBinding != null) {
            teamName = typeBinding.getQualifiedName();
         } else {
            PackageDeclaration currentPackage = astRoot.getPackage();
            if (currentPackage != null)
               teamName = currentPackage.getName().getFullyQualifiedName()+'.';
            teamName += type.getName().getIdentifier();
         }
         IJavaProject prj = astRoot.getJavaElement().getJavaProject();
         try {
            for (IPackageFragmentRoot roots : prj.getPackageFragmentRoots()) {
               IPackageFragment pkg = roots.getPackageFragment(teamName);
               if (pkg.exists())
                  for (IJavaElement cu : pkg.getChildren())
                     if (cu.getElementType() == IJavaElement.COMPILATION_UNIT)
                        for(IType roleType : ((org.eclipse.jdt.internal.core.CompilationUnit)cu).getTypes()) 
                        {
                           IOTType ottype = OTModelManager.getOTElement(roleType);
                           if (ottype != null && ottype.isRole()) {
                              String baseClass = ((IRoleType)ottype).getBaseclassName();
                              if (baseClass != null) {
                                 // always remember as simple name:
                                 int lastDot = baseClass.lastIndexOf('.');
                                 if (lastDot > -1)
                                    baseClass = baseClass.substring(lastDot+1);
                                 result.add(baseClass);
                              }
                           }
                        }
            }
         } catch (JavaModelException e) {
            // couldn't read team package, skip.
         }
      }
   }
   return result;
}

From source file:org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil.java

License:Open Source License

public static MethodDeclaration methodToDeclaration(IMethod method, CompilationUnit node)
        throws JavaModelException {
    ICompilationUnit methodCU = (ICompilationUnit) method.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (!methodCU.equals(node.getJavaElement())) {
        node = RefactoringASTParser.parseWithASTProvider(methodCU, true, null);
    }/* w w  w . ja v a2s .  c o m*/
    Name result = (Name) NodeFinder.perform(node, method.getNameRange());
    return (MethodDeclaration) getParent(result, MethodDeclaration.class);
}

From source file:org.eclipse.objectteams.otdt.internal.ui.callinmarkers.CallinMarkerCreator2.java

License:Open Source License

/**
 * Fetch all methods and fields contained in the input.
* @param javaElement the corresponding ICompilationUnit, IClassFile or IType
*//*from  w  w w .j a  v  a 2  s  . c  om*/
private Set<IMember> getAllMethodsAndFields(IJavaElement javaElement) {
    if (javaElement == null)
        return new HashSet<IMember>(0);

    Set<IMember> members = new HashSet<IMember>(13);

    switch (javaElement.getElementType()) {
    case IJavaElement.COMPILATION_UNIT: {
        ICompilationUnit unit = (ICompilationUnit) javaElement;
        IType[] types;
        try {

            types = unit.getTypes();
            for (int idx = 0; idx < types.length; idx++)
                members.addAll(getAllMethodsAndFields(types[idx]));

        } catch (JavaModelException e) {
            // ignore, without types we simply find no methods
        }
        break;
    }
    case IJavaElement.CLASS_FILE: {
        IClassFile classFile = (IClassFile) javaElement;
        members.addAll(getAllMethodsAndFields(classFile.getType()));
        break;
    }
    case IJavaElement.TYPE: {
        IType type = (IType) javaElement;
        try {

            members.addAll(Arrays.asList(type.getMethods()));
            members.addAll(Arrays.asList(type.getFields()));
            IOTType otType = OTModelManager.getOTElement(type);
            if (otType != null && otType.isRole())
                for (IMethodMapping mapping : ((IRoleType) otType).getMethodMappings(IRoleType.CALLOUTS))
                    members.add(mapping);
            // handle all inner types
            IType[] memberTypes = type.getTypes();
            for (int idx = 0; idx < memberTypes.length; idx++)
                members.addAll(getAllMethodsAndFields(memberTypes[idx]));

        } catch (JavaModelException e) {
            // ignore, finding methods bailed out but keep those we already found
        }
        break;
    }
    default:
        break;
    }

    return members;
}

From source file:org.eclipse.objectteams.otdt.internal.ui.viewsupport.TeamPackageUtil.java

License:Open Source License

/**
 * If aPackage is a team package answer the corresponding team compilation unit
 * //from   w  ww. jav a2s  . c o m
 * @param aPackage
 * @return a team unit or null.
 * @throws JavaModelException
 */
public static ICompilationUnit getTeamUnit(IPackageFragment aPackage) throws JavaModelException {
    ICompilationUnit[] units = aPackage.getCompilationUnits();
    if (units != null && units.length > 0) {
        IType firstType = JavaElementUtil.getMainType(units[0]);
        IOTType otType = OTModelManager.getOTElement(firstType);
        if (otType != null && otType.isRole()) {
            String unitName = aPackage.getPath().lastSegment() + ".java"; //$NON-NLS-1$
            IPackageFragment enclosingPackage = JavaElementUtil.getParentSubpackage(aPackage);
            if (enclosingPackage != null) {
                return enclosingPackage.getCompilationUnit(unitName);
            } else {
                // if parent is already the root, then we have a team in the default package, yacks.
                // need to travel one up, two down:
                IPackageFragmentRoot root = (IPackageFragmentRoot) aPackage.getParent();
                for (IJavaElement fragment : root.getChildren()) {
                    if (fragment.getElementName() == IPackageFragment.DEFAULT_PACKAGE_NAME)
                        for (IJavaElement element : ((IPackageFragment) fragment).getChildren())
                            if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
                                if (element.getElementName().equals(unitName))
                                    return (ICompilationUnit) element;
                }
            }
        }
    }
    return null;
}

From source file:org.eclipse.objectteams.otdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

@Override
protected IJavaElement getInitialJavaElement(IStructuredSelection structuredSelection) {
    IJavaElement element = super.getInitialJavaElement(structuredSelection);
    if (element != null && element.getElementType() == IJavaElement.COMPILATION_UNIT) {
        // try to improve:
        IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow();
        if (window != null) {
            ISelection selection = window.getSelectionService().getSelection();
            if (selection instanceof ITextSelection) {
                ITextSelection textSelection = (ITextSelection) selection;
                try {
                    IJavaElement selected = ((ICompilationUnit) element)
                            .getElementAt(textSelection.getOffset());
                    if (selected != null) {
                        selected = selected.getAncestor(IJavaElement.TYPE);
                        if (selected != null) {
                            if (((IType) selected).isLocal())
                                selected = ((IType) selected).getDeclaringType();
                            return selected;
                        }//from ww  w . j  a v  a2 s.  c o  m
                    }
                } catch (JavaModelException e) {
                    /* nop */ }
            }
        }
    }
    return element;
}

From source file:org.eclipse.objectteams.otdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

/**
 * initializes the package and enclosing type dialog fields
 * depending on the given initial selected IJavaElement
 * (that is the IJavaElement which was selected in the Package Explorer,
 *  when the request to open the wizard occured)
 *///from  w ww.jav  a 2s.co  m
protected void initPackageAndEnclosingType(IJavaElement initialSelectedElem) {
    IType potentialEnclosingType = null;
    IType typeInCU = (IType) initialSelectedElem.getAncestor(IJavaElement.TYPE);

    if (typeInCU != null) {
        if (typeInCU.getCompilationUnit() != null) {
            potentialEnclosingType = typeInCU;
        }
    } else {
        ICompilationUnit cu = (ICompilationUnit) initialSelectedElem.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (cu != null) {
            potentialEnclosingType = cu.findPrimaryType();
        }
    }

    //default case
    IPackageFragment packageFragment = (IPackageFragment) initialSelectedElem
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    String packName = (packageFragment == null) ? "" //$NON-NLS-1$
            : packageFragment.getElementName();
    setPackageFragmentName(packName);
    setEnclosingTypeName(""); //$NON-NLS-1$

    if (potentialEnclosingType != null) {
        if (OTModelManager.hasOTElementFor(potentialEnclosingType)) {
            IOTType potentialEnclosingOTType = OTModelManager.getOTElement(potentialEnclosingType);

            boolean hasChanges = false;
            if (potentialEnclosingOTType.isTeam()) {
                handleTeamSelected(potentialEnclosingOTType);
                hasChanges = true;
            } else //if potentialEnclosingOTType.isRole()
            {
                handleRoleSelected(potentialEnclosingOTType);
                hasChanges = true;
            }

            if (hasChanges) {
            }
        } else
            try {
                if (potentialEnclosingType.isClass()) {
                    handleClassSelected(potentialEnclosingType);
                }
            } catch (JavaModelException ex) {
                OTDTUIPlugin.logException("", ex); //$NON-NLS-1$
            }
    }
}