Example usage for org.eclipse.jdt.core.dom ArrayInitializer EXPRESSIONS_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom ArrayInitializer EXPRESSIONS_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ArrayInitializer EXPRESSIONS_PROPERTY.

Prototype

ChildListPropertyDescriptor EXPRESSIONS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom ArrayInitializer EXPRESSIONS_PROPERTY.

Click Source Link

Document

The "expressions" structural property of this node type (element type: Expression ).

Usage

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void addExpressionToArrayInitializer(ArrayInitializer arrayInitializer, Expression expression) {
    List list = (List) arrayInitializer.getStructuralProperty(ArrayInitializer.EXPRESSIONS_PROPERTY);
    list.add(expression);/* ww w . j a  v a  2 s  . co  m*/
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static void removeFromArrayInitializer(ArrayInitializer arrayInitializer, int position) {
    List list = (List) arrayInitializer.getStructuralProperty(ArrayInitializer.EXPRESSIONS_PROPERTY);
    list.remove(position);/*from   www  .j av  a 2 s  .  c  om*/
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static void removeFromArrayInitializer(ArrayInitializer arrayInitializer, Object object) {
    List list = (List) arrayInitializer.getStructuralProperty(ArrayInitializer.EXPRESSIONS_PROPERTY);
    list.remove(object);/* w  w  w . j av  a 2s.co  m*/
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static int arrayInitializerSize(ArrayInitializer arrayInitializer) {
    return ((List) arrayInitializer.getStructuralProperty(ArrayInitializer.EXPRESSIONS_PROPERTY)).size();
}

From source file:com.liferay.ide.project.core.modules.NewLiferayModuleProjectOpMethods.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void addProperties(File dest, List<String> properties) throws Exception {
    try {//w  w  w .  j  a v  a 2s .  c o m
        if (properties == null || properties.size() < 1) {
            return;
        }

        ASTParser parser = ASTParser.newParser(AST.JLS8);
        String readContents = FileUtil.readContents(dest, true);
        parser.setSource(readContents.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setResolveBindings(true);
        final CompilationUnit cu = (CompilationUnit) parser.createAST(new NullProgressMonitor());
        cu.recordModifications();
        Document document = new Document(new String(readContents));
        cu.accept(new ASTVisitor() {

            @Override
            public boolean visit(NormalAnnotation node) {
                if (node.getTypeName().getFullyQualifiedName().equals("Component")) {
                    ASTRewrite rewrite = ASTRewrite.create(cu.getAST());
                    AST ast = cu.getAST();
                    List<ASTNode> values = node.values();
                    boolean hasProperty = false;

                    for (ASTNode astNode : values) {
                        if (astNode instanceof MemberValuePair) {
                            MemberValuePair pairNode = (MemberValuePair) astNode;

                            if (pairNode.getName().getFullyQualifiedName().equals("property")) {
                                Expression express = pairNode.getValue();

                                if (express instanceof ArrayInitializer) {
                                    ListRewrite lrw = rewrite.getListRewrite(express,
                                            ArrayInitializer.EXPRESSIONS_PROPERTY);
                                    ArrayInitializer initializer = (ArrayInitializer) express;
                                    List<ASTNode> expressions = (List<ASTNode>) initializer.expressions();
                                    ASTNode propertyNode = null;

                                    for (int i = properties.size() - 1; i >= 0; i--) {
                                        StringLiteral stringLiteral = ast.newStringLiteral();
                                        stringLiteral.setLiteralValue(properties.get(i));

                                        if (expressions.size() > 0) {
                                            propertyNode = expressions.get(expressions.size() - 1);
                                            lrw.insertAfter(stringLiteral, propertyNode, null);
                                        } else {
                                            lrw.insertFirst(stringLiteral, null);
                                        }
                                    }
                                }
                                hasProperty = true;
                            }
                        }
                    }

                    if (hasProperty == false) {
                        ListRewrite clrw = rewrite.getListRewrite(node, NormalAnnotation.VALUES_PROPERTY);
                        ASTNode lastNode = values.get(values.size() - 1);

                        ArrayInitializer newArrayInitializer = ast.newArrayInitializer();
                        MemberValuePair propertyMemberValuePair = ast.newMemberValuePair();

                        propertyMemberValuePair.setName(ast.newSimpleName("property"));
                        propertyMemberValuePair.setValue(newArrayInitializer);

                        clrw.insertBefore(propertyMemberValuePair, lastNode, null);
                        ListRewrite newLrw = rewrite.getListRewrite(newArrayInitializer,
                                ArrayInitializer.EXPRESSIONS_PROPERTY);

                        for (String property : properties) {
                            StringLiteral stringLiteral = ast.newStringLiteral();
                            stringLiteral.setLiteralValue(property);
                            newLrw.insertAt(stringLiteral, 0, null);
                        }
                    }
                    try (FileOutputStream fos = new FileOutputStream(dest)) {
                        TextEdit edits = rewrite.rewriteAST(document, null);
                        edits.apply(document);
                        fos.write(document.get().getBytes());
                        fos.flush();
                    } catch (Exception e) {
                        ProjectCore.logError(e);
                    }
                }
                return super.visit(node);
            }
        });
    } catch (Exception e) {
        ProjectCore.logError("error when adding properties to " + dest.getAbsolutePath(), e);
    }
}

From source file:org.mapstruct.eclipse.internal.quickfix.fixes.AddIgnoreTargetMappingAnnotationQuickFix.java

License:Apache License

private ListRewrite getListForAddingMappingAnnotations(CompilationUnit unit, Collection<String> properties,
        AST ast, ASTRewrite rewrite, MethodDeclaration method) {

    // if there is already an @Mappings annotation, add the new @Mapping's there
    Annotation mappingsAnnotation = findAnnotation(method, MAPPINGS_FQ_NAME);
    if (mappingsAnnotation != null) {
        return rewrite.getListRewrite(((SingleMemberAnnotation) mappingsAnnotation).getValue(),
                ArrayInitializer.EXPRESSIONS_PROPERTY);
    }/*www . j av  a2s .c  o  m*/

    // if repeatable @Mapping's are supported, then add the annotations directly to the method
    if (supportsRepeatableMapping(unit)) {
        return rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
    }

    // if we only need to add one @Mapping and there is none, yet, then add the single annotation directly
    Annotation singleMappingAnnotation = findAnnotation(method, MAPPING_FQ_NAME);
    if (singleMappingAnnotation == null && properties.size() == 1) {
        return rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
    }

    // create a new @Mappings annotation and add the @Mapping's there
    ListRewrite mappingList = addNewMappingsAnnotation(unit, rewrite, ast, method);

    if (singleMappingAnnotation != null) {
        rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY).remove(singleMappingAnnotation,
                null);

        mappingList.insertFirst(singleMappingAnnotation, null);
    }
    return mappingList;
}

From source file:org.mapstruct.eclipse.internal.quickfix.fixes.AddIgnoreTargetMappingAnnotationQuickFix.java

License:Apache License

private ListRewrite addNewMappingsAnnotation(CompilationUnit unit, ASTRewrite rewrite, AST ast,
        MethodDeclaration method) {/*www .  j  a v  a  2s  .  com*/
    SingleMemberAnnotation mappings = ast.newSingleMemberAnnotation();
    mappings.setTypeName(ast.newName(MAPPINGS_SIMPLE_NAME));

    ArrayInitializer mappingArray = ast.newArrayInitializer();
    mappings.setValue(mappingArray);

    ListRewrite annotations = rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
    annotations.insertFirst(mappings, null);

    addImportIfRequired(unit, rewrite, MAPPINGS_FQ_NAME);

    return rewrite.getListRewrite(mappingArray, ArrayInitializer.EXPRESSIONS_PROPERTY);
}

From source file:org.springframework.ide.vscode.boot.java.utils.ASTUtils.java

License:Open Source License

/**
 * For case where a expression can be either a String or a array of Strings and
 * we are interested in the first element of the array. (I.e. typical case
 * when annotation attribute is of type String[] (because Java allows using a single
 * value as a convenient syntax for writing an array of length 1 in that case.
 *//*w w w  .  ja  va  2s  . c  o  m*/
public static Optional<String> getFirstString(Expression exp) {
    if (exp instanceof StringLiteral) {
        return Optional.ofNullable(getLiteralValue((StringLiteral) exp));
    } else if (exp instanceof ArrayInitializer) {
        ArrayInitializer array = (ArrayInitializer) exp;
        Object objs = array.getStructuralProperty(ArrayInitializer.EXPRESSIONS_PROPERTY);
        if (objs instanceof List) {
            List<?> list = (List<?>) objs;
            if (!list.isEmpty()) {
                Object firstObj = list.get(0);
                if (firstObj instanceof Expression) {
                    return getFirstString((Expression) firstObj);
                }
            }
        }
    }
    return Optional.empty();
}