下面列出了怎么用org.eclipse.jdt.core.dom.PrefixExpression的API类实例代码及写法,或者点击链接到github查看源代码。
public static PrefixOperator getPrefixOperator(PrefixExpression.Operator operator) {
switch (operator.toString()) {
case "++":
return PrefixOperator.INCREMENT;
case "--":
return PrefixOperator.DECREMENT;
case "+":
return PrefixOperator.PLUS;
case "-":
return PrefixOperator.MINUS;
case "~":
return PrefixOperator.COMPLEMENT;
case "!":
return PrefixOperator.NOT;
default:
return null;
}
}
private PrefixExpr visit(PrefixExpression node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
PrefixExpr prefixExpr = new PrefixExpr(startLine, endLine, node);
Expr expression = (Expr) process(node.getOperand());
expression.setParent(prefixExpr);
prefixExpr.setExpression(expression);
prefixExpr.setOperator(node.getOperator());
Type type = NodeUtils.parseExprType(null, node.getOperator().toString(), expression);
prefixExpr.setType(type);
return prefixExpr;
}
private static boolean isLeftValue(ASTNode node) {
ASTNode parent = node.getParent();
if (parent instanceof Assignment) {
Assignment assignment = (Assignment) parent;
if (assignment.getLeftHandSide() == node) {
return true;
}
}
if (parent instanceof PostfixExpression) {
return true;
}
if (parent instanceof PrefixExpression) {
PrefixExpression.Operator op = ((PrefixExpression) parent).getOperator();
if (op.equals(PrefixExpression.Operator.DECREMENT)) {
return true;
}
if (op.equals(PrefixExpression.Operator.INCREMENT)) {
return true;
}
return false;
}
return false;
}
private static boolean isLeftValue(ASTNode node) {
ASTNode parent = node.getParent();
if (parent instanceof Assignment) {
Assignment assignment = (Assignment) parent;
if (assignment.getLeftHandSide() == node) {
return true;
}
}
if (parent instanceof PostfixExpression) {
return true;
}
if (parent instanceof PrefixExpression) {
PrefixExpression.Operator op = ((PrefixExpression) parent).getOperator();
if (op.equals(PrefixExpression.Operator.DECREMENT)) {
return true;
}
if (op.equals(PrefixExpression.Operator.INCREMENT)) {
return true;
}
return false;
}
return false;
}
@Override
public boolean visit(PrefixExpression node) {
Expression operand = node.getOperand();
if (!considerBinding(resolveBinding(operand), operand)) {
return true;
}
PrefixExpression.Operator operator = node.getOperator();
if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT) {
return true;
}
checkParent(node);
fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
return false;
}
@Override
public boolean visit(PrefixExpression prefixExpression) {
if (prefixExpression.getOperand() == null)
return true;
if (! (prefixExpression.getOperand() instanceof SimpleName))
return true;
if (! prefixExpression.getOperator().equals(Operator.DECREMENT) &&
! prefixExpression.getOperator().equals(Operator.INCREMENT))
return true;
SimpleName simpleName= (SimpleName)prefixExpression.getOperand();
if (! isNameReferenceToTemp(simpleName))
return true;
fFirstAssignment= prefixExpression;
return false;
}
private static boolean isLeftValue(ASTNode node) {
ASTNode parent= node.getParent();
if (parent instanceof Assignment) {
Assignment assignment= (Assignment) parent;
if (assignment.getLeftHandSide() == node)
return true;
}
if (parent instanceof PostfixExpression)
return true;
if (parent instanceof PrefixExpression) {
PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator();
if (op.equals(PrefixExpression.Operator.DECREMENT))
return true;
if (op.equals(PrefixExpression.Operator.INCREMENT))
return true;
return false;
}
return false;
}
@Override
public boolean visit(PrefixExpression node) {
Expression operand= node.getOperand();
if (!considerBinding(resolveBinding(operand), operand))
return true;
PrefixExpression.Operator operator= node.getOperator();
if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT)
return true;
checkParent(node);
fRewriter.replace(node,
createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()),
createGroupDescription(PREFIX_ACCESS));
return false;
}
private Statement createOuterComparison() {
MethodInvocation outer1= fAst.newMethodInvocation();
outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));
MethodInvocation outer2= fAst.newMethodInvocation();
outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));
outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED));
MethodInvocation outerEql= fAst.newMethodInvocation();
outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS));
outerEql.setExpression(outer1);
outerEql.arguments().add(outer2);
PrefixExpression not= fAst.newPrefixExpression();
not.setOperand(outerEql);
not.setOperator(PrefixExpression.Operator.NOT);
IfStatement notEqNull= fAst.newIfStatement();
notEqNull.setExpression(not);
notEqNull.setThenStatement(getThenStatement(getReturnFalse()));
return notEqNull;
}
private Statement createArrayComparison(String name) {
MethodInvocation invoc= fAst.newMethodInvocation();
invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS));
invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
invoc.arguments().add(getThisAccessForEquals(name));
invoc.arguments().add(getOtherAccess(name));
PrefixExpression pe= fAst.newPrefixExpression();
pe.setOperator(PrefixExpression.Operator.NOT);
pe.setOperand(invoc);
IfStatement ifSt= fAst.newIfStatement();
ifSt.setExpression(pe);
ifSt.setThenStatement(getThenStatement(getReturnFalse()));
return ifSt;
}
private Statement createMultiArrayComparison(String name) {
MethodInvocation invoc= fAst.newMethodInvocation();
invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_EQUALS));
invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
invoc.arguments().add(getThisAccessForEquals(name));
invoc.arguments().add(getOtherAccess(name));
PrefixExpression pe= fAst.newPrefixExpression();
pe.setOperator(PrefixExpression.Operator.NOT);
pe.setOperand(invoc);
IfStatement ifSt= fAst.newIfStatement();
ifSt.setExpression(pe);
ifSt.setThenStatement(getThenStatement(getReturnFalse()));
return ifSt;
}
private boolean isUpdated(Expression expr) {
if(expr.getParent() instanceof Assignment) {
Assignment assignment = (Assignment)expr.getParent();
if(assignment.getLeftHandSide().equals(expr)) {
return true;
}
}
else if(expr.getParent() instanceof PostfixExpression) {
return true;
}
else if(expr.getParent() instanceof PrefixExpression) {
PrefixExpression prefix = (PrefixExpression)expr.getParent();
if(prefix.getOperator().equals(PrefixExpression.Operator.INCREMENT) ||
prefix.getOperator().equals(PrefixExpression.Operator.DECREMENT)) {
return true;
}
}
return false;
}
protected boolean isTypeHolder(Object o) {
if(o.getClass().equals(MethodInvocation.class) || o.getClass().equals(SuperMethodInvocation.class)
|| o.getClass().equals(NumberLiteral.class) || o.getClass().equals(StringLiteral.class)
|| o.getClass().equals(CharacterLiteral.class) || o.getClass().equals(BooleanLiteral.class)
|| o.getClass().equals(TypeLiteral.class) || o.getClass().equals(NullLiteral.class)
|| o.getClass().equals(ArrayCreation.class)
|| o.getClass().equals(ClassInstanceCreation.class)
|| o.getClass().equals(ArrayAccess.class) || o.getClass().equals(FieldAccess.class)
|| o.getClass().equals(SuperFieldAccess.class) || o.getClass().equals(ParenthesizedExpression.class)
|| o.getClass().equals(SimpleName.class) || o.getClass().equals(QualifiedName.class)
|| o.getClass().equals(CastExpression.class) || o.getClass().equals(InfixExpression.class)
|| o.getClass().equals(PrefixExpression.class) || o.getClass().equals(InstanceofExpression.class)
|| o.getClass().equals(ThisExpression.class) || o.getClass().equals(ConditionalExpression.class))
return true;
return false;
}
public static boolean isUpdatingVariable(Expression updater, SimpleName variable)
{
if (updater instanceof PrefixExpression)
{
PrefixExpression prefixExpression = (PrefixExpression) updater;
return isSameVariable(prefixExpression.getOperand(), variable);
}
else if (updater instanceof PostfixExpression)
{
PostfixExpression postfixExpression = (PostfixExpression) updater;
return isSameVariable(postfixExpression.getOperand(), variable);
}
else if (updater instanceof Assignment)
{
Assignment assignment = (Assignment) updater;
return isSameVariable(assignment.getLeftHandSide(), variable);
}
else if (updater instanceof MethodInvocation)
{
MethodInvocation methodInvocation = (MethodInvocation) updater;
return isSameVariable(methodInvocation.getExpression(), variable);
}
return false;
}
public static Integer getUpdateValue(Expression updater)
{
if (updater instanceof PrefixExpression || updater instanceof PostfixExpression)
{
return AbstractLoopUtilities.getIncrementValue(updater);
}
else if (updater instanceof Assignment)
{
Assignment assignment = (Assignment) updater;
return assignmentUpdateValue(assignment);
}
else if (updater instanceof MethodInvocation)
{
MethodInvocation methodInvocation = (MethodInvocation) updater;
AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
return bindingInformation.getUpdateMethodValue(methodInvocation.resolveMethodBinding().getMethodDeclaration().getKey());
}
return null;
}
private static Integer getIncrementValue(Expression expression)
{
Integer incrementValue = null;
String operator = null;
if (expression instanceof PrefixExpression)
{
PrefixExpression prefixExpression = (PrefixExpression) expression;
operator = prefixExpression.getOperator().toString();
}
else if (expression instanceof PostfixExpression)
{
PostfixExpression postfixExpression = (PostfixExpression) expression;
operator = postfixExpression.getOperator().toString();
}
if (operator != null && operator.equals("++"))
{
incrementValue = 1;
}
else if (operator != null && operator.equals("--"))
{
incrementValue = (-1);
}
return incrementValue;
}
@Override
public void endVisit(PrefixExpression node) {
PrefixExpression.Operator op = node.getOperator();
if (PrefixExpression.Operator.INCREMENT.equals(op) || PrefixExpression.Operator.DECREMENT.equals(op)) {
endVisitIncDecOperation(node, node.getOperand());
} else {
assignFlowInfo(node, node.getOperand());
}
}
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
prefixExpression.setOperand(parenthesizedExpression);
return prefixExpression;
}
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
Expression expression = (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
protected Expression createNotEqualsExpression(ASTRewrite rewrite, InfixExpression stringEqualityCheck) {
Expression equalsExpression = createEqualsExpression(rewrite, stringEqualityCheck);
final AST ast = rewrite.getAST();
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
prefixExpression.setOperand(equalsExpression);
return prefixExpression;
}
public Iterable<Expression> findAssignmentsInBlock(final Block scope, final VariableDeclaration varDecl) {
final Function1<Expression, Boolean> _function = (Expression it) -> {
Expression name = null;
boolean _matched = false;
if (it instanceof Assignment) {
_matched=true;
name = ((Assignment)it).getLeftHandSide();
}
if (!_matched) {
if (it instanceof PrefixExpression) {
_matched=true;
name = ((PrefixExpression)it).getOperand();
}
}
if (!_matched) {
if (it instanceof PostfixExpression) {
_matched=true;
name = ((PostfixExpression)it).getOperand();
}
}
if ((name instanceof Name)) {
final IBinding binding = ((Name)name).resolveBinding();
if ((binding instanceof IVariableBinding)) {
final IVariableBinding declBinding = varDecl.resolveBinding();
return Boolean.valueOf((varDecl.getName().getIdentifier().equals(this.toSimpleName(((Name)name))) && ((IVariableBinding)binding).equals(declBinding)));
}
}
return Boolean.valueOf(false);
};
return this.findAssignmentsInBlock(scope, _function);
}
public Boolean isAssignedInBody(final Block scope, final SimpleName nameToLookFor) {
final Function1<Expression, Boolean> _function = (Expression it) -> {
Expression simpleName = null;
boolean _matched = false;
if (it instanceof Assignment) {
_matched=true;
simpleName = ((Assignment)it).getLeftHandSide();
}
if (!_matched) {
if (it instanceof PrefixExpression) {
_matched=true;
simpleName = ((PrefixExpression)it).getOperand();
}
}
if (!_matched) {
if (it instanceof PostfixExpression) {
_matched=true;
simpleName = ((PostfixExpression)it).getOperand();
}
}
if ((simpleName instanceof SimpleName)) {
return Boolean.valueOf(((simpleName != null) && nameToLookFor.getIdentifier().equals(((SimpleName)simpleName).getIdentifier())));
}
return Boolean.valueOf(false);
};
boolean _isEmpty = IterableExtensions.isEmpty(this.findAssignmentsInBlock(scope, _function));
return Boolean.valueOf((!_isEmpty));
}
public boolean visit(PrefixExpression node) {
prefixExpressions.add(node.toString());
if(current.getUserObject() != null) {
AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
anonymous.getPrefixExpressions().add(node.toString());
}
return super.visit(node);
}
private boolean labelMatch(ASTNode nodeM, ASTNode nodeN) {
if (nodeM.getNodeType() != nodeN.getNodeType())
return false;
if (nodeM instanceof Assignment)
return labelMatch((Assignment) nodeM, (Assignment) nodeN);
if (nodeM instanceof InfixExpression)
return labelMatch((InfixExpression) nodeM, (InfixExpression) nodeN);
if (nodeM instanceof PostfixExpression)
return labelMatch((PostfixExpression) nodeM, (PostfixExpression) nodeN);
if (nodeM instanceof PrefixExpression)
return labelMatch((PrefixExpression) nodeM, (PrefixExpression) nodeN);
return true;
}
public static char buildLabelForVector(ASTNode node) {
char label = (char) node.getNodeType();
if (node instanceof Expression) {
if (node.getClass().getSimpleName().endsWith("Literal")) {
return (char) (label | (node.toString().hashCode() << 7));
}
int type = node.getNodeType();
switch (type) {
case ASTNode.INFIX_EXPRESSION:
return (char) (label | (((InfixExpression) node).getOperator().toString().hashCode() << 7));
case ASTNode.SIMPLE_NAME:
return (char) (label | (node.toString().hashCode() << 7));
case ASTNode.POSTFIX_EXPRESSION:
return (char) (label | (((PostfixExpression) node).getOperator().toString().hashCode() << 7));
case ASTNode.PREFIX_EXPRESSION:
return (char) (label | (((PrefixExpression) node).getOperator().toString().hashCode() << 7));
default:
break;
}
} else if (node instanceof Modifier) {
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof Type) {
if (node instanceof PrimitiveType)
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof TextElement) {
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof TagElement) {
String tag = ((TagElement) node).getTagName();
if (tag != null)
return (char) (label | (((TagElement) node).getTagName().hashCode() << 7));
}
return label;
}
public static String buildASTLabel(ASTNode node) {
String label = node.getClass().getSimpleName();
if (node instanceof Expression) {
if (node.getClass().getSimpleName().endsWith("Literal")) {
return label + "(" + node.toString() + ")";
}
int type = node.getNodeType();
switch (type) {
case ASTNode.INFIX_EXPRESSION:
return label + "(" + ((InfixExpression) node).getOperator().toString() + ")";
case ASTNode.SIMPLE_NAME:
return label + "(" + node.toString() + ")";
case ASTNode.POSTFIX_EXPRESSION:
return label + "(" + ((PostfixExpression) node).getOperator().toString() + ")";
case ASTNode.PREFIX_EXPRESSION:
return label + "(" + ((PrefixExpression) node).getOperator().toString() + ")";
default:
break;
}
} else if (node instanceof Modifier) {
return label + "(" + node.toString() + ")";
} else if (node instanceof Type) {
if (node instanceof PrimitiveType)
return label + "(" + node.toString() + ")";
} else if (node instanceof TextElement) {
return label + "(" + node.toString() + ")";
} else if (node instanceof TagElement) {
String tag = ((TagElement) node).getTagName();
if (tag == null)
return label;
return label + "(" + tag + ")";
}
return label;
}
@Override
public final boolean visit(final PrefixExpression node) {
final IVariableBinding binding= getFieldBinding(node.getOperand());
if (binding != null)
fWritten.add(binding.getKey());
return false;
}
@Override
public void endVisit(PrefixExpression node) {
PrefixExpression.Operator op= node.getOperator();
if (PrefixExpression.Operator.INCREMENT.equals(op) || PrefixExpression.Operator.DECREMENT.equals(op)) {
endVisitIncDecOperation(node, node.getOperand());
} else {
assignFlowInfo(node, node.getOperand());
}
}
/**
* Returns the precedence of the expression. Expression
* with higher precedence are executed before expressions
* with lower precedence.
* i.e. in:
* <br><code> int a= ++3--;</code></br>
*
* the precedence order is
* <ul>
* <li>3</li>
* <li>++</li>
* <li>--</li>
* <li>=</li>
* </ul>
* 1. 3 -(++)-> 4<br>
* 2. 4 -(--)-> 3<br>
* 3. 3 -(=)-> a<br>
*
* @param expression the expression to determine the precedence for
* @return the precedence the higher to stronger the binding to its operand(s)
*/
public static int getExpressionPrecedence(Expression expression) {
if (expression instanceof InfixExpression) {
return getOperatorPrecedence(((InfixExpression)expression).getOperator());
} else if (expression instanceof Assignment) {
return ASSIGNMENT;
} else if (expression instanceof ConditionalExpression) {
return CONDITIONAL;
} else if (expression instanceof InstanceofExpression) {
return RELATIONAL;
} else if (expression instanceof CastExpression) {
return TYPEGENERATION;
} else if (expression instanceof ClassInstanceCreation) {
return POSTFIX;
} else if (expression instanceof PrefixExpression) {
return PREFIX;
} else if (expression instanceof FieldAccess) {
return POSTFIX;
} else if (expression instanceof MethodInvocation) {
return POSTFIX;
} else if (expression instanceof ArrayAccess) {
return POSTFIX;
} else if (expression instanceof PostfixExpression) {
return POSTFIX;
}
return Integer.MAX_VALUE;
}
@Override
public boolean visit(PrefixExpression node) {
Object operator= node.getOperator();
if (operator == PrefixExpression.Operator.INCREMENT || operator == PrefixExpression.Operator.DECREMENT) {
fSideEffectNodes.add(node);
return false;
}
return true;
}