private static void maybeReportIssue( int flagValue, JavaContext context, MethodInvocation node) { if ((flagValue & GET_SIGNATURES_FLAG) != 0) { context.report(ISSUE, node, context.getLocation(node.astArguments().last()), "Reading app signatures from getPackageInfo: The app signatures " + "could be exploited if not validated properly; " + "see issue explanation for details."); } }
/** * 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 void visitTypeApply(JCTypeApply node) { TypeReference ref = (TypeReference) toTree(node.clazz, FlagKey.TYPE_REFERENCE); TypeReferencePart last = ref.astParts().last(); fillList(node.arguments, last.rawTypeArguments(), FlagKey.TYPE_REFERENCE); setPos(node, ref); setConversionPositionInfo(last, "<", getPosition(node)); set(node, ref); }
@Override public void visitTypeApply(JCTypeApply node) { TypeReference ref = (TypeReference) toTree(node.clazz, FlagKey.TYPE_REFERENCE); TypeReferencePart last = ref.astParts().last(); fillList(node.arguments, last.rawTypeArguments(), FlagKey.TYPE_REFERENCE); setPos(node, ref); setConversionPositionInfo(last, "<", getPosition(node)); set(node, ref); }
public void varargsOnlyLegalOnMethods(VariableDefinition node) { if (!node.astVarargs()) return; if (node.getParent() == null) return; MethodDeclaration md = node.upIfParameterToMethodDeclaration(); ConstructorDeclaration cd = node.upIfParameterToConstructorDeclaration(); Node last; if (md != null) last = md.astParameters().last(); else if (cd != null) last = cd.astParameters().last(); else last = null; if (node != last) { node.addMessage(error(VARIABLEDEFINITION_VARARGS_NOT_LEGAL_HERE, "Varargs are only legal on the last parameter of a constructor or method.")); } }
@Override public void visitTypeApply(JCTypeApply node) { TypeReference ref = (TypeReference) toTree(node.clazz, FlagKey.TYPE_REFERENCE); TypeReferencePart last = ref.astParts().last(); fillList(node.arguments, last.rawTypeArguments(), FlagKey.TYPE_REFERENCE); setPos(node, ref); setConversionPositionInfo(last, "<", getPosition(node)); set(node, ref); }
public void varargsOnlyLegalOnMethods(VariableDefinition node) { if (!node.astVarargs()) return; if (node.getParent() == null) return; MethodDeclaration md = node.upIfParameterToMethodDeclaration(); ConstructorDeclaration cd = node.upIfParameterToConstructorDeclaration(); Node last; if (md != null) last = md.astParameters().last(); else if (cd != null) last = cd.astParameters().last(); else last = null; if (node != last) { node.addMessage(error(VARIABLEDEFINITION_VARARGS_NOT_LEGAL_HERE, "Varargs are only legal on the last parameter of a constructor or method.")); } }
public void varargsOnlyLegalOnMethods(VariableDefinition node) { if (!node.astVarargs()) return; if (node.getParent() == null) return; MethodDeclaration md = node.upIfParameterToMethodDeclaration(); ConstructorDeclaration cd = node.upIfParameterToConstructorDeclaration(); Node last; if (md != null) last = md.astParameters().last(); else if (cd != null) last = cd.astParameters().last(); else last = null; if (node != last) { node.addMessage(error(VARIABLEDEFINITION_VARARGS_NOT_LEGAL_HERE, "Varargs are only legal on the last parameter of a constructor or method.")); } }
@CopyMethod static StrictListAccessor<TypeReference, TypeReference> getTypeArguments(TypeReference self) { try { return self.astParts().last().typeArguments.wrap(self).asStrict(); } catch (Exception e) { return ListAccessor.emptyStrict("typeArguments", self); } } }
/** * Returns true if this method looks like it's overriding android.view.View's * {@code public void layout(int l, int t, int r, int b)} */ private static boolean isLayoutMethod(MethodDeclaration node) { if (LAYOUT.equals(node.astMethodName().astValue())) { StrictListAccessor<VariableDefinition, MethodDeclaration> parameters = node.astParameters(); if (parameters != null && parameters.size() == 4) { Iterator<VariableDefinition> iterator = parameters.iterator(); for (int i = 0; i < 4; i++) { if (!iterator.hasNext()) { return false; } VariableDefinition next = iterator.next(); TypeReferencePart type = next.astTypeReference().astParts().last(); if (!TYPE_INT.equals(type.getTypeName())) { return false; } } return true; } } return false; }
@CopyMethod static StrictListAccessor<TypeReference, TypeReference> getTypeArguments(TypeReference self) { try { return self.astParts().last().typeArguments.wrap(self).asStrict(); } catch (Exception e) { return ListAccessor.emptyStrict("typeArguments", self); } } }
@CopyMethod static StrictListAccessor<TypeReference, TypeReference> getTypeArguments(TypeReference self) { try { return self.astParts().last().typeArguments.wrap(self).asStrict(); } catch (Exception e) { return ListAccessor.emptyStrict("typeArguments", self); } } }
/** * 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; }
@Override public void visitImport(JCImport node) { ImportDeclaration imp = new ImportDeclaration(); fillWithIdentifiers(node.getQualifiedIdentifier(), imp.astParts()); Identifier last = imp.astParts().last(); if (last != null && "*".equals(last.astValue())) { imp.astParts().remove(last); imp.astStarImport(true); setConversionPositionInfo(imp, ".*", last.getPosition()); } imp.astStaticImport(node.isStatic()); set(node, imp); }
@Override public void visitImport(JCImport node) { ImportDeclaration imp = new ImportDeclaration(); fillWithIdentifiers(node.getQualifiedIdentifier(), imp.astParts()); Identifier last = imp.astParts().last(); if (last != null && "*".equals(last.astValue())) { imp.astParts().remove(last); imp.astStarImport(true); setConversionPositionInfo(imp, ".*", last.getPosition()); } imp.astStaticImport(node.isStatic()); set(node, imp); }
@Override public void visitImport(JCImport node) { ImportDeclaration imp = new ImportDeclaration(); fillWithIdentifiers(node.getQualifiedIdentifier(), imp.astParts()); Identifier last = imp.astParts().last(); if (last != null && "*".equals(last.astValue())) { imp.astParts().remove(last); imp.astStarImport(true); setConversionPositionInfo(imp, ".*", last.getPosition()); } imp.astStaticImport(node.isStatic()); set(node, imp); }
@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 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 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 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); } } }