Search in sources :

Example 6 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.

the class ExtractMethodRefactoring method createDeclaration.

private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
    VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
    VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
    fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
    fragment.setInitializer(intilizer);
    VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
    result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
    result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
    return result;
}
Also used : ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 7 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.

the class ExtractMethodAnalyzer method initReturnType.

private void initReturnType(ImportRewrite rewriter) {
    AST ast = fEnclosingBodyDeclaration.getAST();
    fReturnType = null;
    fReturnTypeBinding = null;
    switch(fReturnKind) {
        case ACCESS_TO_LOCAL:
            VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
            fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
            if (declaration.resolveBinding() != null) {
                fReturnTypeBinding = declaration.resolveBinding().getType();
            }
            break;
        case EXPRESSION:
            Expression expression = (Expression) getFirstSelectedNode();
            if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
                fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
            } else {
                fExpressionBinding = expression.resolveTypeBinding();
            }
            if (fExpressionBinding != null) {
                if (fExpressionBinding.isNullType()) {
                    getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
                } else {
                    ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
                    if (normalizedBinding != null) {
                        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
                        fReturnType = rewriter.addImport(normalizedBinding, ast, context);
                        fReturnTypeBinding = normalizedBinding;
                    }
                }
            } else {
                fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
                //$NON-NLS-1$
                fReturnTypeBinding = ast.resolveWellKnownType("void");
                getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
            }
            break;
        case RETURN_STATEMENT_VALUE:
            LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
            if (enclosingLambdaExpr != null) {
                fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
                IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
                fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
            } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
                fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
                fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
            }
            break;
        default:
            fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
            //$NON-NLS-1$
            fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
    if (fReturnType == null) {
        fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
        //$NON-NLS-1$
        fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 8 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project XobotOS by xamarin.

the class NativeGlueGenerator method generate.

public Method generate() {
    final Method method = new Method(_nativeFunction, _returnType, Visibility.PUBLIC, Flags.EXPORT);
    final List<Expression> args = new ArrayList<Expression>();
    final List<Statement> preStmnts = new ArrayList<Statement>();
    final List<Statement> postStmnts = new ArrayList<Statement>();
    final Kind kind = _method.getKind();
    final ByRef<Expression> instanceArg;
    if ((kind == Kind.INSTANCE) || (kind == Kind.PROXY))
        instanceArg = new ByRef<Expression>(null);
    else
        instanceArg = null;
    if (_implicitInstance != null) {
        Parameter param = new Parameter(new PointerType(_nativeClass), "_instance");
        method.addParameter(param);
        Expression arg = new VariableReference(param);
        if (instanceArg != null)
            instanceArg.value = arg;
        else
            args.add(new DereferenceExpression(arg));
    }
    for (int i = 0; i < _paramInfo.length; i++) {
        if (_paramInfo[i] == null)
            continue;
        VariableDeclaration vdecl = (VariableDeclaration) _decl.parameters().get(i);
        IVariableBinding vbinding = vdecl.resolveBinding();
        final String name = getNativeName(vbinding.getName());
        boolean hasInstanceArg = (instanceArg != null) && (instanceArg.value == null);
        final boolean isInstanceArg = hasInstanceArg && (i == 0);
        INativeMarshalContext context = new INativeMarshalContext() {

            @Override
            public void addPreStatement(Statement statement) {
                preStmnts.add(statement);
            }

            @Override
            public void addPostStatement(Statement statement) {
                postStmnts.add(statement);
            }

            @Override
            public NativeVariable createParameter(String suffix, AbstractTypeReference type) {
                String pname = suffix != null ? name + "_" + suffix : name;
                Parameter param = new Parameter(type, pname);
                method.addParameter(param);
                return new NativeVariable(param);
            }

            @Override
            public NativeVariable createVariable(String suffix, AbstractTypeReference type, Expression init) {
                String vname = suffix != null ? name + "_" + suffix : name;
                LocalVariable var = new LocalVariable(type, vname, init);
                preStmnts.add(var);
                return new NativeVariable(var);
            }

            @Override
            public void addArgument(Expression arg) {
                if (isInstanceArg)
                    instanceArg.value = arg;
                else
                    args.add(arg);
            }
        };
        _paramInfo[i].marshal.marshalNative(context, _paramInfo[i].mode, _paramInfo[i].flags);
    }
    if (_method.getKind() == Kind.DESTRUCTOR) {
        method.getBody().addStatement(new DestructorInvocation(args.get(0)));
        return method;
    }
    final AbstractInvocation invocation;
    if (_method.getKind() == Kind.INSTANCE) {
        Expression member = new InstanceMemberAccess(instanceArg.value, _nativeName);
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.PROXY) {
        Expression member = new StaticMemberAccess(_nativeClass, _nativeName);
        args.add(0, new DereferenceExpression(instanceArg.value));
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.STATIC) {
        Expression member = new StaticMemberAccess(_nativeClass, _nativeName);
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.CONSTRUCTOR) {
        AbstractTypeReference type = ((PointerType) _returnType).getElementType();
        invocation = new ConstructorInvocation(type, args);
    } else {
        return null;
    }
    for (Statement stm : preStmnts) {
        method.getBody().addStatement(stm);
    }
    final NativeVariable retval;
    if (_returnHelper != null) {
        NativeVariable cppRetval = NativeVariable.createLocal(method.getBody(), _returnHelper.getRealNativeType(), "_returnObj", invocation, NativeVariable.Flags.BYREF);
        retval = NativeVariable.createLocal(method.getBody(), _returnHelper.getNativePInvokeType(), "_retval", _returnHelper.marshalNativeRetval(cppRetval.getReference()), NativeVariable.Flags.BYREF);
        method.getBody().addStatement(new DestructorInvocation(cppRetval.getReference()));
    } else if (_returnInfo != null) {
        retval = NativeVariable.createLocal(method.getBody(), _returnType, "_retval", invocation);
    } else {
        method.getBody().addStatement(invocation);
        retval = null;
    }
    for (Statement stm : postStmnts) {
        method.getBody().addStatement(stm);
    }
    if (retval != null) {
        method.getBody().addStatement(new ReturnStatement(retval.getReference()));
    }
    return method;
}
Also used : ByRef(sharpen.core.framework.ByRef) ArrayList(java.util.ArrayList) Kind(sharpen.xobotos.api.interop.NativeMethod.Kind) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 9 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project XobotOS by xamarin.

the class NativeMethodBuilder method resolve.

public boolean resolve(IMarshalContext context) {
    if (_resolveFailed)
        return false;
    if (_resolved)
        return true;
    _binding = _node.resolveBinding();
    ITypeBinding returnType = _binding.getReturnType();
    ITypeBinding[] paramTypes = _binding.getParameterTypes();
    _methodName = BindingUtils.qualifiedSignature(_binding);
    final Signature signature = getNativeMethod().getSignature();
    final Kind kind = _native.getKind();
    MarshalInfo returnInfo = null;
    if (isVoid(returnType) && (kind == Kind.CONSTRUCTOR)) {
        ReturnInfo info = signature.getReturnInfo();
        if ((info != null) && (info.marshal != null))
            returnInfo = info.marshal.resolve(null);
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type of constructor '%s'", _methodName);
            _resolveFailed = true;
        }
    } else if (!isVoid(returnType) && !getNativeMethod().returnVoid()) {
        ReturnInfo info = signature.getReturnInfo();
        if (info != null) {
            if (info.marshal != null)
                returnInfo = info.marshal.resolve(returnType);
            else
                returnInfo = context.getMarshalInfo(returnType);
        } else {
            returnInfo = context.getMarshalInfo(returnType);
        }
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type '%s' of method '%s'", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        }
    }
    if (kind == Kind.CONSTRUCTOR) {
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type '%s' of constructor '%s'", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        } else if (!(returnInfo instanceof MarshalAsClass)) {
            Sharpen.Log(Level.SEVERE, "Return type '%s' of constructor '%s' must have MarshalAsClass marshal info", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        }
    }
    if (kind == Kind.DESTRUCTOR) {
        if (_returnInfo != null) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' cannot return a value.", _methodName);
            _resolveFailed = true;
        } else if (paramTypes.length != 1) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' must take one single argument.", _methodName);
            _resolveFailed = true;
        } else if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' must have a native handle", _methodName);
            _resolveFailed = true;
        }
    }
    if (_resolveFailed)
        return false;
    if (returnInfo != null) {
        if (!returnInfo.resolve(context, returnType)) {
            Sharpen.Log(Level.SEVERE, "Cannot resolve return value in '%s'.", _methodName);
            _resolveFailed = true;
        } else {
            _returnInfo = new ElementInfo(returnType, returnInfo, Mode.OUT, null);
        }
    }
    if (kind == Kind.DESTRUCTOR) {
        _paramInfo = new ElementInfo[1];
        if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Missing 'native-handle' for destructor '%s'.", _methodName);
            _resolveFailed = true;
        } else {
            MarshalInfo info = new MarshalAsClass(paramTypes[0], null, _nativeHandle.getTemplate());
            VariableDeclaration vdecl = (VariableDeclaration) _node.parameters().get(0);
            IVariableBinding vbinding = vdecl.resolveBinding();
            ITypeBinding vtype = vbinding.getType();
            if (!info.resolve(context, vtype)) {
                Sharpen.Log(Level.SEVERE, "Cannot resolve marshal info for destructor '%s'.", _methodName);
                _resolveFailed = true;
            } else {
                _paramInfo[0] = new ElementInfo(vtype, info, null, null);
            }
        }
    } else {
        _paramInfo = new ElementInfo[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            final ParameterInfo info = signature.getParameterInfo(i);
            if ((info == null) || (info.mode == Mode.REMOVE))
                continue;
            MarshalInfo marshal;
            if (info.marshal != null)
                marshal = info.marshal.resolve(paramTypes[i]);
            else
                marshal = context.getMarshalInfo(paramTypes[i]);
            if (marshal == null) {
                Sharpen.Log(Level.SEVERE, "Missing marshal info for type '%s' in method '%s'", BindingUtils.qualifiedName(paramTypes[i]), _methodName);
                _resolveFailed = true;
                continue;
            }
            VariableDeclaration vdecl = (VariableDeclaration) _node.parameters().get(i);
            IVariableBinding vbinding = vdecl.resolveBinding();
            ITypeBinding vtype = vbinding.getType();
            boolean isInstanceArg;
            if ((kind == Kind.INSTANCE) || (kind == Kind.PROXY))
                isInstanceArg = (i == 0) && !signature.implicitInstance();
            else
                isInstanceArg = false;
            if (isInstanceArg) {
                _paramInfo[0] = new ElementInfo(vtype, marshal, Mode.REF, null);
                if (!marshal.resolve(context, vtype)) {
                    Sharpen.Log(Level.SEVERE, "Cannot resolve instance parameter of method '%s'.", _methodName);
                    _resolveFailed = true;
                }
            } else {
                _paramInfo[i] = new ElementInfo(vtype, marshal, info.mode, info.flags);
                if (!marshal.resolve(context, vtype)) {
                    Sharpen.Log(Level.SEVERE, "Cannot resolve parameter %d of method '%s'.", i, _methodName);
                    _resolveFailed = true;
                }
            }
        }
    }
    if (signature.implicitInstance()) {
        if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Cannot use <add-instance> without <native-handle> in '%s'", _methodName);
            _resolveFailed = true;
            return false;
        }
        String fieldName = _nativeHandle.getTemplate().getField();
        _implicitInstance = findField(_binding, fieldName);
        if (_implicitInstance == null) {
            Sharpen.Log(Level.SEVERE, "No such field '%s' in type '%s'", fieldName, BindingUtils.qualifiedName(_binding.getDeclaringClass()));
            _resolveFailed = true;
            return false;
        }
    }
    if (_resolveFailed)
        return false;
    _resolved = true;
    return true;
}
Also used : MarshalAsClass(sharpen.xobotos.api.interop.marshal.MarshalAsClass) ParameterInfo(sharpen.xobotos.api.interop.Signature.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MarshalInfo(sharpen.xobotos.api.interop.marshal.MarshalInfo) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Kind(sharpen.xobotos.api.interop.NativeMethod.Kind) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ReturnInfo(sharpen.xobotos.api.interop.Signature.ReturnInfo)

Example 10 with VariableDeclaration

use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.

the class AbstractClassSubstituteRefactoring method canInstantiationBeRefactored.

private boolean canInstantiationBeRefactored(final ASTNode node, final List<VariableDeclaration> variablesToRefactor, final List<MethodInvocation> methodCallsToRefactorAlone, final List<MethodInvocation> methodCallsToRefactorWithVariable) {
    final List<MethodInvocation> localMethodCallsToRefactor = new ArrayList<MethodInvocation>();
    ASTNode childNode = node;
    do {
        ASTNode parentNode = childNode.getParent();
        switch(parentNode.getNodeType()) {
            case ASSIGNMENT:
            case RETURN_STATEMENT:
            case CAST_EXPRESSION:
            case INSTANCEOF_EXPRESSION:
                return false;
            case SINGLE_VARIABLE_DECLARATION:
            case VARIABLE_DECLARATION_EXPRESSION:
            case VARIABLE_DECLARATION_FRAGMENT:
            case VARIABLE_DECLARATION_STATEMENT:
                final VariableDeclaration varDecl = (VariableDeclaration) parentNode;
                final VariableDeclarationStatement variableDeclaration = (VariableDeclarationStatement) varDecl.getParent();
                if (hasType(variableDeclaration.getType().resolveBinding(), getExistingClassCanonicalName())) {
                    variablesToRefactor.add(varDecl);
                    methodCallsToRefactorWithVariable.addAll(localMethodCallsToRefactor);
                    return true;
                }
                return false;
            case METHOD_INVOCATION:
                final MethodInvocation mi = (MethodInvocation) parentNode;
                if (isObjectPassedInParameter(childNode, mi) || !canMethodBeRefactored(mi, localMethodCallsToRefactor)) {
                    return false;
                } else if (!isMethodReturningExistingClass(mi)) {
                    methodCallsToRefactorAlone.addAll(localMethodCallsToRefactor);
                    return true;
                }
                break;
            case PARENTHESIZED_EXPRESSION:
                break;
            default:
                methodCallsToRefactorAlone.addAll(localMethodCallsToRefactor);
                return true;
        }
        childNode = parentNode;
    } while (true);
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Aggregations

VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)40 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 ArrayList (java.util.ArrayList)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 Type (org.eclipse.jdt.core.dom.Type)7 AST (org.eclipse.jdt.core.dom.AST)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)5