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

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

Introduction

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

Prototype

public ICompilationUnit getCompilationUnit() 

Source Link

Document

Answer the compilation unit or null if the receiver represents a binary or source 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  w  w  w.  j a va2s  . com
    }
    return null;
}

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

License:Open Source License

@GET
@Produces(MediaType.APPLICATION_JSON)/*from w  w  w. j a v  a2s.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();
    }
}