"Make this anonymous inner class a lambda" SonarLint 中的代码异味

"Make this anonymous inner class a lambda" code smell in SonarLint

在检查 SonarLint 报告时,我多次闻到这种代码味道 “将此匿名内部 class 设为 lambda”。 我怎样才能使这个内部 class 成为 lambda?这对我来说有点挑战。我该如何解决这个问题,这些是我受影响的代码。

Aggregation agg = Aggregation.newAggregation(

        new AggregationOperation() {

            @Override
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
                        Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))));
            }

        }, new AggregationOperation() {

            @Override
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(resourceDetails, resourceDeatilsV));
            }

        }, LookupOperation.newLookup().from(resourceDetailsStr).localField(resourceDetails).foreignField("_id")
                .as(resourceDetails),
        new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(resourceDetails,
                        new Document(arrayElemAt, Arrays.asList(resourcedetailsTempString, 0L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(domainStr, new Document(arrayElemAt,
                        Arrays.asList(new Document(objToArray, resourceDetailsDomain), 1L))));
            }
        }

        , new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(domainStr, domainV));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(lookup, new Document("from", "infra_asset")
                        .append("let", new Document(assetIdStr, assetRefId).append(domainStr, rootDomain))
                        .append(pipeLineStr,
                                Arrays.asList(new Document(match, new Document(expr, new Document("$and",
                                        Arrays.asList(new Document("$eq", Arrays.asList(domain, infra)),
                                                new Document("$eq", Arrays.asList("$_id", assetId))))))))
                        .append("as", "infraAssest"));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(lookup, new Document("from", "application_asset")
                        .append("let", new Document(assetIdStr, assetRefId).append(domainStr, rootDomain))
                        .append(pipeLineStr,
                                Arrays.asList(new Document(match, new Document(expr, new Document("$and",
                                        Arrays.asList(new Document("$eq", Arrays.asList(domain, "Application")),
                                                new Document("$eq", Arrays.asList("$_id", assetId))))))))
                        .append("as", applicationAsset));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(project, new Document(resourceDetails, 1L).append(resourceThresholdStr, 1L)
                        .append(customFieldStr, new Document(cond, new Document("if",
                                new Document("$eq", Arrays.asList(rootDomain, infra)))
                                .append("then", rootInfraAsset)
                                .append("else", new Document(cond,
                                        new Document("if",
                                                new Document("$eq", Arrays.asList(rootDomain, application)))
                                                .append("then", rootApplicationAsset)
                                                .append("else", "null"))))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(customFieldStr,
                        new Document(arrayElemAt, Arrays.asList(customField, 0L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(match, new Document(expr, new Document("$and",
                        Arrays.asList(new Document("$eq", Arrays.asList(customFieldPlatformName, platformName)),
                                new Document("$eq", Arrays.asList(customFieldAssetName, assetName))))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(moduleStr, new Document(arrayElemAt,
                        Arrays.asList(new Document(objToArray, resourceDetailModule), 1L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(moduleStr, moduleV));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(lookup, new Document("from", modulesStr).append(localFieldStr, moduleStr)
                        .append(foreignFieldStr, "_id").append("as", moduleStr));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields,
                        new Document(moduleStr, new Document(arrayElemAt, Arrays.asList(module, 0L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(match,
                        new Document(expr, new Document("$eq", Arrays.asList(moduleString, moduleName))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {

                return new Document(addFields, new Document(resourceThresholdStr, new Document(arrayElemAt,
                        Arrays.asList(new Document(objToArray, "$$ROOT.resourceThreshold"), 1L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(resourceThresholdStr, "$resourceThreshold.v"));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(lookup,
                        new Document("from", "resource_threshold").append(localFieldStr, resourceThresholdStr)
                                .append(foreignFieldStr, "_id").append("as", resourceThresholdStr));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(resourceThresholdStr,
                        new Document(arrayElemAt, Arrays.asList("$resourceThreshold", 0L))));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(addFields, new Document(platformNameStr, customFieldPlatformName)
                        .append("asset_name", customFieldAssetName).append(moduleNameStr, moduleString));
            }
        }, new AggregationOperation() {
            public Document toDocument(AggregationOperationContext context) {
                return new Document(project, new Document(moduleNameStr, 1L).append(resourceThresholdStr, 1L)
                        .append(platformNameStr, 1L).append("asset_name", 1L));
            }
        }

);
 

new AggregationOperation 开始的每个区块都会受到影响。

而不是写作

new AggregationOperation() {

    @Override
    public Document toDocument(AggregationOperationContext context) {
        return new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
        Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))));
    }
}

你应该会写

context -> new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
        Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))))

对于您创建的每个 AggregationOperation 也是如此。

这是因为编译器可以判断出您需要一个 AggregationOperation 并且知道一个 AggregationOperation 只有一个方法。这就是为什么您可以用该方法的等效 lambda 替换匿名 AggregationOperation

您可能想看看您的 IDE 是否可以自动为您进行重构。