Search in sources :

Example 1 with IJavaEntity

use of org.whole.lang.java.model.IJavaEntity in project whole by wholeplatform.

the class JDTJavaSourcePersistenceKit method doReadModel.

@Override
protected IEntity doReadModel(IPersistenceProvider pp) throws Exception {
    String fileStr = StringUtils.readAsString(pp.getInputStream(), pp.getEncoding());
    IBindingManager bm = pp.getBindings();
    ASTNode ast = null;
    // TODO remove when parse/unparse are added
    if (bm.wIsSet("parseFragments") && bm.wBooleanValue("parseFragments")) {
        IJavaEntity entity = JDTTransformerVisitor.transform(fileStr, JDTUtils.parse(fileStr));
        if (Matcher.matchImpl(JavaEntityDescriptorEnum.ClassDeclaration, entity))
            bm.wDef("syntheticRoot", ((ClassDeclaration) entity).getBodyDeclarations());
        else if (Matcher.matchImpl(JavaEntityDescriptorEnum.Block, entity))
            bm.wDef("syntheticRoot", entity);
        return entity;
    } else {
        if (bm.wIsSet("entityURI")) {
            String entityURI = bm.wStringValue("entityURI");
            if (ResourceUtils.hasFragmentPart(entityURI)) {
                EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.getEntityDescriptor(entityURI, true, bm);
                if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Expression)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.EXPRESSION);
                } else if (ed.equals(JavaEntityDescriptorEnum.Block)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.STATEMENTS);
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Statement)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.STATEMENTS);
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.BodyDeclaration)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.CLASS_BODY_DECLARATIONS);
                } else if (ed.equals(JavaEntityDescriptorEnum.BodyDeclarations)) {
                    ast = JDTUtils.parseAs(fileStr, JAVA_FRAGMENT.CLASS_BODY_DECLARATIONS);
                } else if (ed.equals(JavaEntityDescriptorEnum.CompilationUnit))
                    ast = JDTUtils.parseAsCompilationUnit(fileStr);
            }
        }
        if (ast == null)
            ast = JDTUtils.parse(fileStr);
        IEntity result = JDTTransformerVisitor.transform(fileStr, ast);
        if (bm.wIsSet("entityURI")) {
            String entityURI = bm.wStringValue("entityURI");
            if (ResourceUtils.hasFragmentPart(entityURI)) {
                EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.getEntityDescriptor(entityURI, true, bm);
                if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Expression)) {
                    if (!result.wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                } else if (!ed.equals(JavaEntityDescriptorEnum.Block) && ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.Statement)) {
                    if (result.wSize() != 1 || !(result = result.wGet(0)).wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                } else if (ed.isLanguageSubtypeOf(JavaEntityDescriptorEnum.BodyDeclaration)) {
                    if (result.wSize() != 1 || !(result = result.wGet(0)).wGetEntityDescriptor().isLanguageSubtypeOf(ed))
                        throw new IllegalArgumentException("");
                }
            } else if (result.wGetEntityDescriptor().equals(JavaEntityDescriptorEnum.Block) && result.wSize() == 1)
                result = result.wGet(0);
        }
        return result;
    }
}
Also used : ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) IEntity(org.whole.lang.model.IEntity) IJavaEntity(org.whole.lang.java.model.IJavaEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 ClassDeclaration (org.whole.lang.java.model.ClassDeclaration)1 IJavaEntity (org.whole.lang.java.model.IJavaEntity)1 IEntity (org.whole.lang.model.IEntity)1