Example usage for org.eclipse.jdt.core.dom Expression getLocationInParent

List of usage examples for org.eclipse.jdt.core.dom Expression getLocationInParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Expression getLocationInParent.

Prototype

public final StructuralPropertyDescriptor getLocationInParent() 

Source Link

Document

Returns the location of this node within its parent, or null if this is a root node.

Usage

From source file:org.autorefactor.refactoring.rules.VectorOldToNewAPIRefactoring.java

License:Open Source License

private boolean replaceWithAndSwapArguments(MethodInvocation node, String newMethodName) {
    final List<Expression> args = arguments(node);
    assertSize(args, 2);//from  w  w w  .  j a va 2 s  .  co m
    final Expression arg1 = args.get(1);

    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    r.set(node, NAME_PROPERTY, b.simpleName(newMethodName));
    r.insertAt(b.move(arg1), 0, arg1.getLocationInParent(), arg1.getParent());
    return DO_NOT_VISIT_SUBTREE;
}

From source file:org.eclipse.wb.internal.core.eval.evaluators.SimpleNameEvaluator.java

License:Open Source License

private Object evaluateAsAssignment(EvaluationContext context, SimpleName simpleName) throws Exception {
    ExpressionValue value = ExecutionFlowUtils2.getValue(context.getFlowDescription(), simpleName);
    if (value != null) {
        Expression expression = value.getExpression();
        // field declaration without initializer
        if (expression.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY
                && expression.getParent().getLocationInParent() == FieldDeclaration.FRAGMENTS_PROPERTY) {
            return null;
        }//w w w  . j a v a  2  s .  c  om
        // parameter of method without invocation
        if (expression.getLocationInParent() == SingleVariableDeclaration.NAME_PROPERTY) {
            SingleVariableDeclaration parameter = (SingleVariableDeclaration) expression.getParent();
            MethodDeclaration methodDeclaration = AstNodeUtils.getEnclosingMethod(parameter);
            return context.evaluateUnknownParameter(methodDeclaration, parameter);
        }
        // normal Expression
        return AstEvaluationEngine.evaluate(context, expression);
    }
    // no value
    throw new DesignerException(ICoreExceptionConstants.EVAL_NO_SIMPLE_NAME_FOUND, simpleName.getIdentifier());
}

From source file:org.eclipse.wb.internal.core.model.util.factory.FactoryApplyAction.java

License:Open Source License

/**
 * @param oldArguments/*from www  .  j av a 2s  .  com*/
 *          the arguments of old {@link ClassInstanceCreation} or {@link MethodInvocation} that
 *          was used to create component.
 * 
 * @return the arguments for factory method invocation.
 */
private List<String> getFactoryArguments(List<Expression> oldArguments) throws Exception {
    List<String> arguments = Lists.newArrayList();
    for (ParameterDescription parameter : m_description.getParameters()) {
        // check for "parent"
        if (parameter.isParent()) {
            m_hasParentArgument = true;
            arguments.add(TemplateUtils.getExpression(m_component.getParentJava()));
            continue;
        }
        // try to use source from bound property
        {
            GenericPropertyImpl property = getGenericProperty(parameter.getProperty());
            if (property != null) {
                Expression expression = ((GenericProperty) property).getExpression();
                // argument of creation
                if (oldArguments.contains(expression)) {
                    arguments.add(m_editor.getSource(expression));
                    continue;
                }
                // argument of separate method (only literal)
                if (expression.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY
                        && (expression instanceof NullLiteral || expression instanceof BooleanLiteral
                                || expression instanceof NumberLiteral
                                || expression instanceof StringLiteral)) {
                    arguments.add(m_editor.getSource(expression));
                    m_editor.removeEnclosingStatement(expression);
                    continue;
                }
            }
        }
        // default source
        arguments.add(parameter.getDefaultSource());
    }
    return arguments;
}

From source file:org.eclipse.wb.internal.core.parser.AbstractParseFactory.java

License:Open Source License

public JavaInfo create(AstEditor editor, Expression expression) throws Exception {
    // (Type) super.someMethodInvocation()
    if (expression instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) expression;
        if (castExpression.getExpression() instanceof SuperMethodInvocation) {
            // prepare component Class
            Class<?> componentClass;
            {//from w  w w. j  a v a 2s .c o  m
                String componentClassName = AstNodeUtils.getFullyQualifiedName(expression, true);
                componentClass = EditorState.get(editor).getEditorLoader().loadClass(componentClassName);
            }
            // create JavaInfo
            return JavaInfoUtils.createJavaInfo(editor, componentClass,
                    new CastedSuperInvocationCreationSupport(castExpression));
        }
    }
    // super.someMethodInvocation()
    if (expression instanceof SuperMethodInvocation
            && expression.getLocationInParent() != CastExpression.EXPRESSION_PROPERTY) {
        SuperMethodInvocation invocation = (SuperMethodInvocation) expression;
        ITypeBinding typeBinding = AstNodeUtils.getTypeBinding(invocation);
        if (isToolkitObject(editor, typeBinding)) {
            // prepare component Class
            Class<?> componentClass;
            {
                String componentClassName = AstNodeUtils.getFullyQualifiedName(typeBinding, true);
                componentClass = EditorState.get(editor).getEditorLoader().loadClass(componentClassName);
            }
            // create JavaInfo
            return JavaInfoUtils.createJavaInfo(editor, componentClass,
                    new SuperInvocationCreationSupport(invocation));
        }
    }
    // can not create model
    return null;
}

From source file:org.eclipse.wb.internal.core.parser.JavaInfoResolver.java

License:Open Source License

private JavaInfo getJavaInfo0(Expression expression) throws Exception {
    // check for nodes that can not be JavaInfo
    if (expression instanceof NullLiteral || expression instanceof BooleanLiteral
            || expression instanceof NumberLiteral || expression instanceof StringLiteral
            || expression instanceof TypeLiteral || expression instanceof Assignment
            || expression instanceof ArrayCreation || expression instanceof ArrayInitializer) {
        return null;
    }/*from w w w  . jav  a  2s  .  co m*/
    // "null" or "this"
    if (expression == null || expression instanceof ThisExpression) {
        if (!m_thisJavaInfoReady) {
            m_thisJavaInfoReady = true;
            for (JavaInfo component : getComponents()) {
                if (component.getCreationSupport() instanceof ThisCreationSupport) {
                    m_thisJavaInfo = component;
                    break;
                }
            }
        }
        if (m_thisJavaInfo != null) {
            m_thisJavaInfo.addRelatedNode(expression);
        }
        return m_thisJavaInfo;
    }
    // try to find tracked value
    {
        ExecutionFlowDescription flowDescription = m_editorState.getFlowDescription();
        ExpressionValue value = ExecutionFlowUtils2.getValue(flowDescription, expression);
        if (value != null) {
            // may be bound JavaInfo creation/access
            JavaInfo javaInfo = (JavaInfo) value.getModel();
            if (javaInfo != null) {
                boolean expressionPartOfCasted = javaInfo
                        .getCreationSupport() instanceof CastedSuperInvocationCreationSupport
                        && expression.getLocationInParent() == CastExpression.EXPRESSION_PROPERTY;
                if (!expressionPartOfCasted) {
                    javaInfo.addRelatedNode(expression);
                }
                return javaInfo;
            }
            // resolve "this"
            {
                Expression newExpression = value.getExpression();
                if (newExpression instanceof ThisExpression && newExpression != expression) {
                    return getJavaInfo0(newExpression);
                }
            }
        }
    }
    // XXX special support for RCP FormToolkit
    if (AstNodeUtils.isSuccessorOf(expression, "org.eclipse.ui.forms.widgets.FormToolkit")) {
        for (JavaInfo component : getComponents()) {
            if (component instanceof InstanceFactoryInfo) {
                if (component.getCreationSupport().isJavaInfo(expression)) {
                    ExecutionFlowUtils2.ensurePermanentValue(expression).setModel(component);
                    component.addRelatedNode(expression);
                    return component;
                }
            }
        }
    }
    // invocation
    if (expression instanceof MethodInvocation) {
        MethodInvocation invocation = (MethodInvocation) expression;
        String invName = invocation.getName().getIdentifier();
        // XXX special support for GWT RootPanel
        if (invName.equals("get")
                && AstNodeUtils.isSuccessorOf(invocation, "com.google.gwt.user.client.ui.RootPanel")) {
            for (JavaInfo component : getComponents()) {
                if (component.getCreationSupport().isJavaInfo(invocation)) {
                    ExecutionFlowUtils2.ensurePermanentValue(expression).setModel(component);
                    component.addRelatedNode(expression);
                    return component;
                }
            }
        }
        // generic get()
        if (invName.startsWith("get")) {
            JavaInfo expressionJavaInfo = getJavaInfo(invocation.getExpression());
            // getContentPane()
            {
                JavaInfo result = getExposedJavaInfo(expressionJavaInfo, invocation);
                if (result != null) {
                    return result;
                }
            }
            // viewer.getTable()
            if (expressionJavaInfo instanceof IWrapperInfo) {
                IWrapperInfo wrapperInfo = (IWrapperInfo) expressionJavaInfo;
                IWrapper wrapper = wrapperInfo.getWrapper();
                JavaInfo javaInfo = wrapper.getWrappedInfo();
                if (wrapper.isWrappedInfo(invocation)) {
                    ExecutionFlowUtils2.ensurePermanentValue(expression).setModel(javaInfo);
                    javaInfo.addRelatedNode(expression);
                    return javaInfo;
                }
            }
        }
        return null;
    }
    // m_exposedField
    if (expression instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) expression;
        JavaInfo thisJavaInfo = getJavaInfo(null);
        String fieldName = simpleName.getIdentifier();
        return getExposedJavaInfo(thisJavaInfo, expression, fieldName);
    }
    // someJavaInfo.m_exposedField
    if (expression instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) expression;
        JavaInfo hostJavaInfo = getJavaInfo(qualifiedName.getQualifier());
        String fieldName = qualifiedName.getName().getIdentifier();
        return getExposedJavaInfo(hostJavaInfo, expression, fieldName);
    }
    // no
    return null;
}

From source file:org.eclipse.wb.internal.swing.model.bean.ActionExpressionAccessor.java

License:Open Source License

@Override
public boolean setExpression(final JavaInfo javaInfo, final String source) throws Exception {
    final Expression expression = getExpression(javaInfo);
    if (expression != null) {
        final AstEditor editor = javaInfo.getEditor();
        if (source == null) {
            if (expression.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
                ExecutionUtils.run(javaInfo, new RunnableEx() {
                    public void run() throws Exception {
                        editor.removeEnclosingStatement(expression);
                    }//from   w  w  w  . j a  v  a2  s .  c  om
                });
            }
        } else if (!editor.getSource(expression).equals(source)) {
            ExecutionUtils.run(javaInfo, new RunnableEx() {
                public void run() throws Exception {
                    editor.replaceExpression(expression, source);
                }
            });
        }
    } else if (source != null) {
        ExecutionUtils.run(javaInfo, new RunnableEx() {
            public void run() throws Exception {
                String statementSource = "putValue(" + m_keyName + ", " + source + ");";
                javaInfo.getEditor().addStatement(statementSource, getTarget());
            }

            private StatementTarget getTarget() throws Exception {
                // if first statement in constructor is "super", add after it
                Block block = m_actionInfo.getInitializationBlocks().get(0);
                List<Statement> statements = DomGenerics.statements(block);
                if (!statements.isEmpty()) {
                    Statement statement = statements.get(0);
                    if (statement instanceof SuperConstructorInvocation) {
                        return new StatementTarget(statement, false);
                    }
                }
                // in other case add as first statement in constructor
                return new StatementTarget(block, true);
            }
        });
    }
    // success
    return true;
}