1 4 package com.puppycrawl.tools.checkstyle.bcel.checks; 5 6 import org.apache.bcel.classfile.FieldOrMethod; 7 import org.apache.bcel.classfile.JavaClass; 8 import org.apache.commons.beanutils.ConversionException; 9 import java.util.regex.Pattern ; 10 import java.util.regex.PatternSyntaxException ; 11 12 import com.puppycrawl.tools.checkstyle.api.Scope; 13 import com.puppycrawl.tools.checkstyle.api.Utils; 14 import com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor; 15 import com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor; 16 import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor; 17 import com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition; 18 import com.puppycrawl.tools.checkstyle.bcel.classfile.ReferenceDAO; 19 20 24 public abstract class AbstractReferenceCheck 25 extends AbstractCheckVisitor 26 { 27 28 private Scope mScope = Scope.PRIVATE; 29 30 31 private Pattern mIgnoreClassNameRegexp; 32 33 34 private Pattern mIgnoreNameRegexp; 35 36 40 public AbstractReferenceCheck() 41 { 42 setIgnoreClassName("^$"); 43 setIgnoreName("^$"); 44 } 45 46 50 public void setScope(String aScopeName) 51 { 52 mScope = Scope.getInstance(aScopeName); 53 } 54 55 61 protected boolean ignore(String aClassName, FieldOrMethod aFieldOrMethod) 62 { 63 final String fieldOrMethodName = aFieldOrMethod.getName(); 64 return (!equalScope(aFieldOrMethod) 65 || mIgnoreClassNameRegexp.matcher(aClassName).matches() 66 || mIgnoreNameRegexp.matcher(fieldOrMethodName).matches()); 67 } 68 69 77 private boolean equalScope(FieldOrMethod aFieldOrMethod) 78 { 79 if (aFieldOrMethod.isPrivate()) { 80 return (mScope == Scope.PRIVATE); 81 } 82 else if (aFieldOrMethod.isProtected()) { 83 return (mScope == Scope.PROTECTED); 84 } 85 else if (aFieldOrMethod.isPublic()) { 86 return (mScope == Scope.PUBLIC); 87 } 88 else { 89 return (mScope == Scope.PACKAGE); 90 } 91 } 92 93 94 99 public void setIgnoreClassName(String aFormat) 100 throws ConversionException 101 { 102 try { 103 mIgnoreClassNameRegexp = Utils.getPattern(aFormat); 104 } 105 catch (PatternSyntaxException e) { 106 throw new ConversionException("unable to parse " + aFormat, e); 107 } 108 } 109 110 115 public void setIgnoreName(String aFormat) 116 throws ConversionException 117 { 118 try { 119 mIgnoreNameRegexp = Utils.getPattern(aFormat); 120 } 121 catch (PatternSyntaxException e) { 122 throw new ConversionException("unable to parse " + aFormat, e); 123 } 124 } 125 126 127 public IDeepVisitor getVisitor() 128 { 129 return ReferenceVisitor.getInstance(); 130 } 131 132 136 public ReferenceDAO getReferenceDAO() 137 { 138 return ((ReferenceVisitor) getVisitor()).getReferenceDAO(); 139 } 140 141 146 protected JavaClassDefinition findJavaClassDef(JavaClass aJavaClass) 147 { 148 return getReferenceDAO().findJavaClassDef(aJavaClass); 149 } 150 } 151 | Popular Tags |