Example usage for org.eclipse.jdt.core IPackageDeclaration getElementName

List of usage examples for org.eclipse.jdt.core IPackageDeclaration getElementName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageDeclaration getElementName.

Prototype

@Override
String getElementName();

Source Link

Document

Returns the name of the package the statement refers to.

Usage

From source file:com.curlap.orb.plugin.common.JavaElementSearcher.java

License:Open Source License

public JavaElementSearcher(ICompilationUnit target) throws JavaModelException {
    scope = createScope(target);//  ww  w  .ja v  a  2s. c o  m
    packageNamesForMask = createPackageNameMask(target);

    // get current package name.
    // TODO handle the case where the package declaration doesn't exist.
    IPackageDeclaration[] pkgDecs = target.getPackageDeclarations();
    for (IPackageDeclaration pkgDec : pkgDecs)
        currentPackageName = pkgDec.getElementName();
}

From source file:com.jstar.eclipse.objects.JavaFile.java

License:BSD License

public String getPackage() {
    try {/*from  w  w w  . jav  a2s  .  co m*/
        IPackageDeclaration[] packages = getCompilationUnit().getPackageDeclarations();

        if (packages.length == 0) {
            return "";
        }

        //ICompilationUnit.getPackageDeclaration documentation: There normally is at most one package declaration.
        final IPackageDeclaration filePackage = packages[0];

        return filePackage.getElementName() + ".";
    } catch (JavaModelException jme) {
        ConsoleService.getInstance()
                .printErrorMessage("Cannot obtain the package declaration from the source file.");
        throw new RuntimeException();
    }
}

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

License:Open Source License

private static void outputNameDetails(IPackageDeclaration pkg, IvyXmlWriter xw) {
    outputSymbol(pkg, "PackageDecl", pkg.getElementName(), null, xw);
}

From source file:net.sf.refactorit.eclipse.RitActionDelegate.java

License:Open Source License

private Object getBinObject(IJavaElement element) throws NotFromSrcOrFromIgnoredException {
    Project project = IDEController.getInstance().getActiveProject();

    try {//from   ww  w . ja  v  a  2s.c  o  m
        if (element instanceof IJavaProject) {
            return project;
        }

        if (element instanceof IPackageFragmentRoot) {
            return project;
        }

        if (element instanceof IPackageDeclaration) {
            IPackageDeclaration pd = (IPackageDeclaration) element;
            return ItemByNameFinder.findBinPackage(project, pd.getElementName());
        }

        if (element instanceof IPackageFragment) {
            IPackageFragment pf = (IPackageFragment) element;
            return ItemByNameFinder.findBinPackage(project, pf.getElementName());
        }

        if (element instanceof ICompilationUnit) {
            ICompilationUnit cu = (ICompilationUnit) element;
            IType type = cu.findPrimaryType();
            if (type != null) {
                return getBinObject(type);
            }
            return null;
        }

        if (element instanceof IClassFile) {
            return getBinObject(((IClassFile) element).getType());
        }

        if (element instanceof IType) {
            IType type = (IType) element;
            if (type.isClass() || type.isInterface()) {
                String typeName = type.getFullyQualifiedName();

                Object o = ItemByNameFinder.findBinCIType(project, typeName);

                if (o != null) {
                    return o;
                } else {
                    throw new NotFromSrcOrFromIgnoredException();
                }
            }
            return null; // TODO: enum/annotation
        }

        if (element instanceof IMember) {
            IMember member = (IMember) element;
            IType type = member.getDeclaringType();

            BinCIType btype = ItemByNameFinder.findBinCIType(project, type.getFullyQualifiedName());

            if (btype == null) {
                throw new NotFromSrcOrFromIgnoredException();
            }

            if (member instanceof IField) {
                return ItemByNameFinder.findBinField(btype, member.getElementName());
            }

            if (member instanceof IMethod) {
                IMethod method = (IMethod) member;

                String[] signatures = method.getParameterTypes();
                String[] args = new String[signatures.length];
                for (int i = 0; i < args.length; i++) {
                    args[i] = Signature.toString(signatures[i]);
                }

                if (method.isConstructor()) {
                    return ItemByNameFinder.findBinConstructor((BinClass) btype, args);
                }

                return ItemByNameFinder.findBinMethod(btype, member.getElementName(), args);
            }

            if (element instanceof IInitializer) {
                return null; // TODO: initializer
            }
        }
    } catch (JavaModelException e) {
        log.debug("getBinClass()", e);
    }

    return null;
}

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

License:Open Source License

/**
 * @param declaration//from  w w w . ja va  2 s . c  o m
 * @return
 */
private static String getStringForPackage(IPackageDeclaration declaration) {
    return declaration.getElementName();
}

From source file:org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory.java

License:Open Source License

public String getMappedSuperclassPackageDeclaration(PersistentType jpt) throws JavaModelException {
    String packageName = null;//  w  w  w . j a v a2  s  . c o m
    IPackageDeclaration[] packages = JPAEditorUtil.getCompilationUnit(jpt).getPackageDeclarations();
    if (packages.length > 0) {
        IPackageDeclaration packageDecl = packages[0];
        packageName = packageDecl.getElementName();
    }
    return packageName;
}

From source file:org.incha.core.jswingripples.parser.BindingSupport.java

License:Open Source License

/**
 * @param shortName/*from   w w w .j ava2  s.c om*/
 * @return
 */
private String[][] resolveType(final String shortName) {
    final List<String> types = new LinkedList<String>();
    try {
        for (final ICompilationUnit unit : units) {
            for (final IType type : unit.getTypes()) {
                if (type.getFullyQualifiedName().endsWith("." + shortName)) {
                    types.add(type.getFullyQualifiedName());
                }
            }

            //add some package
            final IPackageDeclaration[] packages = unit.getPackageDeclarations();
            for (final IPackageDeclaration p : packages) {
                types.add(p.getElementName() + "." + shortName);
            }

            //add also types from import declaration
            final IImportDeclaration[] imports = unit.getImports();
            for (final IImportDeclaration decl : imports) {
                String className = null;
                final String name = decl.getElementName();

                if (decl.isOnDemand()) {
                    className = name.substring(0, name.length() - 1) + shortName;
                } else if (name.endsWith(shortName)) {
                    className = name;
                }

                if (className != null) {
                    types.add(className);
                }
            }
        }
    } catch (final JavaModelException e) {
        e.printStackTrace();
    }

    final String[][] candidates = new String[types.size()][];
    for (int i = 0; i < types.size(); i++) {
        final String className = types.get(i);
        final int index = className.lastIndexOf('.');
        candidates[i] = new String[2];
        if (index > -1) {
            candidates[i][0] = className.substring(0, index);
            candidates[i][1] = className.substring(index + 1);
        } else {
            candidates[i][0] = "";
            candidates[i][1] = className;
        }
    }
    return candidates;
}

From source file:org.incha.core.jswingripples.parser.BindingSupport.java

License:Open Source License

/**
 * @param binding//from w  ww  .jav  a 2 s  .c  om
 * @return
 * @throws JavaModelException
 */
public IPackageDeclaration findPackageDeclaration(final IPackageBinding binding) throws JavaModelException {
    final IPackageBinding pckg = binding;
    for (final ICompilationUnit unit : units) {
        final IPackageDeclaration[] packages = unit.getPackageDeclarations();
        for (final IPackageDeclaration p : packages) {
            if (p.getElementName().equals(pckg.getName())) {
                return p;
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.cdi.internal.core.impl.definition.PackageDefinition.java

License:Open Source License

public void setPackage(IPackageDeclaration pkg, IRootDefinitionContext context) {
    qualifiedName = pkg.getElementName();
    IType contextType = null;/*w w  w .  ja v  a  2  s.c  o m*/
    ICompilationUnit u = null;
    if (pkg.getParent() instanceof ICompilationUnit) {
        try {
            u = ((ICompilationUnit) pkg.getParent()).getWorkingCopy(new NullProgressMonitor());
            contextType = u.createType("class A {}", null, false, new NullProgressMonitor());
        } catch (JavaModelException e) {

        }
    }
    super.setAnnotatable(pkg, contextType, context, 0);
    if (u != null) {
        try {
            u.discardWorkingCopy();
        } catch (JavaModelException e) {

        }
    }
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

public static boolean addImport(String qualifiedName, ICompilationUnit compilationUnit, boolean staticFlag,
        MultiTextEdit rootEdit) throws JavaModelException {
    if (primitives.contains(qualifiedName) || qualifiedName.indexOf(DOT) < 0)
        return false;

    if (qualifiedName != null) {
        String shortName = getShortName(qualifiedName);

        IPackageDeclaration[] packages = compilationUnit.getPackageDeclarations();

        // local classes do not need to be imported
        String typePackage = qualifiedName.substring(0, qualifiedName.lastIndexOf(DOT));

        for (IPackageDeclaration packageDeclaration : packages) {
            if (packageDeclaration.getElementName().equals(typePackage))
                return false;
        }/*from   w w w .  ja  v  a2 s  .  c o  m*/

        for (IPackageDeclaration packageDeclaration : packages) {
            IType type = compilationUnit.getJavaProject()
                    .findType(packageDeclaration.getElementName() + DOT + shortName);
            if (type != null && type.exists())
                return true;
        }

        IImportDeclaration[] importDeclarations = compilationUnit.getImports();

        for (IImportDeclaration importDeclaration : importDeclarations) {
            String importName = importDeclaration.getElementName();
            String elementShort = getShortName(importName);
            if (importDeclaration.isOnDemand()) {
                int importLastDot = importName.lastIndexOf(DOT);
                if (importLastDot == -1)
                    return false; // invalid import declaration
                int elementLastDot = qualifiedName.lastIndexOf(DOT);
                if (elementLastDot == -1)
                    return false; // invalid import declaration

                if (qualifiedName.substring(0, elementLastDot).equals(importName.substring(0, importLastDot)))
                    return false;
            }

            if (importName.equals(qualifiedName))
                return false;
            if (elementShort.equals(shortName))
                return true;

        }
        if (rootEdit == null) {
            if (staticFlag) {
                compilationUnit.createImport(qualifiedName, null, Flags.AccStatic, new NullProgressMonitor());
            } else {
                compilationUnit.createImport(qualifiedName, null, new NullProgressMonitor());
            }
        } else {
            String staticStr = "";
            if (staticFlag) {
                staticStr = STATIC + SPACE;
            }
            String text = compilationUnit.findRecommendedLineSeparator() + IMPORT + SPACE + staticStr
                    + qualifiedName + SEMICOLON;
            if (!isDuplicate(rootEdit, text)) {
                int importPosition = findPositionForImport(compilationUnit);
                TextEdit edit = new InsertEdit(importPosition, text);
                rootEdit.addChild(edit);
            }
        }
    }
    return false;
}