Java Code Examples for org.eclipse.jdt.core.dom.AST#newAST()
The following examples show how to use
org.eclipse.jdt.core.dom.AST#newAST() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 6 votes |
private static Type parsePreExprType(Expr expr, String operator){ AST ast = AST.newAST(AST.JLS8); switch(operator){ case "++": case "--": return ast.newPrimitiveType(PrimitiveType.INT); case "+": case "-": return expr.getType(); case "~": case "!": return ast.newPrimitiveType(PrimitiveType.BOOLEAN); default : return null; } }
Example 2
Source File: CodeBlock.java From SimFix with GNU General Public License v2.0 | 6 votes |
private InstanceofExpr visit(InstanceofExpression node) { int startLine = _cunit.getLineNumber(node.getStartPosition()); int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength()); InstanceofExpr instanceofExpr = new InstanceofExpr(startLine, endLine, node); Expr expression = (Expr) process(node.getLeftOperand()); expression.setParent(instanceofExpr); instanceofExpr.setExpression(expression); instanceofExpr.setInstanceType(node.getRightOperand()); AST ast = AST.newAST(AST.JLS8); Type exprType = ast.newPrimitiveType(PrimitiveType.BOOLEAN); instanceofExpr.setType(exprType); return instanceofExpr; }
Example 3
Source File: ReferencedClassesParserTest.java From BUILD_file_generator with Apache License 2.0 | 5 votes |
@Test public void testExtractTypeNameWithoutGenerics() { AST ast = AST.newAST(AST.JLS4); org.eclipse.jdt.core.dom.SimpleName entry = ast.newSimpleName("Entry"); ParameterizedType map = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Map"))); map.typeArguments().add(ast.newSimpleType(ast.newSimpleName("Foo"))); QualifiedType type = ast.newQualifiedType(map, entry); assertThat(type.toString()).isEqualTo("Map<Foo>.Entry"); assertThat(ReferencedClassesParser.extractTypeNameWithoutGenerics(type)).isEqualTo("Map.Entry"); }
Example 4
Source File: TryPurifyByException.java From SimFix with GNU General Public License v2.0 | 5 votes |
public Set<String> purify(){ if(_method != null && _method.getBody() != null){ AST ast = AST.newAST(AST.JLS8); _backupBody = (Block) ASTNode.copySubtree(ast, _method.getBody()); splitMethodCalls(); } return _purifiedTestCases; }
Example 5
Source File: Variable.java From SimFix with GNU General Public License v2.0 | 5 votes |
public Variable(Node node, String name, Type type) { super(node); _name = name; _type = type; if(_type == null){ AST ast = AST.newAST(AST.JLS8); _type = ast.newWildcardType(); } if(node != null){ _kind = node.getUseType(node); } else { _kind = USE_TYPE.USE_UNKNOWN; } }
Example 6
Source File: TypeParseVisitor.java From SimFix with GNU General Public License v2.0 | 5 votes |
public boolean visit(VariableDeclarationExpression node) { if(isAnonymousClass(node)){ return true; } for (Object o : node.fragments()) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) o; Type type = node.getType(); if(vdf.getExtraDimensions() > 0){ AST ast = AST.newAST(AST.JLS8); type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions()); } map.put(vdf.getName().toString(), type); } return true; }
Example 7
Source File: CodeBlock.java From SimFix with GNU General Public License v2.0 | 5 votes |
private BoolLiteral visit(BooleanLiteral node) { int startLine = _cunit.getLineNumber(node.getStartPosition()); int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength()); BoolLiteral literal = new BoolLiteral(startLine, endLine, node); literal.setValue(node.booleanValue()); AST ast = AST.newAST(AST.JLS8); Type type = ast.newPrimitiveType(PrimitiveType.BOOLEAN); literal.setType(type); return literal; }
Example 8
Source File: CodeBlock.java From SimFix with GNU General Public License v2.0 | 5 votes |
private CharLiteral visit(CharacterLiteral node) { int startLine = _cunit.getLineNumber(node.getStartPosition()); int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength()); CharLiteral charLiteral = new CharLiteral(startLine, endLine, node); charLiteral.setValue(node.charValue()); AST ast = AST.newAST(AST.JLS8); Type type = ast.newPrimitiveType(PrimitiveType.CHAR); charLiteral.setType(type); return charLiteral; }
Example 9
Source File: CodeBlock.java From SimFix with GNU General Public License v2.0 | 5 votes |
private StrLiteral visit(StringLiteral node) { int startLine = _cunit.getLineNumber(node.getStartPosition()); int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength()); StrLiteral literal = new StrLiteral(startLine, endLine, node); literal.setValue(node.getLiteralValue()); AST ast = AST.newAST(AST.JLS8); Type type = ast.newSimpleType(ast.newSimpleName("String")); literal.setType(type); return literal; }
Example 10
Source File: TypeParseVisitor.java From SimFix with GNU General Public License v2.0 | 5 votes |
public boolean visit(VariableDeclarationStatement node) { if(isAnonymousClass(node)){ return true; } for (Object o : node.fragments()) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) o; Type type = node.getType(); if(vdf.getExtraDimensions() > 0){ AST ast = AST.newAST(AST.JLS8); type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions()); } map.put(vdf.getName().toString(), type); } return true; }
Example 11
Source File: TypeParseVisitor.java From SimFix with GNU General Public License v2.0 | 5 votes |
public boolean visit(TypeDeclaration node) { Pair<String, String> clazzAndMethodName = NodeUtils.getTypeDecAndMethodDec(node.getName()); String clazz = clazzAndMethodName.getFirst(); AST ast = AST.newAST(AST.JLS8); Type type = ast.newSimpleType(ast.newSimpleName(clazz)); ProjectInfo.addFieldType(clazz, "THIS", type); Type suType = node.getSuperclassType(); if(suType != null){ ProjectInfo.addFieldType(clazz, "SUPER", suType); ProjectInfo.addSuperClass(clazz, suType.toString()); } List<Object> sInterfaces = node.superInterfaceTypes(); if(sInterfaces != null){ for(Object object : sInterfaces){ if(object instanceof Type){ Type interfaceType = (Type) object; ProjectInfo.addSuperInterface(clazz, interfaceType.toString()); } } } FieldDeclaration fields[] = node.getFields(); for (FieldDeclaration f : fields) { for (Object o : f.fragments()) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) o; Type tmpType = f.getType(); if(vdf.getExtraDimensions() > 0){ tmpType = ast.newArrayType((Type) ASTNode.copySubtree(ast, tmpType), vdf.getExtraDimensions()); } ProjectInfo.addFieldType(clazz, vdf.getName().toString(), tmpType); } } return true; }
Example 12
Source File: JavaASTFlattener.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@Override public boolean visit(final PrefixExpression node) { final Expression operand = node.getOperand(); PrefixExpression.Operator _operator = node.getOperator(); boolean _matched = false; if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) { _matched=true; } if (!_matched) { if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) { _matched=true; } } if (_matched) { if ((operand instanceof ArrayAccess)) { final String arrayName = this.computeArrayName(((ArrayAccess)operand)); StringConcatenation _builder = new StringConcatenation(); _builder.append("_tPreInx_"); _builder.append(arrayName); final String idxName = _builder.toString(); String op = "-"; PrefixExpression.Operator _operator_1 = node.getOperator(); boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT); if (_equals) { op = "+"; } StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("{val "); _builder_1.append(idxName); _builder_1.append("="); this.appendToBuffer(_builder_1.toString()); ((ArrayAccess)operand).getIndex().accept(this); StringConcatenation _builder_2 = new StringConcatenation(); _builder_2.append(" "); _builder_2.append("val "); _builder_2.append(idxName, " "); _builder_2.append("_res="); _builder_2.append(arrayName, " "); _builder_2.append(".get("); _builder_2.append(idxName, " "); _builder_2.append(")"); _builder_2.append(op, " "); _builder_2.append("1"); this.appendToBuffer(_builder_2.toString()); StringConcatenation _builder_3 = new StringConcatenation(); _builder_3.append(" "); _builder_3.append(arrayName, " "); _builder_3.append(".set("); _builder_3.append(idxName, " "); _builder_3.append(", "); _builder_3.append(idxName, " "); _builder_3.append("_res) "); _builder_3.append(idxName, " "); _builder_3.append("_res}"); this.appendToBuffer(_builder_3.toString()); return false; } else { final AST dummyAST = AST.newAST(node.getAST().apiLevel()); final Assignment assigment = dummyAST.newAssignment(); final InfixExpression infixOp = dummyAST.newInfixExpression(); ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand); infixOp.setLeftOperand(((Expression) _copySubtree)); PrefixExpression.Operator _operator_2 = node.getOperator(); boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT); if (_equals_1) { infixOp.setOperator(InfixExpression.Operator.MINUS); } else { infixOp.setOperator(InfixExpression.Operator.PLUS); } infixOp.setRightOperand(dummyAST.newNumberLiteral("1")); ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand); final Expression leftSide = ((Expression) _copySubtree_1); assigment.setLeftHandSide(leftSide); assigment.setRightHandSide(infixOp); this.appendToBuffer("{"); Type type = null; if ((operand instanceof SimpleName)) { type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName)operand)); } this.handleAssignment(assigment, leftSide, type); this.appendToBuffer("}"); return false; } } if (!_matched) { if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) { _matched=true; node.getOperand().accept(this); this.appendToBuffer(".bitwiseNot"); } } if (!_matched) { { this.appendToBuffer(node.getOperator().toString()); node.getOperand().accept(this); } } return false; }
Example 13
Source File: JavaASTFlattener.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@Override public boolean visit(final PostfixExpression node) { final AST dummyAST = AST.newAST(node.getAST().apiLevel()); final PostfixExpression.Operator pfOperator = node.getOperator(); Expression _operand = node.getOperand(); if ((_operand instanceof ArrayAccess)) { Expression _operand_1 = node.getOperand(); final ArrayAccess pfOperand = ((ArrayAccess) _operand_1); if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) || Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) { final String arrayName = this.computeArrayName(pfOperand); StringConcatenation _builder = new StringConcatenation(); _builder.append("_postIndx_"); _builder.append(arrayName); final String idxName = _builder.toString(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("_postVal_"); _builder_1.append(arrayName); final String tempVarName = _builder_1.toString(); StringConcatenation _builder_2 = new StringConcatenation(); _builder_2.append("{ var "); _builder_2.append(idxName); _builder_2.append("="); this.appendToBuffer(_builder_2.toString()); pfOperand.getIndex().accept(this); StringConcatenation _builder_3 = new StringConcatenation(); _builder_3.append(" "); _builder_3.append("var "); this.appendToBuffer(_builder_3.toString()); final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment(); varDeclaration.setName(dummyAST.newSimpleName(tempVarName)); ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand); final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree); arrayAccess.setIndex(dummyAST.newSimpleName(idxName)); varDeclaration.setInitializer(arrayAccess); varDeclaration.accept(this); final InfixExpression infixOp = dummyAST.newInfixExpression(); infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName)); PostfixExpression.Operator _operator = node.getOperator(); boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT); if (_equals) { infixOp.setOperator(InfixExpression.Operator.MINUS); } else { infixOp.setOperator(InfixExpression.Operator.PLUS); } infixOp.setRightOperand(dummyAST.newNumberLiteral("1")); final Assignment assigment = dummyAST.newAssignment(); ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand); final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1); writeArray.setIndex(dummyAST.newSimpleName(idxName)); assigment.setLeftHandSide(writeArray); ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp); assigment.setRightHandSide(((Expression) _copySubtree_2)); assigment.accept(this); StringConcatenation _builder_4 = new StringConcatenation(); String _xifexpression = null; boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node); if (_needsReturnValue) { _xifexpression = tempVarName; } _builder_4.append(_xifexpression); _builder_4.append(" }"); this.appendToBuffer(_builder_4.toString()); return false; } } node.getOperand().accept(this); this.appendToBuffer(pfOperator.toString()); return false; }
Example 14
Source File: Expr.java From SimFix with GNU General Public License v2.0 | 4 votes |
protected Expr(int startLine, int endLine, ASTNode node) { super(startLine, endLine, node, null); AST ast = AST.newAST(AST.JLS8); _exprType = ast.newWildcardType(); }
Example 15
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 4 votes |
private static Type parsePostExprType(Expr expr, String operator){ // ++/-- AST ast = AST.newAST(AST.JLS8); return ast.newPrimitiveType(PrimitiveType.INT); }
Example 16
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 4 votes |
public static Type parseExprType(Expr left, String operator, Expr right){ if(left == null){ return parsePreExprType(right, operator); } if(right == null){ return parsePostExprType(left, operator); } AST ast = AST.newAST(AST.JLS8); switch(operator){ case "*": case "/": case "+": case "-": Type type = union(left.getType(), right.getType()); if(type == null){ type = ast.newPrimitiveType(PrimitiveType.DOUBLE); } return type; case "%": case "<<": case ">>": case ">>>": case "^": case "&": case "|": return ast.newPrimitiveType(PrimitiveType.INT); case "<": case ">": case "<=": case ">=": case "==": case "!=": case "&&": case "||": return ast.newPrimitiveType(PrimitiveType.BOOLEAN); default : return null; } }
Example 17
Source File: Purification.java From SimFix with GNU General Public License v2.0 | 4 votes |
public boolean visit(MethodDeclaration node){ if(node.getName().getFullyQualifiedName().equals(_methodName)){ ASTNode parent = node.getParent(); while(parent != null){ if(parent instanceof TypeDeclaration){ break; } parent = parent.getParent(); } if(parent == null){ return false; } TypeDeclaration typeDeclaration = (TypeDeclaration) parent; Set<Integer> assertLines = analysis(node); if(assertLines.size() <= 1){ _purifiedTestCases.add(_clazz + "::" + _methodName); } else { int methodID = 1; for(Integer line : assertLines){ AST ast = AST.newAST(AST.JLS8); MethodDeclaration newMethod = ast.newMethodDeclaration(); String newName = _methodName + "_purify_" + methodID; methodID ++; newMethod.setName(ast.newSimpleName(newName)); newMethod.modifiers().addAll(ASTNode.copySubtrees(ast, node.modifiers())); if(node.thrownExceptionTypes().size() > 0){ newMethod.thrownExceptionTypes().addAll(ASTNode.copySubtrees(ast, node.thrownExceptionTypes())); } newMethod.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); List<ASTNode> result = new ArrayList<>(); for(int i = 0; i < line; i++){ if(assertLines.contains(i)){ continue; } result.add((ASTNode) node.getBody().statements().get(i)); } result.add((ASTNode) node.getBody().statements().get(line)); // cannot simply remove duplicate assignment since it may cause side-effect // result = simplify(result); Block body = ast.newBlock(); for(ASTNode astNode : result){ body.statements().add(ASTNode.copySubtree(ast, astNode)); } newMethod.setBody(body); typeDeclaration.bodyDeclarations().add(ASTNode.copySubtree(node.getAST(), newMethod)); _purifiedTestCases.add(_clazz + "::" + newName); } node.getBody().statements().clear(); } return false; } return true; }
Example 18
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 2 votes |
private static Type union(Type ty1, Type ty2){ if(ty1 == null){ return ty2; } else if(ty2 == null){ return ty1; } if(!ty1.isPrimitiveType() || !ty2.isPrimitiveType()){ return null; } String ty1String = ty1.toString().toLowerCase().replace("integer", "int"); String ty2String = ty2.toString().toLowerCase().replace("integer", "int"); AST ast = AST.newAST(AST.JLS8); if(ty1String.equals("double") || ty2String.equals("double")){ return ast.newPrimitiveType(PrimitiveType.DOUBLE); } else if(ty1String.equals("float") || ty2String.equals("float")){ return ast.newPrimitiveType(PrimitiveType.FLOAT); } else if(ty1String.equals("long") || ty2String.equals("long")){ return ast.newPrimitiveType(PrimitiveType.LONG); } else if(ty1String.equals("int") || ty2String.equals("int")){ return ast.newPrimitiveType(PrimitiveType.INT); } else if(ty1String.equals("short") || ty2String.equals("short")){ return ast.newPrimitiveType(PrimitiveType.SHORT); } else { return ast.newPrimitiveType(PrimitiveType.BYTE); } }