/** * Check expression statement for test assertions. * * @param estmt Expression statements. */ private void checkAssertions(ExpressionStatement estmt) { if (estmt.getExpression() instanceof MethodInvocation) { MethodInvocation mi = (MethodInvocation) estmt.getExpression(); // Increment number of test assertions. if (mi.getName() != null && mi.getName().getIdentifier().startsWith("assert")) { this.numOfTestAssertions++; } } }
/* * @see ASTVisitor#visit(MethodInvocation) */ public boolean visit(MethodInvocation node) { if (node.getExpression() != null) { node.getExpression().accept(this); this.fBuffer.append("."); // $NON-NLS-1$ } if (node.getAST().apiLevel() >= AST.JLS3) { if (!node.typeArguments().isEmpty()) { this.fBuffer.append("<"); // $NON-NLS-1$ for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { Type t = (Type) it.next(); t.accept(this); if (it.hasNext()) { this.fBuffer.append(","); // $NON-NLS-1$ } } this.fBuffer.append(">"); // $NON-NLS-1$ } } node.getName().accept(this); this.fBuffer.append("("); // $NON-NLS-1$ for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { Expression e = (Expression) it.next(); e.accept(this); if (it.hasNext()) { this.fBuffer.append(","); // $NON-NLS-1$ } } this.fBuffer.append(")"); // $NON-NLS-1$ return false; }
/* * @see ASTVisitor#endVisit(MethodInvocation) */ @Override public boolean visit(MethodInvocation node) { evalQualifyingExpression(node.getExpression(), node.getName()); doVisitChildren(node.typeArguments()); doVisitChildren(node.arguments()); return false; }
@Override public boolean visit(MethodInvocation node) { String mName = node.getName().getFullyQualifiedName().toString(); Expression e = node.getExpression(); String typName = ""; if (e instanceof StringLiteral) { typName = "string"; } else if (e instanceof FieldAccess) { FieldAccess field = (FieldAccess) e; typName = field.getName().getFullyQualifiedName(); } else if (e instanceof Name) { typName = ((Name) e).getFullyQualifiedName(); if (varTypMap.containsKey(typName)) { typName = varTypMap.get(typName); } } else { if (e != null) { typName = e.toString(); if (typName.contains(".")) typName = typName.substring(0, typName.indexOf('.', 0)); if (varTypMap.containsKey(typName)) typName = varTypMap.get(typName); } } String key = typName + "->" + mName; if (freqRecord.containsKey(key)) { freqRecord.put(key, freqRecord.get(key) + 1); } else { freqRecord.put(key, 1); } return super.visit(node); }
//////////////////////////////////////////////////////////////////////////// // // Parser // //////////////////////////////////////////////////////////////////////////// @Override public AstObjectInfo parseExpression( AstEditor editor, String signature, MethodInvocation invocation, Expression[] arguments, IModelResolver resolver, IDatabindingsProvider provider) throws Exception { if (SET_STORE.equals(signature)) { m_storeReference = CoreUtils.getNodeReference(arguments[0]); return null; } if (AUTO_BIND.equals(signature)) { setAutobind(true); return null; } if (BIND.equals(signature)) { AnonymousClassDeclaration anonymousClassDeclaration = AstNodeUtils.getEnclosingNode(invocation, AnonymousClassDeclaration.class); if (anonymousClassDeclaration != null && anonymousClassDeclaration.getParent() instanceof ClassInstanceCreation && anonymousClassDeclaration.getParent().getParent() instanceof MethodInvocation) { MethodInvocation addListenerInvocation = (MethodInvocation) anonymousClassDeclaration.getParent().getParent(); if ("addListener".equals(addListenerInvocation.getName().getIdentifier()) && addListenerInvocation.getExpression() instanceof MethodInvocation) { MethodInvocation getSelectionModelInvocation = (MethodInvocation) addListenerInvocation.getExpression(); if ("getSelectionModel".equals(getSelectionModelInvocation.getName().getIdentifier())) { finishBinding(getSelectionModelInvocation.getExpression(), arguments[0], provider); return null; } } } finishBinding(arguments, provider); return null; } // return super.parseExpression(editor, signature, invocation, arguments, resolver, provider); }
private Type evaluateVariableType( AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) { if (fOriginalNode.getParent() instanceof MethodInvocation) { MethodInvocation parent = (MethodInvocation) fOriginalNode.getParent(); if (parent.getExpression() == fOriginalNode) { // _x_.foo() -> guess qualifier type by looking for a type with method 'foo' ITypeBinding[] bindings = ASTResolving.getQualifierGuess( fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext); if (bindings.length > 0) { for (int i = 0; i < bindings.length; i++) { addLinkedPositionProposal(KEY_TYPE, bindings[i]); } return imports.addImport(bindings[0], ast, importRewriteContext); } } } ITypeBinding binding = ASTResolving.guessBindingForReference(fOriginalNode); if (binding != null) { if (binding.isWildcardType()) { binding = ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast); if (binding == null) { // only null binding applies binding = ast.resolveWellKnownType("java.lang.Object"); // $NON-NLS-1$ } } if (isVariableAssigned()) { ITypeBinding[] typeProposals = ASTResolving.getRelaxingTypes(ast, binding); for (int i = 0; i < typeProposals.length; i++) { addLinkedPositionProposal(KEY_TYPE, typeProposals[i]); } } return imports.addImport(binding, ast, importRewriteContext); } // no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode); if (type != null) { return type; } if (fVariableKind == CONST_FIELD) { return ast.newSimpleType(ast.newSimpleName("String")); // $NON-NLS-1$ } return ast.newSimpleType(ast.newSimpleName("Object")); // $NON-NLS-1$ }
@SuppressWarnings("rawtypes") @Override public boolean visit(MethodInvocation node) { SimpleName methodName = node.getName(); List args = node.arguments(); Expression expression = node.getExpression(); Map<String, Integer> scopeBindings = getNodeScopes().get(node); String target = getTarget(expression); String targetType = translateTargetToType(target, scopeBindings); // Add only if you could guess the type of target, else ignore. // TODO: In case of a method in super type, this will still infer it as in "this". if (!targetType.isEmpty()) { List<String> argTypes = translateArgsToTypes(args, scopeBindings); if (!methodStack.empty()) { MethodDeclaration currentMethod = methodStack.peek(); MethodDecl methodDecl = getMethodDecl(currentMethod); List<MethodInvokRef> invoks = methodInvoks.get(methodDecl); if (invoks == null) { invoks = new ArrayList<>(); methodInvoks.put(methodDecl, invoks); } MethodInvokRef methodInvokRef = new MethodInvokRef( methodName.toString(), targetType, target, args.size(), node.getName().getStartPosition(), argTypes, methodName.getLength(), false, getReturnType(node)); invoks.add(methodInvokRef); } } return true; }
@Override public void endVisit(MethodInvocation node) { // TODO need more complex cases Stack<VMExpression> arguments = new Stack<VMExpression>(); for (int i = 0; i < node.arguments().size(); i++) { arguments.push((VMExpression) expressions.pop()); } VMExpression role = null; if (!Modifier.isStatic(node.resolveMethodBinding().getModifiers())) { role = (node.getExpression() == null) ? null : (VMExpression) (expressions.pop()); } VMMethodInvocation methodInvo = new VMMethodInvocation(node, node.getName().toString(), role); while (!arguments.empty()) { methodInvo.getArguments().add(arguments.pop()); } expressions.push(methodInvo); }
/** * Visits {@link SimpleName} AST nodes. Resolves the binding of the simple name and looks for it * in the {variableScope} map. If the binding is found, this is a reference to a variable. * * @param node the node to visit */ @Override public boolean visit(final SimpleName node) { if (node.getParent().getNodeType() == ASTNode.METHOD_INVOCATION) { final MethodInvocation invocation = (MethodInvocation) node.getParent(); if (invocation.getName() == node) { return true; } } // method declaration can have same name as variable but this does not mean it is binding to // that variable // added particularly for enum if (node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION) { return true; } addBindingData(node.getIdentifier(), node, nodeScopes.get(node)); return true; }
private static boolean replaceEnsureClassInitialized(ImplWriter w, MethodInvocation node) { if (!node.getName().getIdentifier().equals("ensureClassInitialized")) { return false; } if (node.arguments().size() != 1 || !(node.arguments().get(0) instanceof TypeLiteral)) { return false; } IMethodBinding mb = node.resolveMethodBinding(); if (!mb.getDeclaringClass().getQualifiedName().equals("sun.misc.Unsafe")) { return false; } TypeLiteral tl = (TypeLiteral) node.arguments().get(0); ITypeBinding tb = tl.getType().resolveBinding(); w.hardDep(tb); w.print(CName.relative(tb, w.type, true) + "::clinit()"); return true; }
/** * Decide whether or not the given invocation resolves to a MongoDB collection. * * @param invocation * @return */ private boolean isGetCollection(final MethodInvocation invocation) { boolean returnValue; String currentVariable; String invocationString; returnValue = false; if (invocation .resolveMethodBinding() .getDeclaringClass() .getQualifiedName() .equals("com.mongodb.DB")) { if (invocation.getName().toString().equals("getCollection")) { invocationString = invocation.getParent().toString(); currentVariable = invocationString.substring(0, invocationString.indexOf('=')); if (this.variableName.equals(currentVariable)) { returnValue = true; } } } return returnValue; }
@Override public boolean visit(MethodInvocation node) { String methodKey = castHelper.methodKey(node); String methodName = node.getName().toString(); int lineNumber = compilationUnit.getLineNumber(node.getStartPosition()); // 函数调用在线程调用到的函数中(单个函数可能牵扯到多个线程) if (threadMethodMapTable.containsKey(methodKey) && node.resolveMethodBinding() != null) { IMethodBinding iMethodBinding = node.resolveMethodBinding(); // 1.中断判断函数(中断接受节点) if ((methodName.equals("interrupted") || methodName.equals("isInterrupted"))) { if (iMethodBinding.getDeclaringClass() != null && iMethodBinding.getDeclaringClass().getBinaryName().equals("java.lang.Thread")) { System.out.println(node); System.out.println("interrupted judge"); HashSet<String> threads = threadMethodMapTable.get(methodKey); // (1)添加到每个线程的中断接受节点集合 for (String thread : threads) { ThreadInformation threadInformation = threadInfo.get(thread); Node interruptedNode = new Node(filePath, lineNumber); threadInformation.getInterruptNodes().add(interruptedNode); } // (2)变量自身判断(根据自己所绑定的线程类型而添加中断接受节点) interruptAcceptNodeHandle(node); } } // 2.引发中断的函数调用(中断通知节点) else if (methodName.equals("interrupt")) { ThreadInterruptNode threadInterruptNode = new ThreadInterruptNode(filePath, lineNumber); // (1)自中断 if (node.getExpression() instanceof MethodInvocation && ((MethodInvocation) node.getExpression()) .getName() .getIdentifier() .equals("currentThread")) { // 对有可能调用该自中断的线程添加到中断通知节点中 HashSet<String> threads = threadMethodMapTable.get(methodKey); for (String thread : threads) { threadInterruptNode.getThreadKeyList().add(thread); } threadInterruptNodes.add(threadInterruptNode); } // (2)调用中断,通过变量来定位threadKey else if (iMethodBinding.getDeclaringClass() != null && iMethodBinding.getDeclaringClass().getBinaryName().equals("java.lang.Thread")) { interruptNotifyNodeHandle(node, threadInterruptNode); } } // 3.会抛出中断异常的函数(中断接受节点) else { ITypeBinding[] typeBindings = node.resolveMethodBinding().getExceptionTypes(); for (ITypeBinding iTypeBinding : typeBindings) { if (iTypeBinding.getBinaryName().equals("java.lang.InterruptedException")) { System.out.println(node); System.out.println("interrupted exception"); HashSet<String> threads = threadMethodMapTable.get(methodKey); // (1)添加到每个线程的中断接受节点集合 for (String thread : threads) { ThreadInformation threadInformation = threadInfo.get(thread); Node interruptedNode = new Node(filePath, lineNumber); threadInformation.getInterruptNodes().add(interruptedNode); } // (2)变量自身判断(根据自己所绑定的线程类型而添加中断接受节点) interruptAcceptNodeHandle(node); } } } } return super.visit(node); }
/* (non-Javadoc) * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation) */ public boolean visit(MethodInvocation node) { expression = createMethodInvocation( node.getName().getIdentifier(), perform(node.getExpression()), node.arguments()); return false; }
public boolean visit(MethodInvocation invocation) { if (this.have(invocation.getName().resolveBinding().getJavaElement())) recordOwner(); return true; }