private char[][] chain(lombok.ast.StrictListAccessor<lombok.ast.Identifier, ?> parts) { return chain(parts, parts.size()); }
private char[][] chain(lombok.ast.StrictListAccessor<lombok.ast.Identifier, ?> parts) { return chain(parts, parts.size()); }
private char[][] chain(lombok.ast.StrictListAccessor<lombok.ast.Identifier, ?> parts) { return chain(parts, parts.size()); }
public Expression getArgument(int argument) { if (!(mTargetNode instanceof MethodInvocation)) { return null; } MethodInvocation call = (MethodInvocation) mTargetNode; StrictListAccessor<Expression, MethodInvocation> args = call.astArguments(); if (argument >= args.size()) { return null; } Iterator<Expression> iterator = args.iterator(); int index = 0; while (iterator.hasNext()) { Expression arg = iterator.next(); if (index++ == argument) { return arg; } } return null; }
@Override public boolean visitMethodInvocation(MethodInvocation node) { if (node.astName().astValue().equals(SET_THEME)) { // Look at argument StrictListAccessor<Expression, MethodInvocation> args = node.astArguments(); if (args.size() == 1) { Expression arg = args.first(); if (arg instanceof Select) { String resource = arg.toString(); if (resource.startsWith(R_STYLE_PREFIX)) { if (mActivityToTheme == null) { mActivityToTheme = new HashMap<String, String>(); } String name = ((Select) arg).astIdentifier().astValue(); mActivityToTheme.put(mClassFqn, STYLE_RESOURCE_PREFIX + name); } } } } return false; } }
@Override public void visitMethod( @NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { StrictListAccessor<Expression, MethodInvocation> argumentList = node.astArguments(); if (argumentList != null && argumentList.size() == 1) { Expression argument = argumentList.first(); if (argument instanceof Select) { String expression = argument.toString(); if (expression.startsWith(R_LAYOUT_RESOURCE_PREFIX)) { whiteListLayout(expression.substring(R_LAYOUT_RESOURCE_PREFIX.length())); } } } } }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { if (node.astArguments().size() == 1 && !node.astArguments().first().toString().equals("false")) { //$NON-NLS-1$ context.report(ISSUE, node, context.getLocation(node), "Using `setJavaScriptEnabled` can introduce XSS vulnerabilities " + "into you application, review carefully."); } }
@Override public boolean visitMethodInvocation(MethodInvocation node) { if (SET_SMALL_ICON.equals(node.astName().astValue())) { StrictListAccessor<Expression,MethodInvocation> arguments = node.astArguments(); if (arguments.size() == 1 && arguments.first() instanceof Select) { handleSelect((Select) arguments.first()); } } return super.visitMethodInvocation(node); } }
private void checkSparseArray(ConstructorInvocation node, TypeReference reference) { // reference.hasTypeArguments returns false where it should not StrictListAccessor<TypeReference, TypeReference> types = reference.getTypeArguments(); if (types != null && types.size() == 1) { TypeReference first = types.first(); String valueType = first.getTypeName(); if (valueType.equals(INTEGER)) { mContext.report(USE_SPARSE_ARRAY, node, mContext.getLocation(node), "Use `new SparseIntArray(...)` instead for better performance"); } else if (valueType.equals(BOOLEAN)) { mContext.report(USE_SPARSE_ARRAY, node, mContext.getLocation(node), "Use `new SparseBooleanArray(...)` instead for better performance"); } } } }
/** * Returns true if this method looks like it's overriding android.view.View's * {@code protected void onDraw(Canvas canvas)} */ private static boolean isOnDrawMethod(MethodDeclaration node) { if (ON_DRAW.equals(node.astMethodName().astValue())) { StrictListAccessor<VariableDefinition, MethodDeclaration> parameters = node.astParameters(); if (parameters != null && parameters.size() == 1) { VariableDefinition arg0 = parameters.first(); TypeReferencePart type = arg0.astTypeReference().astParts().last(); String typeName = type.getTypeName(); if (typeName.equals(CANVAS)) { return true; } } } return false; }
/** * Returns true if this method looks like it's overriding android.view.View's * {@code protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)} */ private static boolean isOnMeasureMethod(MethodDeclaration node) { if (ON_MEASURE.equals(node.astMethodName().astValue())) { StrictListAccessor<VariableDefinition, MethodDeclaration> parameters = node.astParameters(); if (parameters != null && parameters.size() == 2) { VariableDefinition arg0 = parameters.first(); VariableDefinition arg1 = parameters.last(); TypeReferencePart type1 = arg0.astTypeReference().astParts().last(); TypeReferencePart type2 = arg1.astTypeReference().astParts().last(); return TYPE_INT.equals(type1.getTypeName()) && TYPE_INT.equals(type2.getTypeName()); } } return false; }
@Override public boolean visitArrayCreation(lombok.ast.ArrayCreation node) { ArrayAllocationExpression aae = new ArrayAllocationExpression(); aae.sourceStart = start(node); aae.sourceEnd = end(node); aae.type = (TypeReference) toTree(node.astComponentTypeReference()); // TODO uncompilable parser test: new Type<Generics>[]... // TODO uncompilable parser test: new Type[][expr][][expr]... aae.type.bits |= ASTNode.IgnoreRawTypeCheck; int i = 0; Expression[] dimensions = new Expression[node.astDimensions().size()]; for (lombok.ast.ArrayDimension dim : node.astDimensions()) { dimensions[i++] = (Expression) toTree(dim.astDimension()); } aae.dimensions = dimensions; aae.initializer = (ArrayInitializer) toTree(node.astInitializer()); return set(node, aae); }
@Override public boolean visitArrayCreation(lombok.ast.ArrayCreation node) { ArrayAllocationExpression aae = new ArrayAllocationExpression(); aae.sourceStart = start(node); aae.sourceEnd = end(node); aae.type = (TypeReference) toTree(node.astComponentTypeReference()); // TODO uncompilable parser test: new Type<Generics>[]... // TODO uncompilable parser test: new Type[][expr][][expr]... aae.type.bits |= ASTNode.IgnoreRawTypeCheck; int i = 0; Expression[] dimensions = new Expression[node.astDimensions().size()]; for (lombok.ast.ArrayDimension dim : node.astDimensions()) { dimensions[i++] = (Expression) toTree(dim.astDimension()); } aae.dimensions = dimensions; aae.initializer = (ArrayInitializer) toTree(node.astInitializer()); return set(node, aae); }
@Override public boolean visitArrayCreation(lombok.ast.ArrayCreation node) { ArrayAllocationExpression aae = new ArrayAllocationExpression(); aae.sourceStart = start(node); aae.sourceEnd = end(node); aae.type = (TypeReference) toTree(node.astComponentTypeReference()); // TODO uncompilable parser test: new Type<Generics>[]... // TODO uncompilable parser test: new Type[][expr][][expr]... aae.type.bits |= ASTNode.IgnoreRawTypeCheck; int i = 0; Expression[] dimensions = new Expression[node.astDimensions().size()]; for (lombok.ast.ArrayDimension dim : node.astDimensions()) { dimensions[i++] = (Expression) toTree(dim.astDimension()); } aae.dimensions = dimensions; aae.initializer = (ArrayInitializer) toTree(node.astInitializer()); return set(node, aae); }
@Override public boolean visitTry(lombok.ast.Try node) { TryStatement tryStatement = new TryStatement(); tryStatement.sourceStart = start(node); tryStatement.sourceEnd = end(node); tryStatement.tryBlock = (Block) toTree(node.astBody()); int catchSize = node.astCatches().size(); if (catchSize > 0) { tryStatement.catchArguments = new Argument[catchSize]; tryStatement.catchBlocks = new Block[catchSize]; int i = 0; for (lombok.ast.Catch c : node.astCatches()) { tryStatement.catchArguments[i] = (Argument)toTree(c.astExceptionDeclaration()); tryStatement.catchBlocks[i] = (Block) toTree(c.astBody()); i++; } } tryStatement.finallyBlock = (Block) toTree(node.astFinally()); return set(node, tryStatement); }
@Override public boolean visitTry(lombok.ast.Try node) { TryStatement tryStatement = new TryStatement(); tryStatement.sourceStart = start(node); tryStatement.sourceEnd = end(node); tryStatement.tryBlock = (Block) toTree(node.astBody()); int catchSize = node.astCatches().size(); if (catchSize > 0) { tryStatement.catchArguments = new Argument[catchSize]; tryStatement.catchBlocks = new Block[catchSize]; int i = 0; for (lombok.ast.Catch c : node.astCatches()) { tryStatement.catchArguments[i] = (Argument)toTree(c.astExceptionDeclaration()); tryStatement.catchBlocks[i] = (Block) toTree(c.astBody()); i++; } } tryStatement.finallyBlock = (Block) toTree(node.astFinally()); return set(node, tryStatement); }
@Override public boolean visitTypeReference(TypeReference node) { ReplacementOrder order = currentOrder(); if (order != null && node.astParts().size() == 1 && node.astParts().last().rawTypeArguments().isEmpty() && node.astParts().last().astIdentifier().astValue().equals(order.typeReferenceToReplace)) { Node replacement = order.replacement.get(0); int startLoc, endLoc; if (order.position == null) { if (order.replacement.isEmpty() || replacement.getPosition().getStart() < 0) startLoc = location; else startLoc = replacement.getPosition().getStart(); if (order.replacement.isEmpty() || replacement.getPosition().getEnd() < 0) endLoc = location; else endLoc = replacement.getPosition().getEnd(); } else { startLoc = order.position.getStart(); endLoc = order.position.getEnd(); } if (replacement.getPosition().isUnplaced()) Ast.setAllPositions(replacement, new Position(startLoc, endLoc, responsible)); location = endLoc; node.replace(replacement); replacementsPointer++; return true; } return visitNode(node); } };
@Override public boolean visitModifiers(Modifiers node) { JCModifiers mods = treeMaker.Modifiers(node.getExplicitModifierFlags(), toList(JCAnnotation.class, node.astAnnotations())); Comment javadoc = null; if (node.getParent() instanceof JavadocContainer) { javadoc = ((JavadocContainer)node.getParent()).astJavadoc(); } else if (node.getParent() instanceof VariableDefinition && node.getParent().getParent() instanceof VariableDeclaration) { javadoc = ((VariableDeclaration)node.getParent().getParent()).astJavadoc(); } if (javadoc != null && javadoc.isMarkedDeprecated()) mods.flags |= Flags.DEPRECATED; if (node.isEmpty() && !hasConversionStructureInfo(node, "converted")) { //Workaround for a javac bug; start (but not end!) gets set of an empty modifiers object, //but only if these represent the modifiers of a constructor or method that has type variables. if ( (node.getParent() instanceof MethodDeclaration && ((MethodDeclaration)node.getParent()).astTypeVariables().size() > 0) || (node.getParent() instanceof ConstructorDeclaration && ((ConstructorDeclaration)node.getParent()).astTypeVariables().size() > 0)) { mods.pos = node.getParent().getPosition().getStart(); } return set(node, mods); } else { return posSet(node, mods); } }
@Override public boolean visitModifiers(Modifiers node) { JCModifiers mods = treeMaker.Modifiers(node.getExplicitModifierFlags(), toList(JCAnnotation.class, node.astAnnotations())); Comment javadoc = null; if (node.getParent() instanceof JavadocContainer) { javadoc = ((JavadocContainer)node.getParent()).astJavadoc(); } else if (node.getParent() instanceof VariableDefinition && node.getParent().getParent() instanceof VariableDeclaration) { javadoc = ((VariableDeclaration)node.getParent().getParent()).astJavadoc(); } if (javadoc != null && javadoc.isMarkedDeprecated()) mods.flags |= Flags.DEPRECATED; if (node.isEmpty() && !hasConversionStructureInfo(node, "converted")) { //Workaround for a javac bug; start (but not end!) gets set of an empty modifiers object, //but only if these represent the modifiers of a constructor or method that has type variables. if ( (node.getParent() instanceof MethodDeclaration && ((MethodDeclaration)node.getParent()).astTypeVariables().size() > 0) || (node.getParent() instanceof ConstructorDeclaration && ((ConstructorDeclaration)node.getParent()).astTypeVariables().size() > 0)) { mods.pos = node.getParent().getPosition().getStart(); } return set(node, mods); } else { return posSet(node, mods); } }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { ResolvedNode resolved = context.resolve(node); if (!(resolved instanceof ResolvedMethod) || !((ResolvedMethod) resolved).getContainingClass() .isSubclassOf(PACKAGE_MANAGER_CLASS, false)) { return; } StrictListAccessor<Expression, MethodInvocation> argumentList = node.astArguments(); // Ignore if the method doesn't fit our description. if (argumentList != null && argumentList.size() == 2) { TypeDescriptor firstParameterType = context.getType(argumentList.first()); if (firstParameterType != null && firstParameterType.matchesSignature(JavaParser.TYPE_STRING)) { maybeReportIssue(calculateValue(context, argumentList.last()), context, node); } } }