Example usage for org.eclipse.jdt.core IImportDeclaration getFlags

List of usage examples for org.eclipse.jdt.core IImportDeclaration getFlags

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IImportDeclaration getFlags.

Prototype

int getFlags() throws JavaModelException;

Source Link

Document

Returns the modifier flags for this import.

Usage

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

License:Open Source License

private void outputImports(IvyXmlWriter xw, IPackageFragment ipf) {
    try {//from www .j a v  a2s .com
        for (ICompilationUnit icu : ipf.getCompilationUnits()) {
            for (IImportDeclaration imp : icu.getImports()) {
                xw.begin("IMPORT");
                if (imp.isOnDemand())
                    xw.field("DEMAND", true);
                if (Modifier.isStatic(imp.getFlags()))
                    xw.field("STATIC", true);
                xw.text(imp.getElementName());
                xw.end("IMPORT");
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.che.jdt.search.JavaElementToDtoConverter.java

License:Open Source License

private void addImport(List<ImportDeclaration> result, Set<Object> imports) throws JavaModelException {
    for (Object im : imports) {
        if (im instanceof IImportDeclaration) {
            IImportDeclaration dec = (IImportDeclaration) im;
            ImportDeclaration importDeclaration = DtoFactory.newDto(ImportDeclaration.class);
            importDeclaration.setFlags(dec.getFlags());
            importDeclaration.setHandleIdentifier(dec.getHandleIdentifier());
            importDeclaration.setElementName(dec.getElementName());
            result.add(importDeclaration);
        }//from   ww w. j a va 2s  . c o  m
    }
}

From source file:org.eclipseguru.gwt.core.internal.codegen.AsyncServiceCodeGenerator.java

License:Open Source License

/**
 * Collects imports including imports from super-interfaces of the specified
 * type./* w ww .j av a  2  s  .co  m*/
 * 
 * @param type
 * @param imports
 * @throws JavaModelException
 */
private static void collectImports(final IType type, final ImportsManager imports) throws JavaModelException {
    final ICompilationUnit compilationUnit = type.getCompilationUnit();
    if (compilationUnit != null) {
        final IImportDeclaration[] existingImports = compilationUnit.getImports();
        for (final IImportDeclaration declaration : existingImports) {
            if (Flags.isStatic(declaration.getFlags())) {
                String name = Signature.getSimpleName(declaration.getElementName());
                final boolean isField = !name.endsWith("()"); //$NON-NLS-1$
                if (!isField) {
                    name = name.substring(0, name.length() - 2);
                }
                final String qualifier = Signature.getQualifier(declaration.getElementName());
                imports.addStaticImport(qualifier, name, isField);
            } else {
                imports.addImport(declaration.getElementName());
            }
        }
    }

    for (final String superInterfaceName : type.getSuperInterfaceNames()) {
        final String[][] resolvedType = type.resolveType(superInterfaceName);
        if (resolvedType != null) {
            for (final String[] typeSegments : resolvedType) {
                final IType superInterface = type.getJavaProject()
                        .findType(Signature.toQualifiedName(typeSegments));
                if (superInterface != null) {
                    collectImports(superInterface, imports);
                }
            }
        }
    }
}

From source file:org.fastcode.popup.actions.easycreate.CopyMemberAction.java

License:Open Source License

/**
 *
 * @param type//from ww w .  jav  a  2s. c om
 * @param importDeclaration
 * @param selection
 * @throws Exception
 */
protected void copyImport(final IType type, final IImportDeclaration importDeclaration,
        final ITextSelection selection) throws Exception {
    if (!Flags.isStatic(importDeclaration.getFlags())) {
        MessageDialog.openError(this.editorPart.getSite().getShell(), "Static import",
                "Please use this feature only for static imports.");
        return;
    }
    final String newImportStr = findImport(type, importDeclaration, selection);
    final IImportDeclaration newImportDecl = type.getCompilationUnit().createImport(newImportStr,
            findNextImportDeclaration(type, importDeclaration), AccStatic, null);
    final int off = newImportDecl.getSourceRange().getOffset();
    int start = 0, len = 0;
    if (newImportStr.endsWith(NEW_IMPORT)) {
        start = newImportDecl.getElementName().indexOf(NEW_IMPORT);
        len = NEW_IMPORT.length();
    } else {
        final int tmp = importDeclaration.getElementName().lastIndexOf(DOT_CHAR);
        final String fieldImp = importDeclaration.getElementName().substring(tmp + 1);
        start = newImportDecl.getElementName().lastIndexOf(DOT_CHAR) + 1;
        len = newImportDecl.getElementName().length() - start;
        if (!fieldImp.equals(selection.getText())) {
            final int diff = fieldImp.length() - selection.getText().length();
            start += diff;
            len -= diff;

        }
    }
    final ISelection newSelection = new TextSelection(off + 14 + start, len);
    this.editorPart.getEditorSite().getSelectionProvider().setSelection(newSelection);
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param fromType//w  w  w  .  j  a  v a2 s.  c o m
 * @param modTypeName
 * @param toType
 * @return
 * @throws Exception
 */
public static IType findTypeForImport(final IType fromType, final String typeName, final IType toType)
        throws Exception {
    String importName = null;
    final String modTypeName = typeName.replaceAll("\\[[\\[ \\]]*\\]", EMPTY_STR); // remove

    if (isNativeType(modTypeName)) {
        return null;
    }
    if (fromType.getCompilationUnit() == null || !fromType.getCompilationUnit().exists()
            || fromType.getCompilationUnit().getImports() == null) {
        return null;
    }

    for (final IImportDeclaration importDeclaration : fromType.getCompilationUnit().getImports()) {
        if (isStatic(importDeclaration.getFlags())) {
            continue;
        }
        importName = importDeclaration.getElementName();
        if (importName.endsWith(ASTERISK)) {
            importName = importName.replace(ASTERISK, modTypeName);
        }
        if (importName.endsWith(modTypeName)) {
            final IType imptType = toType.getCompilationUnit().getJavaProject().findType(importName);
            if (imptType != null && imptType.exists()) {
                return imptType;
            }
        }
    }
    // check if it is the same package as fromType
    final IType imptType = toType.getCompilationUnit().getJavaProject()
            .findType(fromType.getPackageFragment().getElementName() + DOT + typeName);
    if (imptType != null && imptType.exists()) {
        return imptType;
    }

    if (isJavaLangType(modTypeName, toType)) {
        return null;
    }
    // MessageDialog.openInformation(new Shell(), "Warning",
    // "Unable to find " + typeName +
    // " in the classpath, project build path may be incorrect.");
    throw new Exception(
            "Unable to find import for " + modTypeName + " Please correct the errors and try again");
}

From source file:org.grails.ide.eclipse.groovy.debug.core.evaluation.GroovyJDIEvaluator.java

License:Open Source License

private String createEvaluationSourceFromSnippet(String snippet, IJavaStackFrame frame) throws CoreException {
    StringBuffer sb = new StringBuffer();
    sb.append("/////start\n");

    IJavaReferenceType jdiType = frame.getReferenceType();
    IType iType = JavaDebugUtils.resolveType(jdiType);

    // could be a closure type that doesn't exist in source
    if (iType != null && !iType.exists() && iType.getParent().getElementType() == IJavaElement.TYPE) {
        iType = (IType) iType.getParent();
    }/*  w  ww.  ja  v a  2 s.  co m*/

    if (iType != null && !iType.isInterface()) {
        ITypeRoot root = iType.getTypeRoot();
        if (root instanceof ICompilationUnit) {
            // really, a GroovyCompilationUnit
            ICompilationUnit unit = (ICompilationUnit) root;

            // package statement
            IPackageDeclaration[] pDecls = unit.getPackageDeclarations();
            if (pDecls.length > 0) {
                sb.append("package " + pDecls[0].getElementName() + ";\n");
                packageName = pDecls[0].getElementName() + ".";
            } else {
                packageName = "";
            }

            // imports
            IImportContainer container = unit.getImportContainer();
            if (container != null && container.exists()) {
                IJavaElement[] children = container.getChildren();
                for (int j = 0; j < children.length; j++) {
                    IImportDeclaration importChild = (IImportDeclaration) children[j];
                    sb.append("import ");
                    if (Flags.isStatic(importChild.getFlags())) {
                        sb.append("static ");
                    }
                    sb.append(importChild.getElementName());
                    if (importChild.isOnDemand() && !(importChild.getElementName().endsWith(".*"))) {
                        sb.append(".*");
                    }
                    sb.append(";\n");
                }
            }

            // types...create stubs for the types just so that they can be instantiated and referenced
            IType[] allTypes = unit.getAllTypes();
            for (IType otherType : allTypes) {
                if (!otherType.equals(iType)) {
                    if (otherType.isInterface()) {
                        sb.append("interface ");
                    } else if (otherType.isAnnotation()) {
                        // probably don't need this
                        sb.append("@interface ");
                    } else if (otherType.isEnum()) {
                        sb.append("enum ");
                    } else {
                        sb.append("class ");
                    }

                    // use '$' so that inner classes can be remembered
                    String qualifiedTypeName = otherType.getFullyQualifiedName('$');
                    int dotIndex = qualifiedTypeName.lastIndexOf('.') + 1;
                    String simpleName = qualifiedTypeName.substring(dotIndex);
                    sb.append(simpleName + "{ }\n");
                }
            }
        }
    }

    sb.append(snippet);
    sb.append("\n/////end");
    return sb.toString();
}

From source file:org.jboss.tools.common.java.impl.ValueResolver.java

License:Open Source License

private static String getFullName(IType type, IImportDeclaration[] is, String name) throws CoreException {
    for (IImportDeclaration d : is) {
        String n = d.getElementName();
        if (n.equals(name) || n.endsWith("." + name)) {
            return n;
        }//from  ww  w . jav a2  s.co  m
        if (Flags.isStatic(d.getFlags()) && n.endsWith(".*")) {
            String typename = n.substring(0, n.length() - 2);
            IType t = EclipseJavaUtil.findType(type.getJavaProject(), typename);
            if (t != null && t.exists()) {
                IField f = EclipseJavaUtil.findField(t, name);
                if (f != null) {
                    return f.getDeclaringType().getFullyQualifiedName() + "." + name;
                }
            }

        }
    }
    return null;
}

From source file:projectdependencies.views.ViewContentProviderJdt.java

License:Open Source License

@SuppressWarnings("unchecked")
private Collection<IJavaProject> findProjectsForImport(IImportDeclaration importDecparam)
        throws JavaModelException {
    boolean isStatic = Flags.isStatic(importDecparam.getFlags());
    String importStr = importDecparam.getElementName();
    String[] parts = StringUtils.split(importStr, '.');

    int packageEndIndex = parts.length - (isStatic ? 2 : 1); // ignore class name and method name from import

    if (packageEndIndex <= 0) {
        System.out.println("Empty or default package: " + importStr);
        return null;
    }/* w  ww  .  jav a 2  s . com*/

    String subPkg = StringUtils.join(parts, ".", 0, packageEndIndex);

    if (pkgProjects.containsKey(subPkg))
        return pkgProjects.getCollection(subPkg);

    return null;
}