Example usage for org.eclipse.jdt.internal.compiler.env NameEnvironmentAnswer isCompilationUnit

List of usage examples for org.eclipse.jdt.internal.compiler.env NameEnvironmentAnswer isCompilationUnit

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.env NameEnvironmentAnswer isCompilationUnit.

Prototype

public boolean isCompilationUnit() 

Source Link

Document

Answer whether the receiver contains the compilation unit which defines the type.

Usage

From source file:com.codenvy.ide.ext.java.SearchableEnvironmentTest.java

License:Open Source License

private String processAnswer(NameEnvironmentAnswer answer, IJavaProject project, INameEnvironment environment)
        throws JavaModelException {
    if (answer == null)
        return null;
    if (answer.isBinaryType()) {
        IBinaryType binaryType = answer.getBinaryType();
        return BinaryTypeConvector.toJsonBinaryType(binaryType);
    } else if (answer.isCompilationUnit()) {
        ICompilationUnit compilationUnit = answer.getCompilationUnit();
        return getSourceTypeInfo(project, environment, compilationUnit);
    } else if (answer.isSourceType()) {
        ISourceType[] sourceTypes = answer.getSourceTypes();
        if (sourceTypes.length == 1) {
            ISourceType sourceType = sourceTypes[0];
            SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) sourceType;
            IType handle = elementInfo.getHandle();
            org.eclipse.jdt.core.ICompilationUnit unit = handle.getCompilationUnit();
            return getSourceTypeInfo(project, environment, (ICompilationUnit) unit);
        }/*from ww  w  .j a v a2s  .c  o  m*/
    }
    return null;
}

From source file:org.eclipse.che.jdt.RestNameEnvironment.java

License:Open Source License

@GET
@Produces(MediaType.APPLICATION_JSON)//  w ww  .j a  v a 2  s .  c  o  m
@javax.ws.rs.Path("findTypeCompound")
public String findTypeCompound(@QueryParam("compoundTypeName") String compoundTypeName,
        @QueryParam("projectpath") String projectPath) {
    JavaProject javaProject = getJavaProject(projectPath);
    SearchableEnvironment environment = javaProject.getNameEnvironment();
    try {
        NameEnvironmentAnswer answer = environment.findType(getCharArrayFrom(compoundTypeName));
        if (answer == null && compoundTypeName.contains("$")) {
            String innerName = compoundTypeName.substring(compoundTypeName.indexOf('$') + 1,
                    compoundTypeName.length());
            compoundTypeName = compoundTypeName.substring(0, compoundTypeName.indexOf('$'));
            answer = environment.findType(getCharArrayFrom(compoundTypeName));
            if (!answer.isCompilationUnit())
                return null;
            ICompilationUnit compilationUnit = answer.getCompilationUnit();
            CompilationUnit result = getCompilationUnit(javaProject, environment, compilationUnit);
            AbstractTypeDeclaration o = (AbstractTypeDeclaration) result.types().get(0);
            ITypeBinding typeBinding = o.resolveBinding();

            for (ITypeBinding binding : typeBinding.getDeclaredTypes()) {
                if (binding.getBinaryName().endsWith(innerName)) {
                    typeBinding = binding;
                    break;
                }
            }
            Map<TypeBinding, ?> bindings = (Map<TypeBinding, ?>) result
                    .getProperty("compilerBindingsToASTBindings");
            SourceTypeBinding binding = null;
            for (Map.Entry<TypeBinding, ?> entry : bindings.entrySet()) {
                if (entry.getValue().equals(typeBinding)) {
                    binding = (SourceTypeBinding) entry.getKey();
                    break;
                }
            }
            return TypeBindingConvector.toJsonBinaryType(binding);
        }

        return processAnswer(answer, javaProject, environment);
    } catch (JavaModelException e) {
        LOG.debug("Can't parse class: ", e);
        throw new WebApplicationException();
    }
}