|
| 1 | +/* |
| 2 | + * Copyright 2019 Philipp Salvisberg <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 |
| 5 | + * Unported License (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://creativecommons.org/licenses/by-nc-nd/3.0/ |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.trivadis.tvdcc.validators; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.eclipse.emf.ecore.EObject; |
| 23 | +import org.eclipse.emf.ecore.EPackage; |
| 24 | +import org.eclipse.xtext.EcoreUtil2; |
| 25 | +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
| 26 | +import org.eclipse.xtext.validation.Check; |
| 27 | +import org.eclipse.xtext.validation.EValidatorRegistrar; |
| 28 | + |
| 29 | +import com.trivadis.oracle.plsql.plsql.BinaryCompoundExpressionLevel6; |
| 30 | +import com.trivadis.oracle.plsql.plsql.BinaryCompoundExpressionLevel7; |
| 31 | +import com.trivadis.oracle.plsql.plsql.CollectionTypeDefinition; |
| 32 | +import com.trivadis.oracle.plsql.plsql.ConstantDeclaration; |
| 33 | +import com.trivadis.oracle.plsql.plsql.PLSQLFile; |
| 34 | +import com.trivadis.oracle.plsql.plsql.SimpleExpressionNameValue; |
| 35 | +import com.trivadis.oracle.plsql.plsql.SimpleExpressionNumberValue; |
| 36 | +import com.trivadis.oracle.plsql.plsql.SimpleExpressionStringValue; |
| 37 | +import com.trivadis.oracle.plsql.plsql.UserDefinedType; |
| 38 | +import com.trivadis.oracle.plsql.validation.PLSQLCopValidator; |
| 39 | + |
| 40 | +public class OverrideTrivadisGuidelines extends TrivadisGuidelines3 implements PLSQLCopValidator { |
| 41 | + |
| 42 | + // must be overridden to avoid duplicate issues when used via ComposedChecks |
| 43 | + @Override |
| 44 | + public void register(EValidatorRegistrar registrar) { |
| 45 | + List<EPackage> ePackages = getEPackages(); |
| 46 | + if (registrar.getRegistry().get(ePackages.get(0)) == null) { |
| 47 | + // standalone validator, default registration required |
| 48 | + super.register(registrar); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public boolean isConstantDeclaration( |
| 53 | + |
| 54 | + final EObject obj) { |
| 55 | + ConstantDeclaration constantDeclaration = EcoreUtil2.getContainerOfType(obj, ConstantDeclaration.class); |
| 56 | + return (constantDeclaration != null); |
| 57 | + } |
| 58 | + |
| 59 | + public boolean isLoggerCall(EObject obj) { |
| 60 | + if (obj == null) { |
| 61 | + return false; |
| 62 | + } else { |
| 63 | + BinaryCompoundExpressionLevel7 func = EcoreUtil2.getContainerOfType(obj, BinaryCompoundExpressionLevel7.class); |
| 64 | + if (func == null) { |
| 65 | + return false; |
| 66 | + } else { |
| 67 | + if (func.getLeft() instanceof SimpleExpressionNameValue) { |
| 68 | + if (func.eContainer() instanceof BinaryCompoundExpressionLevel6) { |
| 69 | + BinaryCompoundExpressionLevel6 pkg = (BinaryCompoundExpressionLevel6) func.eContainer(); |
| 70 | + if (pkg.getBinaryOperator().equals(".")) { |
| 71 | + if (pkg.getLeft() instanceof SimpleExpressionNameValue) { |
| 72 | + SimpleExpressionNameValue pkgName = (SimpleExpressionNameValue) pkg.getLeft(); |
| 73 | + if (pkgName.getValue().equalsIgnoreCase("logger")) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return isLoggerCall(func.eContainer()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + * Override G-1050: Avoid using literals in your code. (guidelines version 3.x) |
| 87 | + * Override G-05: Avoid using literals in your code. (guidelines version 2.x) |
| 88 | + * re-implement existing functionality while ignoring logger calls |
| 89 | + * "&& !isLoggerCall(obj)" is the only addition to the original code |
| 90 | + */ |
| 91 | + @Check |
| 92 | + @Override |
| 93 | + public void checkGuideline5(PLSQLFile file) { |
| 94 | + HashMap<String, Integer> map = new HashMap<>(); |
| 95 | + int threshold = Integer.parseInt(System.getProperty(COP_1050_THRESHOLD, "2")); |
| 96 | + List<EObject> warnings = new ArrayList<>(); |
| 97 | + List<SimpleExpressionNumberValue> numbers = EcoreUtil2.getAllContentsOfType(file, SimpleExpressionNumberValue.class); |
| 98 | + for (SimpleExpressionNumberValue literal : numbers) { |
| 99 | + ConstantDeclaration declartion = EcoreUtil2.getContainerOfType(literal, ConstantDeclaration.class); |
| 100 | + UserDefinedType udf = EcoreUtil2.getContainerOfType(literal, UserDefinedType.class); |
| 101 | + CollectionTypeDefinition collDef = EcoreUtil2.getContainerOfType(literal, CollectionTypeDefinition.class); |
| 102 | + if (declartion == null && udf == null && collDef == null && !literal.getValue().replace(".", "").equals("0") |
| 103 | + && !literal.getValue().replace(".", "").equals("1")) { |
| 104 | + updateLiteralMap(literal, map, threshold); |
| 105 | + warnings.add(literal); |
| 106 | + } |
| 107 | + } |
| 108 | + List<SimpleExpressionStringValue> strings = EcoreUtil2.getAllContentsOfType(file, SimpleExpressionStringValue.class); |
| 109 | + for (SimpleExpressionStringValue literal : strings) { |
| 110 | + ConstantDeclaration declartion = EcoreUtil2.getContainerOfType(literal, ConstantDeclaration.class); |
| 111 | + if (declartion == null) { |
| 112 | + UserDefinedType udf = EcoreUtil2.getContainerOfType(literal, UserDefinedType.class); |
| 113 | + if (udf == null) { |
| 114 | + updateLiteralMap(literal, map, threshold); |
| 115 | + warnings.add(literal); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + for (EObject obj : warnings) { |
| 120 | + if (!withinThreshold(obj, map, threshold) && !isLoggerCall(obj)) { |
| 121 | + warning(getGuidelineMsg(5), obj, null, getGuidelineId(5), serialize(NodeModelUtils.getNode(obj))); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments