List of usage examples for org.eclipse.jdt.core.dom AST newArrayInitializer
public ArrayInitializer newArrayInitializer()
From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) { existing = annotation;/*from ww w.ja v a2 s .c om*/ break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); String local = importRewrite.addImport(FQCN_SUPPRESS_LINT); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression existingValue = existing.getValue(); if (existingValue instanceof StringLiteral) { StringLiteral stringLiteral = (StringLiteral) existingValue; if (mId.equals(stringLiteral.getLiteralValue())) { // Already contains the id return null; } // Create a new array initializer holding the old string plus the new id ArrayInitializer array = ast.newArrayInitializer(); StringLiteral old = ast.newStringLiteral(); old.setLiteralValue(stringLiteral.getLiteralValue()); array.expressions().add(old); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); array.expressions().add(value); rewriter.set(existing, VALUE_PROPERTY, array, null); } else if (existingValue instanceof ArrayInitializer) { // Existing array: just append the new string ArrayInitializer array = (ArrayInitializer) existingValue; List expressions = array.expressions(); if (expressions != null) { for (Object o : expressions) { if (o instanceof StringLiteral) { if (mId.equals(((StringLiteral) o).getLiteralValue())) { // Already contains the id return null; } } } } StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY); listRewrite.insertLast(value, null); } else { assert false : existingValue; return null; } } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); // Apply to the document MultiTextEdit edit = new MultiTextEdit(); // Create the edit to change the imports, only if // anything changed if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit, BodyDeclaration declaration) throws CoreException { List modifiers = declaration.modifiers(); SingleMemberAnnotation existing = null; for (Object o : modifiers) { if (o instanceof SingleMemberAnnotation) { SingleMemberAnnotation annotation = (SingleMemberAnnotation) o; String type = annotation.getTypeName().getFullyQualifiedName(); if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) { existing = annotation;//from w ww. j a v a 2s .c om break; } } } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); String local = importRewrite.addImport(FQCN_SUPPRESS_LINT); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { Expression existingValue = existing.getValue(); if (existingValue instanceof StringLiteral) { // Create a new array initializer holding the old string plus the new id ArrayInitializer array = ast.newArrayInitializer(); StringLiteral old = ast.newStringLiteral(); StringLiteral stringLiteral = (StringLiteral) existingValue; old.setLiteralValue(stringLiteral.getLiteralValue()); array.expressions().add(old); StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); array.expressions().add(value); rewriter.set(existing, VALUE_PROPERTY, array, null); } else if (existingValue instanceof ArrayInitializer) { // Existing array: just append the new string ArrayInitializer array = (ArrayInitializer) existingValue; StringLiteral value = ast.newStringLiteral(); value.setLiteralValue(mId); ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY); listRewrite.insertLast(value, null); } else { assert false : existingValue; return null; } } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); TextEdit annotationEdits = rewriter.rewriteAST(document, null); // Apply to the document MultiTextEdit edit = new MultiTextEdit(); // Create the edit to change the imports, only if // anything changed if (importEdits.hasChildren()) { edit.addChild(importEdits); } edit.addChild(annotationEdits); return edit; }
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 {// ww w. java 2 s .c om 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.decojer.cavaj.utils.Annotations.java
License:Open Source License
/** * Decompile annotation default value (value or default value literal). * * @param defaultValue//w w w. j a v a 2s .c o m * default value * @param context * context * @return expression AST Node */ @Nullable public static Expression decompileAnnotationDefaultValue(@Nullable final Object defaultValue, @Nonnull final Element context) { final AST ast = context.getCu().getAst(); if (defaultValue == null) { return null; } if (defaultValue instanceof A) { return decompileAnnotation((A) defaultValue, context); } // could be primitive array - use slow reflection if (defaultValue.getClass().isArray()) { final int size = Array.getLength(defaultValue); if (size == 1) { // single entry autoboxing return decompileAnnotationDefaultValue(Array.get(defaultValue, 0), context); } final ArrayInitializer arrayInitializer = ast.newArrayInitializer(); for (int i = 0; i < size; ++i) { final Expression expression = decompileAnnotationDefaultValue(Array.get(defaultValue, i), context); if (expression != null) { arrayInitializer.expressions().add(expression); } } return arrayInitializer; } if (defaultValue instanceof Boolean) { return ast.newBooleanLiteral((Boolean) defaultValue); } if (defaultValue instanceof Byte) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof Character) { final CharacterLiteral characterLiteral = ast.newCharacterLiteral(); characterLiteral.setCharValue((Character) defaultValue); return characterLiteral; } if (defaultValue instanceof T) { final TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(newType((T) defaultValue, context)); return typeLiteral; } if (defaultValue instanceof Double) { return ast.newNumberLiteral(defaultValue.toString() + 'D'); } if (defaultValue instanceof F) { final F f = (F) defaultValue; if (!f.isEnum()) { log.warn("Default value field must be enum!"); } return ast.newQualifiedName(newTypeName(f.getT(), context), newSimpleName(f.getName(), ast)); } if (defaultValue instanceof Float) { return ast.newNumberLiteral(defaultValue.toString() + 'F'); } if (defaultValue instanceof Integer) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof Long) { return ast.newNumberLiteral(defaultValue.toString() + 'L'); } if (defaultValue instanceof Short) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof String) { final StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue((String) defaultValue); return stringLiteral; } log.warn("Unknown member value type '" + defaultValue.getClass().getName() + "'!"); final StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(defaultValue.toString()); return stringLiteral; }
From source file:org.eclipse.jpt.common.core.internal.utility.jdt.StringArrayExpressionConverter.java
License:Open Source License
/** * This method is non-<code>private</code> so it can be called by * {@link AnnotationStringArrayExpressionConverter} *///from w w w . j a v a 2s . c o m @Override protected ArrayInitializer convertObject(String[] strings, AST ast) { if ((strings.length == 0) && this.removeArrayInitializerWhenEmpty) { return null; } ArrayInitializer arrayInitializer = ast.newArrayInitializer(); List<Expression> expressions = this.expressions(arrayInitializer); for (String string : strings) { expressions.add(this.elementConverter.convert(string, ast)); } return arrayInitializer; }
From source file:org.eclipse.jpt.common.core.tests.internal.utility.jdt.AnnotationTestCase.java
License:Open Source License
protected ArrayInitializer newArrayInitializer(AST ast, Expression... expressions) { ArrayInitializer arrayInitializer = ast.newArrayInitializer(); for (Expression expression : expressions) { arrayInitializer.expressions().add(expression); }/* w w w .java 2 s .com*/ return arrayInitializer; }
From source file:org.evosuite.testcarver.codegen.CodeGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) { //-- construct getField() call final MethodInvocation callMethodCall = ast.newMethodInvocation(); if (isConstructorCall) { callMethodCall.setName(ast.newSimpleName("newInstance")); } else {//from w w w . ja v a 2 s .c o m callMethodCall.setName(ast.newSimpleName("callMethod")); } StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(targetTypeName); callMethodCall.arguments().add(stringLiteral); // class name if (!isConstructorCall) { stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(methodName); callMethodCall.arguments().add(stringLiteral); // method name if (varName == null) { callMethodCall.arguments().add(ast.newNullLiteral()); // static call -> no receiver } else { callMethodCall.arguments().add(ast.newSimpleName(varName)); // receiver } } // method arguments ArrayCreation arrCreation = ast.newArrayCreation(); arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object")))); ArrayInitializer arrInit = ast.newArrayInitializer(); arrCreation.setInitializer(arrInit); callMethodCall.arguments().add(arrCreation); Integer arg; // is either an oid or null for (int i = 0; i < methodArgs.length; i++) { arg = (Integer) methodArgs[i]; if (arg == null) { arrInit.expressions().add(ast.newNullLiteral()); } else { arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg))); } } // paramTypes arrCreation = ast.newArrayCreation(); arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class")))); arrInit = ast.newArrayInitializer(); arrCreation.setInitializer(arrInit); callMethodCall.arguments().add(arrCreation); org.objectweb.asm.Type type; for (int i = 0; i < paramTypes.length; i++) { type = paramTypes[i]; if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Boolean")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Byte")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Character")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Double")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Float")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Integer")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Long")); arrInit.expressions().add(facc); } else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) { FieldAccess facc = ast.newFieldAccess(); facc.setName(ast.newSimpleName("TYPE")); facc.setExpression(ast.newSimpleName("Short")); arrInit.expressions().add(facc); } else { final TypeLiteral clazz = ast.newTypeLiteral(); clazz.setType(ast.newSimpleType(ast.newName(type.getClassName()))); arrInit.expressions().add(clazz); } } return callMethodCall; }
From source file:org.jboss.forge.roaster.model.impl.expressions.ArrayInitImpl.java
License:Open Source License
@Override public ArrayInitializer materialize(AST ast) { if (isMaterialized()) { return init; }//from w w w . j ava 2s .c o m this.init = ast.newArrayInitializer(); for (ExpressionSource<O, ArrayInit<O, P>, ?> src : elements) { this.init.expressions().add(wireAndGetExpression(src, this, ast)); } return init; }
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) {//w w w .j a va 2s. co m 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.eclipse.quickfix.jdt.proposals.AddExceptionHandlerCompletionProposal.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w. j a va2s . c om protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(methodDecl); ASTRewrite astRewrite = ASTRewrite.create(astRoot.getAST()); String importName = ExceptionHandler.class.getCanonicalName(); if (!ProposalCalculatorUtil.containsImport(getCompilationUnit(), importName)) { createImportRewrite(astRoot).addImport(importName); } AST ast = astRewrite.getAST(); Annotation annotation; if (exceptionNames.isEmpty()) { MarkerAnnotation mAnnotation = ast.newMarkerAnnotation(); annotation = mAnnotation; } else { SingleMemberAnnotation sAnnotation = ast.newSingleMemberAnnotation(); annotation = sAnnotation; Expression value; if (exceptionNames.size() == 1) { TypeLiteral typeLiteral = getTypeLiteral(exceptionNames.get(0), ast); value = typeLiteral; addLinkedPosition(astRewrite.track(typeLiteral.getType()), true, "ExceptionHandler"); } else { ArrayInitializer arrayInitializer = ast.newArrayInitializer(); List<Expression> expressions = arrayInitializer.expressions(); for (int i = 0; i < exceptionNames.size(); i++) { String exceptionName = exceptionNames.get(i); TypeLiteral typeLiteral = getTypeLiteral(exceptionName, ast); addLinkedPosition(astRewrite.track(typeLiteral.getType()), i == 0, "ExceptionHandler" + i); expressions.add(typeLiteral); } value = arrayInitializer; } sAnnotation.setValue(value); } SimpleName name = ast.newSimpleName("ExceptionHandler"); annotation.setTypeName(name); astRewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY).insertFirst(annotation, null); return astRewrite; }