@Override public Object visit(ASTAllocationExpression node, Object data) { if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) { return data; if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) { return data; List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class); if (exp.size() >= 2) { return data; if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) { return data; ASTName name = node.getFirstDescendantOfType(ASTName.class);
@Override public Object visit(ASTAllocationExpression node, Object data) { if (node.isAnonymousClass()) { currentLookup.push(getMethodLookup(node.getType())); } super.visit(node, data); if (node.isAnonymousClass()) { currentLookup.pop(); } return data; }
/** * Returns true if this expression defines a body, * which is compiled to an anonymous class. If this * method returns false, then {@link #getQualifiedName()} * returns {@code null}. */ public boolean isAnonymousClass() { if (jjtGetNumChildren() > 1) { // check the last child return jjtGetChild(jjtGetNumChildren() - 1) instanceof ASTClassOrInterfaceBody; } return false; }
@Override public Object visit(ASTAllocationExpression node, Object data) { Node type = node.jjtGetChild(0); if (!(type instanceof ASTClassOrInterfaceType)) { return super.visit(node, data); } boolean jdk15 = ((RuleContext) data).getLanguageVersion() .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0; if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class) || jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class)) && !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) { ASTArguments args = node.getFirstChildOfType(ASTArguments.class); if (args.getArgumentCount() == 1) { ASTLiteral literal = node.getFirstDescendantOfType(ASTLiteral.class); if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) { return super.visit(node, data); } String img = literal.getImage(); if (literal.isStringLiteral()) { img = img.substring(1, img.length() - 1); } if ("0".equals(img) || "1".equals(img) || jdk15 && "10".equals(img)) { addViolation(data, node); return data; } } } return super.visit(node, data); }
@Override public Object visit(ASTAllocationExpression node, Object data) { super.visit(node, data); final ASTArrayDimsAndInits dims = node.getFirstChildOfType(ASTArrayDimsAndInits.class); if (dims != null) { final JavaTypeDefinition elementType = ((TypeNode) node.jjtGetChild(0)).getTypeDefinition(); if (elementType != null) { node.setTypeDefinition(elementType.withDimensions(dims.getArrayDepth())); } } else { rollupTypeUnary(node); } return data; }
@Override public Object visit(final ASTAllocationExpression node, final Object data) { if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) { // Ignore primitives final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0); final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage()); final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class); final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
@Override public Object visit(ASTAllocationExpression node, Object data) { if (node.getType() != Thread.class) { return super.visit(node, data); if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) { return super.visit(node, data); ASTFieldDeclaration fieldDeclaration = node.getFirstParentOfType(ASTFieldDeclaration.class); if (node.getDataFlowNode() == null && node.getFirstParentOfType(ASTLambdaExpression.class) != null) { if (fieldDeclaration == null || fieldDeclaration.getType() != ThreadFactory.class) { return addViolationAndReturn(node, data);
final public void AllocationExpression() throws ParseException { ASTAllocationExpression jjtn000 = new ASTAllocationExpression(this, JJTALLOCATIONEXPRESSION); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(NEW); jjtn000.jjtSetLastToken(getToken(0)); checkForBadAnonymousDiamondUsage(); break; if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
/** * Determine if the constructor contains (or ends with) a String Literal * * @param node * @return 1 if the constructor contains string argument, else 0 */ private int checkConstructor(ASTVariableDeclaratorId node, Object data) { Node parent = node.jjtGetParent(); if (parent.jjtGetNumChildren() >= 2) { ASTAllocationExpression allocationExpression = parent.jjtGetChild(1) .getFirstDescendantOfType(ASTAllocationExpression.class); ASTArgumentList list = null; if (allocationExpression != null) { list = allocationExpression.getFirstDescendantOfType(ASTArgumentList.class); } if (list != null) { ASTLiteral literal = list.getFirstDescendantOfType(ASTLiteral.class); if (!isAdditive(list) && literal != null && literal.isStringLiteral()) { return 1; } return processAdditive(data, 0, list, node); } } return 0; }
private void checkForBadAnonymousDiamondUsage() { if (jdkVersion < 9) { ASTAllocationExpression node = (ASTAllocationExpression)jjtree.peekNode(); ASTTypeArguments types = node.getFirstChildOfType(ASTClassOrInterfaceType.class).getFirstChildOfType(ASTTypeArguments.class); if (node.isAnonymousClass() && types != null && types.isDiamond()) { throwParseException("Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!"); } } } /**
@Override public Object visit(ASTAllocationExpression node, Object data) { if (!customBoolean) { if (node.hasDescendantOfType(ASTArrayDimsAndInits.class)) { return super.visit(node, data); } Node n1 = node.getFirstChildOfType(ASTClassOrInterfaceType.class); if (TypeHelper.isA((ASTClassOrInterfaceType) n1, Boolean.class)) { super.addViolation(data, node); return data; } } return super.visit(node, data); }
@Override public Object visit(ASTAllocationExpression node, Object data) { if (!inPrimaryExpressionContext || !(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) { return super.visit(node, data); } if (!PRIMITIVE_WRAPPERS.contains(node.jjtGetChild(0).getImage())) { return super.visit(node, data); } usingPrimitiveWrapperAllocation = true; return super.visit(node, data); }
@Override public Object visit(ASTAllocationExpression node, Object data) { //Custom Class if (node.getType() == null) { return super.visit(node, data); } if (!ExecutorService.class.isAssignableFrom(node.getType())) { return super.visit(node, data); } if (ThreadPoolExecutor.class == node.getType()) { return checkThreadPoolExecutor(node, data); } if (ScheduledThreadPoolExecutor.class == node.getType()) { return checkSchedulePoolExecutor(node, data); } return super.visit(node, data); }
private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) { ASTAllocationExpression ao = node.getFirstParentOfType(ASTAllocationExpression.class); if (ao == null) { return false; } // note that the child can be an ArrayDimsAndInits, for example, from // java.lang.FloatingDecimal: t = new int[ nWords+wordcount+1 ]; ASTClassOrInterfaceType an = ao.getFirstChildOfType(ASTClassOrInterfaceType.class); return an != null && TypeHelper.isEither(an, StringBuffer.class, StringBuilder.class); } }
private boolean isInStaticInitializer(ASTAllocationExpression node) { ASTInitializer initializer = node.getFirstParentOfType(ASTInitializer.class); return initializer != null && initializer.isStatic(); }
literals = constructorCall.findDescendantsOfType(ASTLiteral.class); } else {
@Override public Object visit(ASTAllocationExpression node, Object data) { Node type = node.jjtGetChild(0); if (!(type instanceof ASTClassOrInterfaceType)) { return super.visit(node, data); } boolean jdk15 = ((RuleContext) data).getLanguageVersion() .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0; if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class) || jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class)) && !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) { ASTArguments args = node.getFirstChildOfType(ASTArguments.class); if (args.getArgumentCount() == 1) { ASTLiteral literal = node.getFirstDescendantOfType(ASTLiteral.class); if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) { return super.visit(node, data); } String img = literal.getImage(); if (literal.isStringLiteral()) { img = img.substring(1, img.length() - 1); } if ("0".equals(img) || "1".equals(img) || jdk15 && "10".equals(img)) { addViolation(data, node); return data; } } } return super.visit(node, data); }
@Override public Object visit(ASTAllocationExpression node, Object data) { super.visit(node, data); final ASTArrayDimsAndInits dims = node.getFirstChildOfType(ASTArrayDimsAndInits.class); if (dims != null) { final JavaTypeDefinition elementType = ((TypeNode) node.jjtGetChild(0)).getTypeDefinition(); if (elementType != null) { node.setTypeDefinition(elementType.withDimensions(dims.getArrayDepth())); } } else { rollupTypeUnary(node); } return data; }
@Override public Object visit(final ASTAllocationExpression node, final Object data) { if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) { // Ignore primitives final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0); final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage()); final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class); final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
final public void AllocationExpression() throws ParseException { ASTAllocationExpression jjtn000 = new ASTAllocationExpression(this, JJTALLOCATIONEXPRESSION); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(NEW); jjtn000.jjtSetLastToken(getToken(0)); checkForBadAnonymousDiamondUsage(); break; if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));