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

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

Introduction

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

Prototype

@Override
String getElementName();

Source Link

Document

Returns the name that has been imported.

Usage

From source file:ar.com.fluxit.jqa.utils.JdtUtils.java

License:Open Source License

private static String getCommonTypeName(IImportDeclaration importDeclaration) {
    String importName = importDeclaration.getElementName();
    if (importName.startsWith("java.")) {
        return "java";
    } else {//www  . j av  a  2  s  .  c o m
        String[] splitImport = importName.split("\\.");
        String elementName = "";
        int end = Math.min(COMMON_TYPE_PACKAGE_DEEP, splitImport.length - 1);
        for (int i = 0; i < end; i++) {
            elementName += splitImport[i] + ".";
        }
        return elementName.substring(0, elementName.length() - 1);
    }
}

From source file:com.astamuse.asta4d.ide.eclipse.util.JdtUtils.java

License:Open Source License

public static String resolveClassName(String className, IType type) {
    if (className == null || type == null) {
        return className;
    }/*from w w w .ja  v  a  2s.  c o m*/
    // replace binary $ inner class name syntax with . for source level
    className = className.replace('$', '.');
    String dotClassName = new StringBuilder().append('.').append(className).toString();

    IProject project = type.getJavaProject().getProject();

    try {
        // Special handling for some well-know classes
        if (className.startsWith("java.lang") && getJavaType(project, className) != null) {
            return className;
        }

        // Check if the class is imported
        if (!type.isBinary()) {

            // Strip className to first segment to support ReflectionUtils.MethodCallback
            int ix = className.lastIndexOf('.');
            String firstClassNameSegment = className;
            if (ix > 0) {
                firstClassNameSegment = className.substring(0, ix);
            }

            // Iterate the imports
            for (IImportDeclaration importDeclaration : type.getCompilationUnit().getImports()) {
                String importName = importDeclaration.getElementName();
                // Wildcard imports -> check if the package + className is a valid type
                if (importDeclaration.isOnDemand()) {
                    String newClassName = new StringBuilder(importName.substring(0, importName.length() - 1))
                            .append(className).toString();
                    if (getJavaType(project, newClassName) != null) {
                        return newClassName;
                    }
                }
                // Concrete import matching .className at the end -> check if type exists
                else if (importName.endsWith(dotClassName) && getJavaType(project, importName) != null) {
                    return importName;
                }
                // Check if className is multi segmented (ReflectionUtils.MethodCallback)
                // -> check if the first segment
                else if (!className.equals(firstClassNameSegment)) {
                    if (importName.endsWith(firstClassNameSegment)) {
                        String newClassName = new StringBuilder(
                                importName.substring(0, importName.lastIndexOf('.') + 1)).append(className)
                                        .toString();
                        if (getJavaType(project, newClassName) != null) {
                            return newClassName;
                        }
                    }
                }
            }
        }

        // Check if the class is in the same package as the type
        String packageName = type.getPackageFragment().getElementName();
        String newClassName = new StringBuilder(packageName).append(dotClassName).toString();
        if (getJavaType(project, newClassName) != null) {
            return newClassName;
        }

        // Check if the className is sufficient (already fully-qualified)
        if (getJavaType(project, className) != null) {
            return className;
        }

        // Check if the class is coming from the java.lang
        newClassName = new StringBuilder("java.lang").append(dotClassName).toString();
        if (getJavaType(project, newClassName) != null) {
            return newClassName;
        }

        // Fall back to full blown resolution
        String[][] fullInter = type.resolveType(className);
        if (fullInter != null && fullInter.length > 0) {
            return fullInter[0][0] + "." + fullInter[0][1];
        }
    } catch (JavaModelException e) {
        // SpringCore.log(e);
    }

    return className;
}

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

License:Open Source License

protected Set<String> createPackageNameMask(ICompilationUnit target) throws JavaModelException {
    Set<String> mask = new HashSet<String>();
    //TODO: move to constants.
    String prefix = "java";

    IImportDeclaration[] impDecs = target.getImports();
    for (IImportDeclaration impDec : impDecs)
        if (!impDec.getElementName().startsWith(prefix))
            mask.add(impDec.getElementName());

    return mask;/* w  ww.ja  va2s. c  om*/
}

From source file:com.ecfeed.ui.common.JavaModelAnalyser.java

License:Open Source License

protected static IType getImportedVariableType(IMethod method, ILocalVariable var) {
    String variableTypeName = Signature.toString(var.getTypeSignature());
    try {/*from w  ww  .  j ava  2s.c  o  m*/
        for (IImportDeclaration importDeclaration : method.getDeclaringType().getCompilationUnit()
                .getImports()) {
            String qualifiedName;
            if (importDeclaration.isOnDemand() == false) {
                qualifiedName = importDeclaration.getElementName();
            } else {
                qualifiedName = importDeclaration.getElementName().replaceFirst("\\*", variableTypeName);
            }
            IType type = getIType(qualifiedName);
            if (type != null && type.getElementName().equals(variableTypeName)) {
                return type;
            }
        }
    } catch (JavaModelException e1) {
    }
    return null;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.CompilationUnitCreator.java

License:Open Source License

private boolean addImport(Set<String> names, List<String> entityNameList,
        IImportDeclaration[] importDeclarations, ImportsManager imports) {
    boolean addedImport = false;
    for (String name : names) {
        if (Signature.getTypeSignatureKind(name) == Signature.CLASS_TYPE_SIGNATURE) {
            if (entityNameList.contains(Signature.toString(name))) {
                addedImport = true;//ww  w . j a v a2s. c om
            } else {
                for (IImportDeclaration imp : importDeclarations) {
                    if (imp.getElementName().contains(Signature.toString(name))) {
                        imports.addImport(imp.getElementName());
                    }
                }
            }
        }
    }
    return addedImport;
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

private boolean addImport(List<String> names, List<String> entityNameList,
        IImportDeclaration[] importDeclarations, ImportsManager imports) {
    boolean addedImport = false;
    for (String name : names) {
        if (Signature.getTypeSignatureKind(name) == Signature.CLASS_TYPE_SIGNATURE) {
            if (entityNameList.contains(Signature.toString(name))) {
                addedImport = true;//from   w ww . j  a  v a 2s.c o  m
            } else {
                for (IImportDeclaration imp : importDeclarations) {
                    if (imp.getElementName().contains(Signature.toString(name))) {
                        imports.addImport(imp.getElementName());
                    }
                }
            }
        }
    }
    return addedImport;
}

From source file:com.google.gdt.eclipse.designer.gxt.databinding.DesignPageFactory.java

License:Open Source License

private static boolean isGWT(IDesignerEditor editor) {
    try {// www .j  a v  a2  s .  c  o m
        IImportDeclaration[] imports = editor.getCompilationUnit().getImports();
        for (IImportDeclaration importDeclaration : imports) {
            String elementName = importDeclaration.getElementName();
            if (elementName.startsWith("com.google.gwt") || elementName.startsWith("com.extjs.gxt")) {
                return true;
            }
        }
    } catch (Throwable e) {
    }
    return false;
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

private void fillImportMap(ICompilationUnit iUnit) {
    if (this.importMap == null) {
        this.importMap = new HashMap();
    }//from  w w w .j  a  v  a2 s.co m

    try {
        IImportDeclaration[] imports = iUnit.getImports();
        for (int i = 0; i < imports.length; i++) {
            IImportDeclaration declaration = imports[i];
            String importName = declaration.getElementName();
            this.importMap.put(importName.substring(importName.lastIndexOf(".") + 1), importName);
        }
    } catch (JavaModelException jme) {
        jme.printStackTrace();
    }
}

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

License:Open Source License

private void outputImports(IvyXmlWriter xw, IPackageFragment ipf) {
    try {/*from w ww.  jav a2s. c  o m*/
        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:es.bsc.servicess.ide.editors.CommonFormPage.java

License:Apache License

/** Get the fully qualified domain name of a compilation unit
 * @param cu Compilation unit/*ww  w .j ava  2  s  . co m*/
 * @param signature Class signature
 * @param project Service implementation project
 * @return Fully qualified domain name
 * @throws JavaModelException
 */
private static String getFQType(ICompilationUnit cu, String signature, IJavaProject project)
        throws JavaModelException {
    if (signature.equals(Signature.SIG_BOOLEAN) || signature.equals(Signature.SIG_BYTE)
            || signature.equals(Signature.SIG_CHAR) || signature.equals(Signature.SIG_DOUBLE)
            || signature.equals(Signature.SIG_FLOAT) || signature.equals(Signature.SIG_INT)
            || signature.equals(Signature.SIG_LONG) || signature.equals(Signature.SIG_SHORT)
            || signature.equals(Signature.SIG_VOID)) {
        return Signature.toString(signature);
    } else {
        String qualifier = Signature.getQualifier(signature);
        if (qualifier != null && qualifier.length() > 0) {
            return Signature.toString(signature);
        } else {
            String classname = Signature.getSignatureSimpleName(signature);
            IImportDeclaration[] imports = cu.getImports();
            for (IImportDeclaration imp : imports) {
                String name = imp.getElementName();
                if (name.endsWith(".*")) {
                    String fqclass = searchClassInPackages(project, name.substring(0, name.indexOf(".*")),
                            classname);
                    if (fqclass != null)
                        return fqclass;
                } else if (name.endsWith(classname)) {
                    return name;
                }
            }
            return "java.lang." + classname;
        }
    }
}