/* * Visit the AST expression and get the ParseTree Expression. * This is used by the individual visits when parsing a tree. * It passes to the top method (createExpression), which can * handle the InvalidExpressionException. * * If any visit doesn't return an expression, then an invalid * expression exception will be thrown to indicate this. If the * incoming expression is <code>null</code>, then return of <code>null</code> is ok because * this would be for an optional expression which didn't exist. * * @return The new ParseTree Expression or <code>null</code> if incoming expression was null. * * @see createExpression(org.eclipse.jdt.core.dom.Expression) * @exception InvalidExpressionException * @since 1.0.0 */ protected final PTExpression perform(Expression astExpression) { if (astExpression != null) { expression = null; astExpression.accept(this); if (expression == null) throw new InvalidExpressionException( MessageFormat.format( WorkbenchUtilityMessages.ParseTreeCreationFromAST_ExpressionTooComplicated_EXC_, new Object[] {astExpression.toString()})); return expression; } else return null; // This is ok. It means an optional expression was being processed and the // expression didn't exist. }
/* (non-Javadoc) * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ArrayAccess) */ public boolean visit(ArrayAccess node) { PTArrayAccess aa = InstantiationFactory.eINSTANCE.createPTArrayAccess(); List indexes = aa.getIndexes(); Expression arrayExp = node; while (arrayExp.getNodeType() == ASTNode.ARRAY_ACCESS) { // Visit the index to get the index expression. ArrayAccess array = (ArrayAccess) arrayExp; indexes.add( 0, perform( array .getIndex())); // We're trying to create the final expression from inside out, the // indexes are created in reverse order. arrayExp = array.getArray(); } aa.setArray(perform(arrayExp)); // Final arrayExp is the true expression. expression = aa; // Set the return expression for this visit. return false; }
/** * Process the AST Expression and return a PTExpression. If any part was invalid, then only an * PTInvalidExpression will be returned. * * @param astExpression * @return The PTExpression. * @since 1.0.0 */ public final PTExpression createExpression(Expression astExpression) { try { return perform(astExpression); } catch (InvalidExpressionException e) { // Create a msg that is formed of the exception message and the full init string. String msg = MessageFormat.format( WorkbenchUtilityMessages.ParseTreeCreationFromAST_0, new Object[] {e.getLocalizedMessage(), astExpression.toString()}); PTInvalidExpression exp = InstantiationFactory.eINSTANCE.createPTInvalidExpression(); exp.setMessage(msg); return exp; } }