| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.ast.validator.chunks; |
| 23 | |
| 24 | import com.github.javaparser.ast.NodeList; |
| 25 | import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
| 26 | import com.github.javaparser.ast.body.InitializerDeclaration; |
| 27 | import com.github.javaparser.ast.expr.*; |
| 28 | import com.github.javaparser.ast.validator.SimpleValidator; |
| 29 | import com.github.javaparser.ast.validator.SingleNodeTypeValidator; |
| 30 | import com.github.javaparser.ast.validator.TreeVisitorValidator; |
| 31 | import com.github.javaparser.ast.validator.Validators; |
| 32 | import com.github.javaparser.metamodel.NodeMetaModel; |
| 33 | import com.github.javaparser.metamodel.PropertyMetaModel; |
| 34 | |
| 35 | /** |
| 36 | * Contains validations that are valid for every Java version. |
| 37 | */ |
| 38 | public class CommonValidators extends Validators { |
| 39 | public CommonValidators() { |
| 40 | super( |
| 41 | new SimpleValidator<>(ClassOrInterfaceDeclaration.class, |
| 42 | n -> !n.isInterface() && n.getExtendedTypes().size() > 1, |
| 43 | (n, reporter) -> reporter.report(n.getExtendedTypes(1), "A class cannot extend more than one other class.") |
| 44 | ), |
| 45 | new SimpleValidator<>(ClassOrInterfaceDeclaration.class, |
| 46 | n -> n.isInterface() && !n.getImplementedTypes().isEmpty(), |
| 47 | (n, reporter) -> reporter.report(n.getImplementedTypes(0), "An interface cannot implement other interfaces.") |
| 48 | ), |
| 49 | new SingleNodeTypeValidator<>(ClassOrInterfaceDeclaration.class, (n, reporter) -> { |
| 50 | if (n.isInterface()) { |
| 51 | n.getMembers().forEach(mem -> { |
| 52 | if (mem instanceof InitializerDeclaration) { |
| 53 | reporter.report(mem, "An interface cannot have initializers."); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | } |
| 58 | ), |
| 59 | new SingleNodeTypeValidator<>(AssignExpr.class, (n, reporter) -> { |
| 60 | // https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26 |
| 61 | Expression target = n.getTarget(); |
| 62 | while (target instanceof EnclosedExpr) { |
| 63 | target = ((EnclosedExpr) target).getInner(); |
| 64 | } |
| 65 | if (target instanceof NameExpr |
| 66 | || target instanceof ArrayAccessExpr |
| 67 | || target instanceof FieldAccessExpr) { |
| 68 | return; |
| 69 | } |
| 70 | reporter.report(n.getTarget(), "Illegal left hand side of an assignment."); |
| 71 | } |
| 72 | ), |
| 73 | new TreeVisitorValidator((node, problemReporter) -> { |
| 74 | NodeMetaModel mm = node.getMetaModel(); |
| 75 | for (PropertyMetaModel ppm : mm.getAllPropertyMetaModels()) { |
| 76 | if (ppm.isNonEmpty()) { |
| 77 | if (ppm.isNodeList()) { |
| 78 | NodeList<?> value = (NodeList<?>) ppm.getValue(node); |
| 79 | if (value.isEmpty()) { |
| 80 | problemReporter.report(node, "%s.%s can not be empty.", mm.getTypeName(), ppm.getName()); |
| 81 | } |
| 82 | } |
| 83 | // No need to check empty strings, it should be impossible to set them to "" |
| 84 | } |
| 85 | } |
| 86 | }) |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 |
Members