/** * Adds a cast check to compareTo methods. This helps Comparable types behave well in sorted * collections which rely on Java's runtime type checking. */ @Override public void endVisit(MethodDeclaration node) { IMethodBinding binding = node.getMethodBinding(); if (!binding.getName().equals("compareTo") || node.getBody() == null) { return; } ITypeBinding comparableType = BindingUtil.findInterface(binding.getDeclaringClass(), "java.lang.Comparable"); if (comparableType == null) { return; } ITypeBinding[] typeArguments = comparableType.getTypeArguments(); ITypeBinding[] parameterTypes = binding.getParameterTypes(); if (typeArguments.length != 1 || parameterTypes.length != 1 || !typeArguments[0].isEqualTo(parameterTypes[0])) { return; } IVariableBinding param = node.getParameters().get(0).getVariableBinding(); FunctionInvocation castCheck = createCastCheck(typeArguments[0], new SimpleName(param)); if (castCheck != null) { node.getBody().getStatements().add(0, new ExpressionStatement(castCheck)); } }
@Override public boolean visit(MethodDeclaration node) { IMethodBinding binding = node.getMethodBinding(); if (binding.isConstructor()) { List<SingleVariableDeclaration> params = node.getParameters(); if (params.size() > 0) { IVariableBinding firstParam = params.get(0).getVariableBinding(); if (firstParam.getName().equals("outer$")) { outerParam = firstParam; } } } return true; }
@Override public boolean visit(MethodDeclaration node) { currentMethod = node.getMethodBinding(); count = 1; return true; }