|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guard | Line # 25 | 89.5% |
0.8947368
|
9.09 | 21 | 9 | 9 | 0.43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (7) | |||
| Result | |||
|
0.8947368
|
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheckTest.testTwo
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheckTest.testTwo
|
1 PASS | |
|
0.81578946
|
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheckTest.testOne
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheckTest.testOne
|
1 PASS | |
|
0.57894737
|
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testPkgGuard2
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testPkgGuard2
|
1 PASS | |
|
0.5263158
|
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testPkgGuard1
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testPkgGuard1
|
1 PASS | |
|
0.47368422
|
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testClassGuard
com.puppycrawl.tools.checkstyle.checks.imports.GuardTest.testClassGuard
|
1 PASS | |
|
0.4473684
|
com.puppycrawl.tools.checkstyle.checks.imports.PkgControlTest.testCheckAccess
com.puppycrawl.tools.checkstyle.checks.imports.PkgControlTest.testCheckAccess
|
1 PASS | |
|
0.31578946
|
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlLoaderTest.testLoad
com.puppycrawl.tools.checkstyle.checks.imports.ImportControlLoaderTest.testLoad
|
1 PASS | |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2005 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.imports; | |
| 20 | ||
| 21 | /** | |
| 22 | * Represents whether a package is allowed to be used or not. | |
| 23 | * @author Oliver Burn | |
| 24 | */ | |
| 25 | class Guard | |
| 26 | { | |
| 27 | /** Indicates if allow access or not. */ | |
| 28 | private final boolean mAllowed; | |
| 29 | /** Package to control access to. */ | |
| 30 | private final String mPkgName; | |
| 31 | /** Package to control access to. */ | |
| 32 | private final String mClassName; | |
| 33 | ||
| 34 | /** | |
| 35 | * Indicates if must be an exact match. Only valid if guard using a | |
| 36 | * package. | |
| 37 | */ | |
| 38 | private final boolean mExactMatch; | |
| 39 | /** Indicates if the guard only applies to this package. */ | |
| 40 | private final boolean mLocalOnly; | |
| 41 | ||
| 42 | /** | |
| 43 | * Constructs an instance. | |
| 44 | * @param aAllow whether to allow access. | |
| 45 | * @param aLocalOnly whether guard is to be applied locally only | |
| 46 | * @param aPkgName the package to apply guard on. | |
| 47 | * @param aExactMatch whether the package must match exactly. | |
| 48 | */ | |
| 49 | 25 |
Guard(final boolean aAllow, final boolean aLocalOnly, |
| 50 | final String aPkgName, final boolean aExactMatch) | |
| 51 | { | |
| 52 | 25 | mAllowed = aAllow; |
| 53 | 25 | mLocalOnly = aLocalOnly; |
| 54 | 25 | mPkgName = aPkgName; |
| 55 | 25 | mClassName = null; |
| 56 | 25 | mExactMatch = aExactMatch; |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Constructs an instance. | |
| 61 | * @param aAllow whether to allow access. | |
| 62 | * @param aLocalOnly whether guard is to be applied locally only | |
| 63 | * @param aClassName the class to apply guard on. | |
| 64 | */ | |
| 65 | 10 |
Guard(final boolean aAllow, final boolean aLocalOnly, |
| 66 | final String aClassName) | |
| 67 | { | |
| 68 | 10 | mAllowed = aAllow; |
| 69 | 10 | mLocalOnly = aLocalOnly; |
| 70 | 10 | mPkgName = null; |
| 71 | 10 | mClassName = aClassName; |
| 72 | 10 | mExactMatch = true; // not used. |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Verifies whether a package name be used. | |
| 77 | * @param aForImport the package to check. | |
| 78 | * @return a result {@link AccessResult} indicating whether it can be used. | |
| 79 | */ | |
| 80 | 55 |
AccessResult verifyImport(final String aForImport) |
| 81 | { | |
| 82 | 0 | assert aForImport != null; |
| 83 | 55 | if (mClassName != null) { |
| 84 | 16 | final boolean classMatch = mClassName.equals(aForImport); |
| 85 | 16 | return calculateResult(classMatch); |
| 86 | } | |
| 87 | ||
| 88 | // Must be checking a package. First check that we actually match | |
| 89 | // the package. Then check if matched and we must be an exact match. | |
| 90 | // In this case, the text after the first "." must not contain | |
| 91 | // another "." as this indicates that it is not an exact match. | |
| 92 | 0 | assert mPkgName != null; |
| 93 | //boolean pkgMatch = aForImport.startsWith(mPkgName + "."); | |
| 94 | 39 | boolean pkgMatch = aForImport.startsWith(mPkgName + "."); |
| 95 | 39 | if (pkgMatch && mExactMatch) { |
| 96 | 4 | pkgMatch = (aForImport.indexOf('.', (mPkgName.length() + 1)) == -1); |
| 97 | } | |
| 98 | 39 | return calculateResult(pkgMatch); |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * @return returns whether the guard is to only be applied locally. | |
| 103 | */ | |
| 104 | 43 |
boolean isLocalOnly() |
| 105 | { | |
| 106 | 43 | return mLocalOnly; |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Returns the appropriate {@link AccessResult} based on whether there | |
| 111 | * was a match and if the guard is to allow access. | |
| 112 | * @param aMatched indicates whether there was a match. | |
| 113 | * @return An appropriate {@link AccessResult}. | |
| 114 | */ | |
| 115 | 55 |
private AccessResult calculateResult(final boolean aMatched) |
| 116 | { | |
| 117 | 55 | if (aMatched) { |
| 118 | 13 | return mAllowed ? AccessResult.ALLOWED : AccessResult.DISALLOWED; |
| 119 | } | |
| 120 | 42 | return AccessResult.UNKNOWN; |
| 121 | } | |
| 122 | } | |
|
|||||||||||||