Search in sources :

Example 1 with ClassDeclaration

use of org.whole.lang.java.model.ClassDeclaration 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)

Example 2 with ClassDeclaration

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

the class ControlQueriesTest method testDo1.

@Test
public void testDo1() {
    Model model = new XmlModel().create();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("do1");
    IEntity result = BehaviorUtils.evaluateFirstResult(query, model);
    assertTrue(result instanceof ClassDeclaration);
    assertEquals("HelloWorld", ((ClassDeclaration) result).getName().getValue());
}
Also used : ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) PathExpression(org.whole.lang.queries.model.PathExpression) IEntity(org.whole.lang.model.IEntity) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) XmlModel(org.whole.lang.models.codebase.XmlModel) ITemplateManager(org.whole.lang.templates.ITemplateManager) Test(org.junit.Test)

Example 3 with ClassDeclaration

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

the class ControlQueriesTest method testIf2.

@Test
public void testIf2() {
    Model model = new XmlModel().create();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("if2");
    IEntity result = BehaviorUtils.evaluateFirstResult(query, model);
    assertTrue(result instanceof ClassDeclaration);
    assertEquals(model.getName().getValue(), ((ClassDeclaration) result).getName().getValue());
}
Also used : ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) PathExpression(org.whole.lang.queries.model.PathExpression) IEntity(org.whole.lang.model.IEntity) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) XmlModel(org.whole.lang.models.codebase.XmlModel) ITemplateManager(org.whole.lang.templates.ITemplateManager) Test(org.junit.Test)

Example 4 with ClassDeclaration

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

the class ControlQueriesTest method testChoose4.

@Test
public void testChoose4() {
    Model model = new ModelsModel().create();
    ModelDeclarations decls = model.getDeclarations();
    IBindingManager bm = BindingManagerFactory.instance.createBindingManager();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("choose4");
    int i = 0;
    IEntityIterator<IEntity> iterator = BehaviorUtils.compileAndLazyEvaluate(query, model, bm);
    while (iterator.hasNext()) {
        IEntity result = iterator.next();
        assertSame(result, bm.wGet("type"));
        assertTrue(result instanceof ClassDeclaration);
        ClassDeclaration classDecl = (ClassDeclaration) result;
        IEntity decl = decls.wGet(i++);
        String name = "", superClassName = "";
        switch(decl.wGetEntityOrd()) {
            case ModelsEntityDescriptorEnum.SimpleEntity_ord:
                SimpleEntity simpleEntity = (SimpleEntity) decl;
                name = simpleEntity.getName().wStringValue();
                superClassName = "AbstractSimpleEntity";
                break;
            case ModelsEntityDescriptorEnum.CompositeEntity_ord:
                CompositeEntity compositeEntity = (CompositeEntity) decl;
                name = compositeEntity.getName().wStringValue();
                superClassName = "AbstractCompositeEntity";
                break;
            case ModelsEntityDescriptorEnum.DataEntity_ord:
                DataEntity dataEntity = (DataEntity) decl;
                name = dataEntity.getName().wStringValue();
                superClassName = "AbstractDataEntity";
                break;
            case ModelsEntityDescriptorEnum.EnumEntity_ord:
                EnumEntity enumEntity = (EnumEntity) decl;
                name = enumEntity.getName().wStringValue();
                superClassName = "AbstractEnumEntity";
                break;
        }
        assertEquals(name, classDecl.getName().wStringValue());
        assertEquals(superClassName, classDecl.getSuperclassType().wStringValue());
    }
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) IEntity(org.whole.lang.model.IEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ITemplateManager(org.whole.lang.templates.ITemplateManager) ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) ModelDeclarations(org.whole.lang.models.model.ModelDeclarations) PathExpression(org.whole.lang.queries.model.PathExpression) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) IBindingManager(org.whole.lang.bindings.IBindingManager) CompositeEntity(org.whole.lang.models.model.CompositeEntity) DataEntity(org.whole.lang.models.model.DataEntity) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Test(org.junit.Test)

Example 5 with ClassDeclaration

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

the class ControlQueriesTest method testIf3.

@Test
public void testIf3() {
    Model model = new XmlModel().create();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("if3");
    IEntity result = BehaviorUtils.evaluateFirstResult(query, model);
    assertTrue(result instanceof ClassDeclaration);
    assertEquals(StringUtils.toUpperCap(model.getName().getValue()), ((ClassDeclaration) result).getName().getValue());
}
Also used : ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) PathExpression(org.whole.lang.queries.model.PathExpression) IEntity(org.whole.lang.model.IEntity) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) XmlModel(org.whole.lang.models.codebase.XmlModel) ITemplateManager(org.whole.lang.templates.ITemplateManager) Test(org.junit.Test)

Aggregations

ClassDeclaration (org.whole.lang.java.model.ClassDeclaration)15 Test (org.junit.Test)12 XmlModel (org.whole.lang.models.codebase.XmlModel)11 Model (org.whole.lang.models.model.Model)11 PathExpression (org.whole.lang.queries.model.PathExpression)11 ITemplateManager (org.whole.lang.templates.ITemplateManager)11 IEntity (org.whole.lang.model.IEntity)7 IBindingManager (org.whole.lang.bindings.IBindingManager)4 ModelsModel (org.whole.lang.models.codebase.ModelsModel)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 SimpleEntity (org.whole.lang.models.model.SimpleEntity)2 ArrayList (java.util.ArrayList)1 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 BodyDeclarations (org.whole.lang.java.model.BodyDeclarations)1 FieldDeclaration (org.whole.lang.java.model.FieldDeclaration)1 IJavaEntity (org.whole.lang.java.model.IJavaEntity)1 InterfaceDeclaration (org.whole.lang.java.model.InterfaceDeclaration)1 MethodDeclaration (org.whole.lang.java.model.MethodDeclaration)1 MethodInvocation (org.whole.lang.java.model.MethodInvocation)1