Search in sources :

Example 1 with FieldDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr in project kie-wb-common by kiegroup.

the class ParserTestUtil method createField.

public static FieldDescr createField(String[] modifiers, String name, String type, String initializer) {
    FieldDescr fieldDescr = new FieldDescr();
    if (modifiers != null) {
        fieldDescr.setModifiers(new ModifierListDescr());
        for (int i = 0; i < modifiers.length; i++) {
            fieldDescr.addModifier(new ModifierDescr(modifiers[i], -1, -1, -1, -1, modifiers[i]));
        }
    }
    if (NamingUtils.isPrimitiveTypeId(type)) {
        fieldDescr.setType(new TypeDescr());
        fieldDescr.getType().setPrimitiveType(new PrimitiveTypeDescr(type, -1, -1, -1, -1, type));
    } else {
        fieldDescr.setType(new TypeDescr());
        fieldDescr.getType().setClassOrInterfaceType(new ClassOrInterfaceTypeDescr(type, -1, -1, -1, -1));
    }
    VariableDeclarationDescr variableDecl = new VariableDeclarationDescr();
    variableDecl.setIdentifier(new IdentifierDescr(name, -1, -1, -1));
    fieldDescr.addVariableDeclaration(variableDecl);
    if (initializer != null) {
        variableDecl.setVariableInitializer(new VariableInitializerDescr(initializer, -1, -1, -1, -1, initializer));
    }
    fieldDescr.setEndSemiColon(new JavaTokenDescr(ElementDescriptor.ElementType.JAVA_SEMI_COLON, ";", -1, -1, -1, -1));
    return fieldDescr;
}
Also used : ModifierListDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ModifierListDescr) ClassOrInterfaceTypeDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ClassOrInterfaceTypeDescr) VariableDeclarationDescr(org.kie.workbench.common.services.datamodeller.parser.descr.VariableDeclarationDescr) VariableInitializerDescr(org.kie.workbench.common.services.datamodeller.parser.descr.VariableInitializerDescr) TypeDescr(org.kie.workbench.common.services.datamodeller.parser.descr.TypeDescr) PrimitiveTypeDescr(org.kie.workbench.common.services.datamodeller.parser.descr.PrimitiveTypeDescr) ClassOrInterfaceTypeDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ClassOrInterfaceTypeDescr) FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) PrimitiveTypeDescr(org.kie.workbench.common.services.datamodeller.parser.descr.PrimitiveTypeDescr) ModifierDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ModifierDescr) JavaTokenDescr(org.kie.workbench.common.services.datamodeller.parser.descr.JavaTokenDescr) IdentifierDescr(org.kie.workbench.common.services.datamodeller.parser.descr.IdentifierDescr)

Example 2 with FieldDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr in project kie-wb-common by kiegroup.

the class AnnotationParsing1Test method init.

private void init() {
    FieldDescr fieldDeclaration;
    VariableDeclarationDescr variableDecl;
    AnnotationDescr annotationDescr;
    // @TestAnnotation()
    // public int field1;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field1", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation", null, new String[][] {}));
    fieldDeclaration.addModifier(new ModifierDescr("public", -1, -1, -1, -1, "public"));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation
    // private int field2;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field2", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation", null, null));
    fieldDeclaration.addModifier(new ModifierDescr("private", -1, -1, -1, -1, "private"));
    expectedFields.add(fieldDeclaration);
    // public
    // @TestAnnotation1
    // static int field3;
    fieldDeclaration = ParserTestUtil.createField(new String[] { "public" }, "field3", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation1", null, null));
    fieldDeclaration.addModifier(new ModifierDescr("static", -1, -1, -1, -1, "static"));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation1("value")
    // int field4;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field4", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation1", "\"value\"", null));
    expectedFields.add(fieldDeclaration);
    // protected
    // @TestAnnotation1( value = "value")
    // int field5;
    fieldDeclaration = ParserTestUtil.createField(new String[] { "protected" }, "field5", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation1", null, new String[][] { { "value", "\"value\"" } }));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation2( method1 = "param1", method2 = "param2")
    // int field6;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field6", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation2", null, new String[][] { { "method1", "\"param1\"" }, { "method2", "\"param2\"" } }));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation2(method2 = "param2")
    // int field7;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field7", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation2", null, new String[][] { { "method2", "\"param2\"" } }));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation3( value = "value", method1 = "param1", method2 = "param2")
    // int field8;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field8", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation3", null, new String[][] { { "value", "\"value\"" }, { "method1", "\"param1\"" }, { "method2", "\"param2\"" } }));
    expectedFields.add(fieldDeclaration);
    // @TestAnnotation
    // @TestAnnotation1("value")
    // @TestAnnotation2( method1 = "param1", method2 = "param2")
    // @TestAnnotation3( value = "value", method1 = "param1", method2 = "param2" )
    // int field9;
    fieldDeclaration = ParserTestUtil.createField(new String[] {}, "field9", "int", null);
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation", null, null));
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation1", "\"value\"", null));
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation2", null, new String[][] { { "method1", "\"param1\"" }, { "method2", "\"param2\"" } }));
    fieldDeclaration.addAnnotation(ParserTestUtil.createAnnotation("TestAnnotation3", null, new String[][] { { "value", "\"value\"" }, { "method1", "\"param1\"" }, { "method2", "\"param2\"" } }));
    expectedFields.add(fieldDeclaration);
}
Also used : VariableDeclarationDescr(org.kie.workbench.common.services.datamodeller.parser.descr.VariableDeclarationDescr) FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) ModifierDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ModifierDescr) AnnotationDescr(org.kie.workbench.common.services.datamodeller.parser.descr.AnnotationDescr)

Example 3 with FieldDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr in project kie-wb-common by kiegroup.

the class JavaFileHandler1Test method testMethodRemoval.

@Test
public void testMethodRemoval() {
    try {
        ClassDescr classDescr = fileHandler.getFileDescr().getClassDescr();
        MethodDescr methodDescr = classDescr.getMethod("getField2");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[0], fileHandler.buildResult());
        methodDescr = classDescr.getMethod("setField1");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[1], fileHandler.buildResult());
        methodDescr = classDescr.getMethod("getField1");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[2], fileHandler.buildResult());
        FieldDescr fieldDescr = classDescr.getField("field12");
        assertNotNull(fieldDescr);
        boolean deleted = classDescr.removeField("field12");
        assertEquals(true, deleted);
        assertEquals(fileContents[3], fileHandler.buildResult());
        fieldDescr = DescriptorFactoryImpl.getInstance().createFieldDescr("public int field100 = 12;");
        StringBuilder indentStr = new StringBuilder(System.lineSeparator() + System.lineSeparator() + "    ");
        TextTokenElementDescr indent = new TextTokenElementDescr("", 0, indentStr.length() - 1, 1, 0);
        indent.setSourceBuffer(indentStr);
        classDescr.addField(fieldDescr);
        classDescr.getElements().addMemberBefore(fieldDescr, indent);
        assertEquals(fileContents[4], fileHandler.buildResult());
        methodDescr = DescriptorFactoryImpl.getInstance().createMethodDescr("public java.lang.String getAddress() { return null; }");
        indentStr = new StringBuilder(System.lineSeparator() + System.lineSeparator() + "    ");
        indent = new TextTokenElementDescr("", 0, indentStr.length() - 1, 1, 0);
        indent.setSourceBuffer(indentStr);
        classDescr.addMethod(methodDescr);
        classDescr.getElements().addMemberBefore(methodDescr, indent);
        assertEquals(fileContents[5], fileHandler.buildResult());
        /*

            TODO add more cases

            assertEquals(fileContents[0], fileHandler.build());


            fileHandler.deleteMethod("getField1");
            logger.debug(fileHandler.build());

            fileHandler.deleteField("setField2");
            logger.debug(fileHandler.build());

            fileHandler.deleteMethod("setField2");
            fileHandler.addField("\n\tprotected String surname = null;\n");
            fileHandler.deleteMethod("setField1");
            fileHandler.deleteField("field7");
            fileHandler.addMethod("\n\tpublic static final java.lang.String echo(String msg) {\n\t\treturn msg;\n\t}\n");
            fileHandler.deleteField("field8");
            fileHandler.addField("\n\tprotected int i = 0;\n");
            fileHandler.deleteField("field6");
            fileHandler.deleteField("field9");
            fileHandler.deleteField("field11");
            fileHandler.deleteField("field14");

            fileHandler.addValuePair("\n\tpublic String getUserName() {\n\t\treturn surname;\n\t}\n");
            */
        String result = fileHandler.buildResult();
    // logger.debug(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MethodDescr(org.kie.workbench.common.services.datamodeller.parser.descr.MethodDescr) FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) ClassDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ClassDescr) TextTokenElementDescr(org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr) Test(org.junit.Test)

Example 4 with FieldDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr in project kie-wb-common by kiegroup.

the class JavaFileHandler2Test method test.

@Test
public void test() {
    try {
        String result = fileHandler.buildResult();
        logger.debug(result);
        ClassDescr classDescr = fileHandler.getFileDescr().getClassDescr();
        FieldDescr field = DescriptorFactoryImpl.getInstance().createFieldDescr("\n\n\tpublic /*eso*/static   int value  = (2+4), otro=1  /**/ ; /**/");
        ParserUtil.setSourceBufferTMP(field, field.getSourceBuffer());
        ParserUtil.populateUnManagedElements(0, field.getSourceBuffer().length() - 1, field);
        ParserUtil.setSourceBufferTMP(field, field.getSourceBuffer());
        classDescr.addField(field);
        classDescr.addField(field);
        result = fileHandler.buildResult();
        logger.debug(result);
        classDescr.getElements().remove(field);
        classDescr.getElements().remove(field);
        result = fileHandler.buildResult();
        logger.debug(result);
        assertEquals(originalFileContent, result);
        int i = 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) ClassDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ClassDescr) Test(org.junit.Test)

Example 5 with FieldDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr in project kie-wb-common by kiegroup.

the class ParseFieldMode1Test method testFieldMode.

@Test
public void testFieldMode() {
    String sentence = "public int field1";
    try {
        JavaParser parser = JavaParserFactory.newParser(sentence, JavaParserBase.ParserMode.PARSE_FIELD);
        parser.fieldDeclaration();
        FieldDescr fieldDescr = parser.getFieldDescr();
        int i = 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) Test(org.junit.Test)

Aggregations

FieldDescr (org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr)5 Test (org.junit.Test)3 ClassDescr (org.kie.workbench.common.services.datamodeller.parser.descr.ClassDescr)2 ModifierDescr (org.kie.workbench.common.services.datamodeller.parser.descr.ModifierDescr)2 VariableDeclarationDescr (org.kie.workbench.common.services.datamodeller.parser.descr.VariableDeclarationDescr)2 AnnotationDescr (org.kie.workbench.common.services.datamodeller.parser.descr.AnnotationDescr)1 ClassOrInterfaceTypeDescr (org.kie.workbench.common.services.datamodeller.parser.descr.ClassOrInterfaceTypeDescr)1 IdentifierDescr (org.kie.workbench.common.services.datamodeller.parser.descr.IdentifierDescr)1 JavaTokenDescr (org.kie.workbench.common.services.datamodeller.parser.descr.JavaTokenDescr)1 MethodDescr (org.kie.workbench.common.services.datamodeller.parser.descr.MethodDescr)1 ModifierListDescr (org.kie.workbench.common.services.datamodeller.parser.descr.ModifierListDescr)1 PrimitiveTypeDescr (org.kie.workbench.common.services.datamodeller.parser.descr.PrimitiveTypeDescr)1 TextTokenElementDescr (org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr)1 TypeDescr (org.kie.workbench.common.services.datamodeller.parser.descr.TypeDescr)1 VariableInitializerDescr (org.kie.workbench.common.services.datamodeller.parser.descr.VariableInitializerDescr)1