use of org.eclipse.jdt.core.dom.ArrayInitializer in project che by eclipse.
the class CallInliner method computeRealArguments.
private void computeRealArguments() {
List<Expression> arguments = Invocations.getArguments(fInvocation);
Set<Expression> canNotInline = crossCheckArguments(arguments);
boolean needsVarargBoxing = needsVarargBoxing(arguments);
int varargIndex = fSourceProvider.getVarargIndex();
AST ast = fInvocation.getAST();
Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
Expression expression = arguments.get(i);
ParameterData parameter = fSourceProvider.getParameterData(i);
if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
realArguments[i] = expression;
} else {
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[i] = ast.newSimpleName(name);
VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
if (parameter.isFinal()) {
local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
}
fLocals.add(local);
}
}
if (needsVarargBoxing) {
ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[varargIndex] = ast.newSimpleName(name);
Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(name));
ArrayInitializer initializer = ast.newArrayInitializer();
for (int i = varargIndex; i < arguments.size(); i++) {
initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
}
fragment.setInitializer(initializer);
VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
decl.setType(type);
fLocals.add(decl);
}
fContext.compilationUnit = fCUnit;
fContext.arguments = realArguments;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final ArrayCreation node) {
ArrayType at = node.getType();
int dims = at.getDimensions();
if ((dims > 1)) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/* FIXME Only one dimensional arrays are supported. ");
_builder.append(node);
_builder.append("*/");
this.appendToBuffer(_builder.toString());
this.addProblem(node, "Only one dimension arrays are supported.");
return false;
}
ArrayInitializer _initializer = node.getInitializer();
boolean _tripleNotEquals = (_initializer != null);
if (_tripleNotEquals) {
if (this.fallBackStrategy) {
this.appendToBuffer("(");
}
node.getInitializer().accept(this);
if (this.fallBackStrategy) {
this.appendToBuffer(" as ");
at.accept(this);
this.appendToBuffer(")");
}
} else {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("new");
String _xifexpression = null;
boolean _isPrimitiveType = node.getType().getElementType().isPrimitiveType();
if (_isPrimitiveType) {
Type _elementType = node.getType().getElementType();
_xifexpression = StringExtensions.toFirstUpper(((PrimitiveType) _elementType).getPrimitiveTypeCode().toString());
}
_builder_1.append(_xifexpression);
_builder_1.append("ArrayOfSize(");
this.appendToBuffer(_builder_1.toString());
List _dimensions = node.dimensions();
((Expression[]) Conversions.unwrapArray(((Iterable<Expression>) _dimensions), Expression.class))[0].accept(this);
this.appendToBuffer(")");
}
return false;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project AutoRefactor by JnRouvignac.
the class CFGBuilder method addVariableAccess.
/**
* @return whether the current variable access can throw an exception.
*/
@SuppressWarnings("unchecked")
private boolean addVariableAccess(CFGBasicBlock basicBlock, Expression node, int flags, ThrowerBlocks throwers) {
if (node == null) {
return false;
}
switch(node.getNodeType()) {
case ARRAY_ACCESS:
ArrayAccess aa = (ArrayAccess) node;
addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
throwers.addThrow(aa, newException(node, "java.lang.ArrayIndexOutOfBoundsException"));
return true;
case ARRAY_CREATION:
ArrayCreation ac = (ArrayCreation) node;
boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
return acMightThrow1 || acMightThrow2;
case ARRAY_INITIALIZER:
ArrayInitializer ai = (ArrayInitializer) node;
return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
case ASSIGNMENT:
Assignment a = (Assignment) node;
boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), WRITE, throwers);
boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), READ, throwers);
return aMightThrow1 || aMightThrow2;
case BOOLEAN_LITERAL:
case CHARACTER_LITERAL:
case NULL_LITERAL:
case NUMBER_LITERAL:
case STRING_LITERAL:
case TYPE_LITERAL:
// nothing to do
return false;
case CAST_EXPRESSION:
CastExpression cae = (CastExpression) node;
return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
case CLASS_INSTANCE_CREATION:
ClassInstanceCreation cic = (ClassInstanceCreation) node;
addVariableAccess(basicBlock, cic.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, cic.arguments(), flags, throwers);
IMethodBinding cicBinding = cic.resolveConstructorBinding();
if (cicBinding != null) {
ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
throwers.addThrow(cic, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case CONDITIONAL_EXPRESSION:
ConditionalExpression coe = (ConditionalExpression) node;
boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
return mightThrow1 || mightThrow2 || mightThrow3;
case FIELD_ACCESS:
FieldAccess fa = (FieldAccess) node;
boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
basicBlock.addVariableAccess(new VariableAccess(fa, flags));
if (is(flags, READ)) {
throwers.addThrow(fa, newException(node, "java.lang.NullPointerException"));
mightThrow = true;
}
return mightThrow;
case INFIX_EXPRESSION:
InfixExpression ie = (InfixExpression) node;
boolean ieMightThrow1 = addVariableAccess(basicBlock, ie.getLeftOperand(), flags, throwers);
boolean ieMightThrow2 = addVariableAccess(basicBlock, ie.getRightOperand(), flags, throwers);
return ieMightThrow1 || ieMightThrow2;
case INSTANCEOF_EXPRESSION:
InstanceofExpression ioe = (InstanceofExpression) node;
return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
case METHOD_INVOCATION:
MethodInvocation mi = (MethodInvocation) node;
addVariableAccess(basicBlock, mi.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, mi.arguments(), flags, throwers);
IMethodBinding methodBinding = mi.resolveMethodBinding();
if (methodBinding != null) {
ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
throwers.addThrow(mi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case SIMPLE_NAME:
SimpleName sn = (SimpleName) node;
basicBlock.addVariableAccess(new VariableAccess(sn, flags));
if (is(flags, READ)) {
throwers.addThrow(sn, newException(node, "java.lang.NullPointerException"));
return true;
}
return false;
case QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) node;
basicBlock.addVariableAccess(new VariableAccess(qn, flags));
throwers.addThrow(qn, newException(node, "java.lang.NullPointerException"));
return true;
case PARENTHESIZED_EXPRESSION:
ParenthesizedExpression pe = (ParenthesizedExpression) node;
return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
case POSTFIX_EXPRESSION:
PostfixExpression poe = (PostfixExpression) node;
return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
case PREFIX_EXPRESSION:
PrefixExpression pre = (PrefixExpression) node;
return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
case SUPER_FIELD_ACCESS:
SuperFieldAccess sfa = (SuperFieldAccess) node;
boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
return sfaMightThrow1 || sfaMightThrow2;
case SUPER_METHOD_INVOCATION:
SuperMethodInvocation smi = (SuperMethodInvocation) node;
addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
addVariableAccess(basicBlock, smi.getName(), flags, throwers);
IMethodBinding sMethodBinding = smi.resolveMethodBinding();
if (sMethodBinding != null) {
ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
throwers.addThrow(smi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case THIS_EXPRESSION:
ThisExpression te = (ThisExpression) node;
// TODO JNR remember use of "this" here
return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
case VARIABLE_DECLARATION_EXPRESSION:
return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
default:
throw new NotImplementedException(node);
}
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project liferay-ide by liferay.
the class NewLiferayModuleProjectOpMethods method addProperties.
@SuppressWarnings("unchecked")
public static boolean addProperties(File dest, List<String> properties) throws Exception {
if (ListUtil.isEmpty(properties)) {
return false;
}
try {
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);
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) {
String qualifiedName = node.getTypeName().getFullyQualifiedName();
if (qualifiedName.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;
String fullQualifiedName = pairNode.getName().getFullyQualifiedName();
if (fullQualifiedName.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 = initializer.expressions();
ASTNode propertyNode = null;
for (int i = properties.size() - 1; i >= 0; i--) {
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(properties.get(i));
if (ListUtil.isNotEmpty(expressions)) {
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 (OutputStream fos = Files.newOutputStream(dest.toPath())) {
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);
}
});
return true;
} catch (Exception e) {
ProjectCore.logError("error when adding properties to " + dest.getAbsolutePath(), e);
return false;
}
}
Aggregations