idx
int64 0
41.2k
| question
stringlengths 83
4.15k
| target
stringlengths 5
715
|
|---|---|---|
3,000
|
private boolean updateExceptionRegister ( CatchInfo ci , int seen , int pc ) { if ( OpcodeUtils . isAStore ( seen ) ) { int reg = RegisterUtils . getAStoreReg ( this , seen ) ; ci . setReg ( reg ) ; exReg . put ( Integer . valueOf ( reg ) , Boolean . TRUE ) ; LocalVariableTable lvt = getMethod ( ) . getLocalVariableTable ( ) ; if ( lvt != null ) { LocalVariable lv = lvt . getLocalVariable ( reg , pc + 2 ) ; if ( lv != null ) { int finish = lv . getStartPC ( ) + lv . getLength ( ) ; if ( finish < ci . getFinish ( ) ) { ci . setFinish ( finish ) ; } } else { return false ; } } } return true ; }
|
looks to update the catchinfo block with the register used for the exception variable . If their is a local variable table but the local variable can t be found return false signifying an empty catch block .
|
3,001
|
private void addCatchBlock ( int start , int finish ) { CatchInfo ci = new CatchInfo ( start , finish ) ; catchInfos . add ( ci ) ; }
|
add a catch block info record for the catch block that is guessed to be in the range of start to finish
|
3,002
|
public void visitClassContext ( ClassContext classContext ) { try { javaClass = classContext . getJavaClass ( ) ; if ( javaClass . getMajor ( ) >= Const . MAJOR_1_5 ) { javaClass . accept ( this ) ; } } finally { javaClass = null ; } }
|
overrides the visitor to make sure that the class was compiled by java 1 . 5 or later .
|
3,003
|
public void visitMethod ( Method obj ) { try { if ( obj . isSynthetic ( ) ) { return ; } if ( Values . CONSTRUCTOR . equals ( getMethodName ( ) ) && javaClass . getClassName ( ) . contains ( "$" ) ) { return ; } List < String > types = SignatureUtils . getParameterSignatures ( obj . getSignature ( ) ) ; if ( ( types . isEmpty ( ) ) || ( types . size ( ) > 2 ) ) { return ; } if ( ( obj . getAccessFlags ( ) & Const . ACC_VARARGS ) != 0 ) { return ; } String lastParmSig = types . get ( types . size ( ) - 1 ) ; if ( ! lastParmSig . startsWith ( Values . SIG_ARRAY_PREFIX ) || lastParmSig . startsWith ( Values . SIG_ARRAY_OF_ARRAYS_PREFIX ) ) { return ; } if ( SignatureBuilder . SIG_BYTE_ARRAY . equals ( lastParmSig ) || SignatureBuilder . SIG_CHAR_ARRAY . equals ( lastParmSig ) ) { return ; } if ( hasSimilarParms ( types ) ) { return ; } if ( obj . isStatic ( ) && "main" . equals ( obj . getName ( ) ) && SIG_STRING_ARRAY_TO_VOID . equals ( obj . getSignature ( ) ) ) { return ; } if ( ! obj . isPrivate ( ) && ! obj . isStatic ( ) && isInherited ( obj ) ) { return ; } super . visitMethod ( obj ) ; bugReporter . reportBug ( new BugInstance ( this , BugType . UVA_USE_VAR_ARGS . name ( ) , LOW_PRIORITY ) . addClass ( this ) . addMethod ( this ) ) ; } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } }
|
overrides the visitor to look for methods that has an array as a last parameter of an array type where the base type is not like the previous parameter nor something like a char or byte array .
|
3,004
|
@ edu . umd . cs . findbugs . annotations . SuppressFBWarnings ( value = "LII_LIST_INDEXED_ITERATING" , justification = "this doesn't iterate over every element, so we can't use a for-each loop" ) private static boolean hasSimilarParms ( List < String > argTypes ) { for ( int i = 0 ; i < ( argTypes . size ( ) - 1 ) ; i ++ ) { if ( argTypes . get ( i ) . startsWith ( Values . SIG_ARRAY_PREFIX ) ) { return true ; } } String baseType = argTypes . get ( argTypes . size ( ) - 1 ) ; while ( baseType . startsWith ( Values . SIG_ARRAY_PREFIX ) ) { baseType = baseType . substring ( 1 ) ; } for ( int i = 0 ; i < ( argTypes . size ( ) - 1 ) ; i ++ ) { if ( argTypes . get ( i ) . equals ( baseType ) ) { return true ; } } return false ; }
|
determines whether a bunch of types are similar and thus would be confusing to have one be a varargs .
|
3,005
|
private boolean isInherited ( Method m ) throws ClassNotFoundException { JavaClass [ ] infs = javaClass . getAllInterfaces ( ) ; for ( JavaClass inf : infs ) { if ( hasMethod ( inf , m ) ) { return true ; } } JavaClass [ ] sups = javaClass . getSuperClasses ( ) ; for ( JavaClass sup : sups ) { if ( hasMethod ( sup , m ) ) { return true ; } } return false ; }
|
looks to see if this method is derived from a super class . If it is we don t want to report on it as that would entail changing a whole hierarchy
|
3,006
|
private static boolean hasMethod ( JavaClass c , Method candidateMethod ) { String name = candidateMethod . getName ( ) ; String sig = candidateMethod . getSignature ( ) ; for ( Method method : c . getMethods ( ) ) { if ( ! method . isStatic ( ) && method . getName ( ) . equals ( name ) && method . getSignature ( ) . equals ( sig ) ) { return true ; } } return false ; }
|
looks to see if a class has a method with a specific name and signature
|
3,007
|
public void visitClassContext ( ClassContext context ) { JavaClass cls = context . getJavaClass ( ) ; if ( ! isInternal ( cls . getClassName ( ) ) ) { ConstantPool pool = cls . getConstantPool ( ) ; int numItems = pool . getLength ( ) ; for ( int i = 0 ; i < numItems ; i ++ ) { Constant c = pool . getConstant ( i ) ; if ( c instanceof ConstantClass ) { String clsName = ( ( ConstantClass ) c ) . getBytes ( pool ) ; if ( isInternal ( clsName ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . IICU_INCORRECT_INTERNAL_CLASS_USE . name ( ) , NORMAL_PRIORITY ) . addClass ( cls ) . addString ( clsName ) ) ; } } } } }
|
implements the visitor to look for classes that reference com . sun . xxx or org . apache . xerces . xxx classes by looking for class Const in the constant pool
|
3,008
|
private static boolean isInternal ( String clsName ) { boolean internal = false ; for ( String internalPackage : internalPackages ) { if ( clsName . startsWith ( internalPackage ) ) { internal = true ; break ; } } if ( internal ) { for ( String externalPackage : externalPackages ) { if ( clsName . startsWith ( externalPackage ) ) { internal = false ; break ; } } } return internal ; }
|
determines if the class in question is an internal class by looking at package prefixes
|
3,009
|
private Set < String > getDependenciesForClass ( String clsName ) { Set < String > dependencies = dependencyGraph . get ( clsName ) ; if ( dependencies == null ) { dependencies = new HashSet < > ( ) ; dependencyGraph . put ( clsName , dependencies ) ; } return dependencies ; }
|
returns a set of dependent class names for a class and if it doesn t exist create the set install it and then return ;
|
3,010
|
public void visitClassContext ( final ClassContext classContext ) { try { stack = new OpcodeStack ( ) ; memberCollections = new HashMap < > ( ) ; memberSourceLineAnnotations = new HashMap < > ( ) ; super . visitClassContext ( classContext ) ; } finally { stack = null ; memberCollections = null ; memberSourceLineAnnotations = null ; } }
|
implements the visitor to create and destroy the stack and member collections
|
3,011
|
public void visitCode ( final Code obj ) { try { localCollections = new HashMap < > ( ) ; localScopeEnds = new HashMap < > ( ) ; localSourceLineAnnotations = new HashMap < > ( ) ; stack . resetForMethodEntry ( this ) ; super . visitCode ( obj ) ; } finally { localCollections = null ; localScopeEnds = null ; localSourceLineAnnotations = null ; } }
|
implements the visitor to reset the opcode stack and clear the various collections
|
3,012
|
public void sawOpcode ( final int seen ) { try { stack . precomputation ( this ) ; BitSet regs = localScopeEnds . remove ( Integer . valueOf ( getPC ( ) ) ) ; if ( regs != null ) { int i = regs . nextSetBit ( 0 ) ; while ( i >= 0 ) { localCollections . remove ( i ) ; localSourceLineAnnotations . remove ( i ) ; i = regs . nextSetBit ( i + 1 ) ; } } if ( seen == Const . INVOKEINTERFACE ) { String className = getClassConstantOperand ( ) ; if ( COLLECTION_CLASSES . contains ( className ) ) { String methodName = getNameConstantOperand ( ) ; String methodSig = getSigConstantOperand ( ) ; if ( "add" . equals ( methodName ) && SignatureBuilder . SIG_OBJECT_TO_BOOLEAN . equals ( methodSig ) ) { if ( stack . getStackDepth ( ) > 1 ) { OpcodeStack . Item colItm = stack . getStackItem ( 1 ) ; OpcodeStack . Item addItm = stack . getStackItem ( 0 ) ; checkAdd ( colItm , addItm ) ; } } else if ( ( "put" . equals ( methodName ) && SignatureBuilder . SIG_TWO_OBJECTS_TO_OBJECT . equals ( methodSig ) ) && ( stack . getStackDepth ( ) > 2 ) ) { OpcodeStack . Item colItm = stack . getStackItem ( 2 ) ; OpcodeStack . Item addItm = stack . getStackItem ( 1 ) ; checkAdd ( colItm , addItm ) ; } } } else if ( seen == Const . AASTORE ) { if ( stack . getStackDepth ( ) > 2 ) { OpcodeStack . Item arrayItm = stack . getStackItem ( 2 ) ; OpcodeStack . Item addItm = stack . getStackItem ( 0 ) ; checkAdd ( arrayItm , addItm ) ; } } else if ( OpcodeUtils . isAStore ( seen ) ) { Integer reg = Integer . valueOf ( RegisterUtils . getAStoreReg ( this , seen ) ) ; localCollections . remove ( reg ) ; localSourceLineAnnotations . remove ( reg ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } finally { stack . sawOpcode ( this , seen ) ; } }
|
implements the visitor to look for collection method calls that put objects into the collection that are unrelated by anything besides java . lang . Objecct
|
3,013
|
private void mergeItem ( final Set < String > supers , final Set < SourceLineAnnotation > sla , final OpcodeStack . Item addItm ) throws ClassNotFoundException { if ( supers . isEmpty ( ) ) { return ; } Set < String > s = new HashSet < > ( ) ; addNewItem ( s , addItm ) ; if ( s . isEmpty ( ) ) { return ; } intersection ( supers , s ) ; if ( supers . isEmpty ( ) ) { BugInstance bug = new BugInstance ( this , BugType . UCC_UNRELATED_COLLECTION_CONTENTS . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) ; if ( addItm . getRegisterNumber ( ) != - 1 ) { bug . addMethod ( this ) ; } for ( SourceLineAnnotation a : sla ) { bug . addSourceLine ( a ) ; } bugReporter . reportBug ( bug ) ; } }
|
intersects the set of possible superclass that this collection might have seen before with this one . If we find that there is no commonality between superclasses report it as a bug .
|
3,014
|
public void visitClassContext ( ClassContext classContext ) { try { JavaClass cls = classContext . getJavaClass ( ) ; if ( isSingleton ( cls ) ) { syncBlockBranchResetValues = new HashMap < > ( ) ; stack = new OpcodeStack ( ) ; super . visitClassContext ( classContext ) ; } } finally { syncBlockBranchResetValues = null ; stack = null ; } }
|
implements the visitor to look for classes that are singletons
|
3,015
|
public void visitCode ( Code obj ) { Method m = getMethod ( ) ; if ( isIgnorableMethod ( m ) ) { return ; } stack . resetForMethodEntry ( this ) ; syncBlockBranchResetValues . clear ( ) ; syncBlockCount = 0 ; super . visitCode ( obj ) ; }
|
implements the visitor to look for methods that could possibly be normal methods where a field is written to
|
3,016
|
public void sawOpcode ( int seen ) { boolean sawResource = false ; try { stack . precomputation ( this ) ; if ( seen == Const . INVOKEVIRTUAL ) { sawResource = processInvokeVirtual ( ) ; } else if ( seen == Const . INVOKESPECIAL ) { sawResource = processInvokeSpecial ( ) ; } } finally { stack . sawOpcode ( this , seen ) ; if ( sawResource && ( stack . getStackDepth ( ) > 0 ) ) { OpcodeStack . Item item = stack . getStackItem ( 0 ) ; item . setUserValue ( Boolean . TRUE ) ; } } }
|
overrides the visitor to look conflated use of resources and files
|
3,017
|
public void visitClassContext ( ClassContext classContext ) { try { stack = new OpcodeStack ( ) ; localMethodCalls = new HashMap < > ( ) ; fieldMethodCalls = new HashMap < > ( ) ; staticMethodCalls = new HashMap < > ( ) ; branchTargets = new BitSet ( ) ; super . visitClassContext ( classContext ) ; } finally { stack = null ; localMethodCalls = null ; fieldMethodCalls = null ; staticMethodCalls = null ; branchTargets = null ; } }
|
implements the visitor to create and clear the stack method call maps and branch targets
|
3,018
|
private static int getBugPriority ( String methodName , MethodInfo mi ) { if ( Values . STATIC_INITIALIZER . equals ( methodName ) ) { return LOW_PRIORITY ; } if ( ( mi . getNumBytes ( ) >= highByteCountLimit ) || ( mi . getNumMethodCalls ( ) >= highMethodCallLimit ) ) { return HIGH_PRIORITY ; } if ( ( mi . getNumBytes ( ) >= normalByteCountLimit ) || ( mi . getNumMethodCalls ( ) >= normalMethodCallLimit ) ) { return NORMAL_PRIORITY ; } if ( ( mi . getNumBytes ( ) >= lowByteCountLimit ) || ( mi . getNumMethodCalls ( ) >= lowMethodCallLimit ) ) { return LOW_PRIORITY ; } return EXP_PRIORITY ; }
|
returns the bug priority based on metrics about the method
|
3,019
|
private static boolean isRiskyName ( String className , String methodName ) { if ( riskyClassNames . contains ( className ) ) { return true ; } String qualifiedMethodName = className + '.' + methodName ; if ( riskyMethodNameContents . contains ( qualifiedMethodName ) ) { return true ; } for ( String riskyName : riskyMethodNameContents ) { if ( methodName . indexOf ( riskyName ) >= 0 ) { return true ; } } return methodName . indexOf ( Values . SYNTHETIC_MEMBER_CHAR ) >= 0 ; }
|
returns true if the class or method name contains a pattern that is considered likely to be this modifying
|
3,020
|
private int getLineNumber ( int pc ) { LineNumberTable lnt = getMethod ( ) . getLineNumberTable ( ) ; if ( lnt == null ) { return pc ; } LineNumber [ ] lns = lnt . getLineNumberTable ( ) ; if ( lns == null ) { return pc ; } if ( pc > lns [ lns . length - 1 ] . getStartPC ( ) ) { return lns [ lns . length - 1 ] . getLineNumber ( ) ; } int lo = 0 ; int hi = lns . length - 2 ; int mid = 0 ; while ( lo <= hi ) { mid = ( lo + hi ) >>> 1 ; if ( pc < lns [ mid ] . getStartPC ( ) ) { hi = mid - 1 ; } else if ( pc >= lns [ mid + 1 ] . getStartPC ( ) ) { lo = mid + 1 ; } else { break ; } } return lns [ mid ] . getLineNumber ( ) ; }
|
returns the source line number for the pc or just the pc if the line number table doesn t exist
|
3,021
|
public void visitClassContext ( ClassContext classContext ) { if ( cloneClass == null ) { return ; } try { cls = classContext . getJavaClass ( ) ; if ( cls . implementationOf ( cloneClass ) ) { clsName = cls . getClassName ( ) ; stack = new OpcodeStack ( ) ; super . visitClassContext ( classContext ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } finally { cls = null ; stack = null ; } }
|
overrides the visitor to check for classes that implement Cloneable .
|
3,022
|
public void sawOpcode ( int seen ) { try { if ( ( seen == Const . ATHROW ) && ( stack . getStackDepth ( ) > 0 ) ) { OpcodeStack . Item item = stack . getStackItem ( 0 ) ; if ( "Ljava/lang/CloneNotSupportedException;" . equals ( item . getSignature ( ) ) ) { throwsCNFE = true ; } } } finally { stack . sawOpcode ( this , seen ) ; } }
|
overrides the visitor to look for a CloneNotSupported being thrown
|
3,023
|
public void visitCode ( Code obj ) { try { lnTable = obj . getLineNumberTable ( ) ; if ( lnTable != null ) { state = State . SEEN_NOTHING ; invokePC = - 1 ; returnType = null ; super . visitCode ( obj ) ; } } finally { lnTable = null ; } }
|
overrides the interface to collect the line number table and reset state
|
3,024
|
public void sawOpcode ( int seen ) { switch ( state ) { case SEEN_NOTHING : if ( ( seen == Const . INVOKEINTERFACE ) || ( seen == Const . INVOKEVIRTUAL ) ) { String sig = getSigConstantOperand ( ) ; String returnSig = SignatureUtils . getReturnSignature ( sig ) ; if ( returnSig . startsWith ( Values . SIG_QUALIFIED_CLASS_PREFIX ) ) { String clsName = getClassConstantOperand ( ) ; if ( ! Values . SLASHED_JAVA_LANG_OBJECT . equals ( clsName ) && ! Values . SLASHED_JAVA_LANG_CLASS . equals ( clsName ) ) { returnType = SignatureUtils . trimSignature ( returnSig ) ; invokePC = getPC ( ) ; state = State . SEEN_INVOKE ; } } } break ; case SEEN_INVOKE : if ( seen == Const . POP ) { state = State . SEEN_POP ; } else { state = State . SEEN_NOTHING ; returnType = null ; } break ; case SEEN_POP : if ( ( ( seen >= Const . ACONST_NULL ) && ( seen <= Const . DCONST_1 ) ) || ( seen == Const . GETFIELD ) ) { state = State . SEEN_POP ; } else if ( ( seen == Const . INVOKESTATIC ) || ( seen == Const . GETSTATIC ) ) { if ( getClassConstantOperand ( ) . equals ( returnType ) && ( lnTable . getSourceLine ( invokePC ) == lnTable . getSourceLine ( getPC ( ) ) ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . NIR_NEEDLESS_INSTANCE_RETRIEVAL . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } state = State . SEEN_NOTHING ; returnType = null ; } else { state = State . SEEN_NOTHING ; returnType = null ; } break ; default : state = State . SEEN_NOTHING ; returnType = null ; break ; } }
|
overrides the interface to find accesses of static variables off of an instance immediately fetched from a method call .
|
3,025
|
public void visitClassContext ( final ClassContext context ) { try { classContext = context ; classContext . getJavaClass ( ) . accept ( this ) ; } finally { classContext = null ; } }
|
overrides the visitor to store the class context
|
3,026
|
public void visitMethod ( final Method obj ) { try { if ( obj . isSynthetic ( ) ) { return ; } Code code = obj . getCode ( ) ; if ( code == null ) { return ; } if ( code . getCode ( ) . length < ( 2 * reportLimit ) ) { return ; } BitSet exceptionNodeTargets = new BitSet ( ) ; CFG cfg = classContext . getCFG ( obj ) ; int branches = 0 ; Iterator < BasicBlock > bbi = cfg . blockIterator ( ) ; while ( bbi . hasNext ( ) ) { BasicBlock bb = bbi . next ( ) ; Iterator < Edge > iei = cfg . outgoingEdgeIterator ( bb ) ; int lastSwitchTargetBlockLabel = Integer . MIN_VALUE ; while ( iei . hasNext ( ) ) { Edge e = iei . next ( ) ; int edgeType = e . getType ( ) ; if ( ( edgeType != EdgeTypes . FALL_THROUGH_EDGE ) && ( edgeType != EdgeTypes . RETURN_EDGE ) && ( edgeType != EdgeTypes . UNKNOWN_EDGE ) ) { if ( ( edgeType == EdgeTypes . UNHANDLED_EXCEPTION_EDGE ) || ( edgeType == EdgeTypes . HANDLED_EXCEPTION_EDGE ) ) { int nodeTarget = e . getTarget ( ) . getLabel ( ) ; if ( ! exceptionNodeTargets . get ( nodeTarget ) ) { exceptionNodeTargets . set ( nodeTarget ) ; branches ++ ; } } else if ( ( edgeType == EdgeTypes . SWITCH_EDGE ) || ( edgeType == EdgeTypes . SWITCH_DEFAULT_EDGE ) ) { int nodeTarget = e . getTarget ( ) . getLabel ( ) ; if ( nodeTarget != lastSwitchTargetBlockLabel ) { branches ++ ; } lastSwitchTargetBlockLabel = nodeTarget ; } else { branches ++ ; } } } } if ( branches > reportLimit ) { int priority = ( branches > ( reportLimit * 2 ) ? HIGH_PRIORITY : NORMAL_PRIORITY ) ; BugInstance bug = new BugInstance ( this , BugType . CC_CYCLOMATIC_COMPLEXITY . name ( ) , priority ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( classContext , this , 0 ) . addInt ( branches ) ; bugReporter . reportBug ( bug ) ; } } catch ( CFGBuilderException cbe ) { bugReporter . logError ( "Failure examining basic blocks for method " + classContext . getJavaClass ( ) . getClassName ( ) + '.' + obj . getName ( ) + " in Cyclomatic Complexity detector" , cbe ) ; } }
|
overrides the visitor to navigate the basic block list to count branches
|
3,027
|
public void visitCode ( Code obj ) { Method m = getMethod ( ) ; if ( m . isSynthetic ( ) ) { return ; } isPublic = m . isPublic ( ) ; stack . resetForMethodEntry ( this ) ; super . visitCode ( obj ) ; }
|
implements the visitor to reset the opcode stack Note that the synthetic check is done in both visitMethod and visitCode as visitMethod is not a proper listener stopping method . We don t want to report issues reported in visitMethod if it is synthetic but we also don t want it to get into sawOpcode so that is why it is done here as well .
|
3,028
|
private void catalogClass ( JavaClass clz ) { transactionalMethods = new HashMap < > ( ) ; isEntity = false ; hasId = false ; hasGeneratedValue = false ; hasEagerOneToMany = false ; hasHCEquals = false ; for ( AnnotationEntry entry : clz . getAnnotationEntries ( ) ) { if ( "Ljavax/persistence/Entity;" . equals ( entry . getAnnotationType ( ) ) ) { isEntity = true ; break ; } } for ( Method m : clz . getMethods ( ) ) { catalogFieldOrMethod ( m ) ; if ( ( "equals" . equals ( m . getName ( ) ) && SignatureBuilder . SIG_OBJECT_TO_BOOLEAN . equals ( m . getSignature ( ) ) ) || ( Values . HASHCODE . equals ( m . getName ( ) ) && SignatureBuilder . SIG_VOID_TO_INT . equals ( m . getSignature ( ) ) ) ) { hasHCEquals = true ; } } for ( Field f : clz . getFields ( ) ) { catalogFieldOrMethod ( f ) ; } }
|
parses the current class for spring - tx and jpa annotations as well as hashCode and equals methods .
|
3,029
|
private void catalogFieldOrMethod ( FieldOrMethod fm ) { for ( AnnotationEntry entry : fm . getAnnotationEntries ( ) ) { String type = entry . getAnnotationType ( ) ; switch ( type ) { case "Lorg/springframework/transaction/annotation/Transactional;" : if ( fm instanceof Method ) { boolean isWrite = true ; for ( ElementValuePair pair : entry . getElementValuePairs ( ) ) { if ( "readOnly" . equals ( pair . getNameString ( ) ) ) { isWrite = "false" . equals ( pair . getValue ( ) . stringifyValue ( ) ) ; break ; } } transactionalMethods . put ( new FQMethod ( cls . getClassName ( ) , fm . getName ( ) , fm . getSignature ( ) ) , isWrite ? TransactionalType . WRITE : TransactionalType . READ ) ; } break ; case "Ljavax/persistence/Id;" : hasId = true ; break ; case "Ljavax/persistence/GeneratedValue;" : hasGeneratedValue = true ; break ; case "Ljavax/persistence/OneToMany;" : for ( ElementValuePair pair : entry . getElementValuePairs ( ) ) { if ( "fetch" . equals ( pair . getNameString ( ) ) && "EAGER" . equals ( pair . getValue ( ) . stringifyValue ( ) ) ) { hasEagerOneToMany = true ; break ; } } break ; case "Lorg/hibernate/annotations/Fetch;" : case "Lorg/eclipse/persistence/annotations/JoinFetch;" : case "Lorg/eclipse/persistence/annotations/BatchFetch;" : hasFetch = true ; break ; default : break ; } } }
|
parses a field or method for spring - tx or jpa annotations
|
3,030
|
private Set < JavaClass > getDeclaredExceptions ( Method method ) throws ClassNotFoundException { ExceptionTable et = method . getExceptionTable ( ) ; if ( ( et == null ) || ( et . getLength ( ) == 0 ) ) { return Collections . < JavaClass > emptySet ( ) ; } Set < JavaClass > exceptions = new HashSet < > ( ) ; for ( String en : et . getExceptionNames ( ) ) { JavaClass exCls = Repository . lookupClass ( en ) ; if ( ! exCls . instanceOf ( runtimeExceptionClass ) ) { exceptions . add ( exCls ) ; } } return exceptions ; }
|
retrieves the set of non - runtime exceptions that are declared to be thrown by the method
|
3,031
|
public void visitCode ( Code obj ) { Method m = getMethod ( ) ; if ( prescreen ( m ) ) { if ( m . isSynchronized ( ) ) { syncPC = 0 ; } else { syncPC = - 1 ; } isStatic = m . isStatic ( ) ; unsafeAliases . clear ( ) ; unsafeAliases . set ( 0 ) ; branchInfo . clear ( ) ; unsafeCallOccurred = false ; stack . resetForMethodEntry ( this ) ; } }
|
implement the visitor to reset the sync count the stack and gather some information
|
3,032
|
public void sawOpcode ( int seen ) { try { stack . precomputation ( this ) ; if ( unsafeCallOccurred && OpcodeUtils . isAStore ( seen ) ) { int storeReg = RegisterUtils . getAStoreReg ( this , seen ) ; if ( storeReg >= 0 ) { unsafeAliases . set ( storeReg ) ; } } if ( ( seen == Const . INVOKEVIRTUAL ) || ( seen == Const . INVOKESPECIAL ) || ( seen == Const . INVOKEINTERFACE ) || ( seen == Const . INVOKEDYNAMIC ) ) { String methodSig = getSigConstantOperand ( ) ; MethodInfo mi = Statistics . getStatistics ( ) . getMethodStatistics ( getClassConstantOperand ( ) , getNameConstantOperand ( ) , methodSig ) ; if ( mi . getModifiesState ( ) ) { unsafeCallOccurred = true ; } else { if ( ! Values . SIG_VOID . equals ( SignatureUtils . getReturnSignature ( methodSig ) ) ) { int parmCount = SignatureUtils . getNumParameters ( methodSig ) ; if ( stack . getStackDepth ( ) > parmCount ) { OpcodeStack . Item itm = stack . getStackItem ( parmCount ) ; unsafeCallOccurred = unsafeAliases . get ( itm . getRegisterNumber ( ) ) ; } else { unsafeCallOccurred = false ; } } else { unsafeCallOccurred = false ; } } } else if ( seen == Const . INVOKESTATIC ) { unsafeCallOccurred = getDottedClassConstantOperand ( ) . equals ( this . getClassContext ( ) . getJavaClass ( ) . getClassName ( ) ) ; } else if ( ( ( seen >= Const . IFEQ ) && ( seen <= Const . GOTO ) ) || ( seen == Const . GOTO_W ) ) { Integer from = Integer . valueOf ( getPC ( ) ) ; Integer to = Integer . valueOf ( getBranchTarget ( ) ) ; branchInfo . put ( from , to ) ; unsafeCallOccurred = false ; } else { unsafeCallOccurred = false ; } if ( seen == Const . MONITORENTER ) { if ( syncPC < 0 ) { syncPC = getPC ( ) ; if ( stack . getStackDepth ( ) > 0 ) { OpcodeStack . Item itm = stack . getStackItem ( 0 ) ; int monitorReg = itm . getRegisterNumber ( ) ; if ( monitorReg >= 0 ) { unsafeAliases . set ( monitorReg ) ; } } } } else if ( seen == Const . MONITOREXIT ) { syncPC = - 1 ; } else if ( syncPC >= 0 ) { processSyncBlockInstruction ( seen ) ; } } finally { stack . sawOpcode ( this , seen ) ; } }
|
implement the visitor to find bloated sync blocks . This implementation only checks the outer most block
|
3,033
|
public void visitClassContext ( ClassContext classContext ) { currentClass = classContext . getJavaClass ( ) ; if ( currentClass . getMajor ( ) >= Const . MAJOR_1_8 ) { try { stack = new OpcodeStack ( ) ; activeStackOps = new ArrayDeque < > ( ) ; super . visitClassContext ( classContext ) ; } finally { activeStackOps = null ; stack = null ; } } currentClass = null ; }
|
implements the visitor to filter out pre - 1 . 8 classes for 1 . 8 + classes it creates the opcode stack and active stack ops
|
3,034
|
public void visitCode ( Code obj ) { stack . resetForMethodEntry ( this ) ; activeStackOps . clear ( ) ; super . visitCode ( obj ) ; }
|
implements the visitor clear the stacks
|
3,035
|
private boolean isTrivialStackOps ( ) { int invokeCount = 0 ; for ( ActiveStackOp op : activeStackOps ) { if ( INVOKE_OPS . get ( op . getOpcode ( ) ) ) { invokeCount ++ ; } } if ( invokeCount == 1 ) { FQMethod method = activeStackOps . getLast ( ) . getMethod ( ) ; if ( method == null ) { return false ; } if ( "valueOf" . equals ( method . getMethodName ( ) ) && method . getClassName ( ) . startsWith ( "java/lang/" ) ) { return true ; } } return false ; }
|
returns whether the set of operations that contributed to the current stack form are trivial or not specifically boxing a primitive value or appending to strings or such .
|
3,036
|
private Method getLambdaMethod ( String methodName ) { for ( Method method : currentClass . getMethods ( ) ) { if ( methodName . equals ( method . getName ( ) ) ) { return method ; } } return null ; }
|
finds the bootstrap method for a lambda ( invokedynamic call . As findbugs doesn t support bcel 6 have to cheat a little here .
|
3,037
|
public void visitClassContext ( ClassContext classContext ) { JavaClass cls = classContext . getJavaClass ( ) ; clsVersion = cls . getMajor ( ) ; super . visitClassContext ( classContext ) ; }
|
overrides the visitor to make sure this is a modern class better than 1 . 4
|
3,038
|
public void sawOpcode ( int seen ) { stack . precomputation ( this ) ; switch ( seen ) { case Const . INVOKESTATIC : { String className = getClassConstantOperand ( ) ; for ( Backports backport : BACKPORTS ) { if ( className . startsWith ( backport . getPathPrefix ( ) ) ) { if ( clsVersion >= backport . getMinimumJDK ( ) ) { reportBug ( backport . getLibrary ( ) ) ; break ; } } } } break ; case Const . INVOKESPECIAL : { String className = getClassConstantOperand ( ) ; String methodName = getNameConstantOperand ( ) ; for ( Backports backport : BACKPORTS ) { if ( Values . CONSTRUCTOR . equals ( methodName ) && className . startsWith ( backport . getPathPrefix ( ) ) && ( clsVersion >= backport . getMinimumJDK ( ) ) ) { reportBug ( backport . getLibrary ( ) ) ; break ; } } } break ; default : break ; } }
|
overrides the visitor to look for method calls to the emory backport concurrent library or threeten bp library when the now built - in versions are available
|
3,039
|
private void reportBug ( Library library ) { bugReporter . reportBug ( new BugInstance ( this , BugType . BRPI_BACKPORT_REUSE_PUBLIC_IDENTIFIERS . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) . addString ( library . name ( ) ) ) ; }
|
issues a new bug at this location
|
3,040
|
public void visitClassContext ( ClassContext classContext ) { try { stack = new OpcodeStack ( ) ; nameOfThisClass = SignatureUtils . getNonAnonymousPortion ( classContext . getJavaClass ( ) . getClassName ( ) ) ; formatterLoggers = new HashSet < > ( ) ; super . visitClassContext ( classContext ) ; } finally { formatterLoggers = null ; stack = null ; } }
|
implements the visitor to discover what the class name is if it is a normal class or the owning class if the class is an anonymous class .
|
3,041
|
@ SuppressWarnings ( "unchecked" ) private void checkForProblemsWithLoggerMethods ( ) throws ClassNotFoundException { String callingClsName = getClassConstantOperand ( ) ; if ( ! ( callingClsName . endsWith ( "Log" ) || ( callingClsName . endsWith ( "Logger" ) ) ) || ( stack . getStackDepth ( ) == 0 ) ) { return ; } String sig = getSigConstantOperand ( ) ; if ( SIG_STRING_AND_THROWABLE_TO_VOID . equals ( sig ) || SIG_OBJECT_AND_THROWABLE_TO_VOID . equals ( sig ) ) { checkForProblemsWithLoggerThrowableMethods ( ) ; } else if ( SignatureBuilder . SIG_OBJECT_TO_VOID . equals ( sig ) ) { checkForProblemsWithLoggerSingleArgumentMethod ( ) ; } else if ( ( SLF4J_LOGGER . equals ( callingClsName ) || LOG4J2_LOGGER . equals ( callingClsName ) ) && ( SignatureBuilder . SIG_STRING_TO_VOID . equals ( sig ) || SignatureBuilder . SIG_STRING_AND_OBJECT_TO_VOID . equals ( sig ) || SIG_STRING_AND_TWO_OBJECTS_TO_VOID . equals ( sig ) || SIG_STRING_AND_OBJECT_ARRAY_TO_VOID . equals ( sig ) ) ) { checkForProblemsWithLoggerParameterisedMethods ( sig ) ; } }
|
looks for a variety of logging issues with log statements
|
3,042
|
@ SuppressWarnings ( "unchecked" ) private int getVarArgsParmCount ( String signature ) { if ( SignatureBuilder . SIG_STRING_AND_OBJECT_TO_VOID . equals ( signature ) ) { return 1 ; } if ( SIG_STRING_AND_TWO_OBJECTS_TO_VOID . equals ( signature ) ) { return 2 ; } OpcodeStack . Item item = stack . getStackItem ( 0 ) ; LOUserValue < Integer > uv = ( LOUserValue < Integer > ) item . getUserValue ( ) ; if ( ( uv != null ) && ( uv . getType ( ) == LOUserValue . LOType . ARRAY_SIZE ) ) { Integer size = uv . getValue ( ) ; if ( size != null ) { return Math . abs ( size . intValue ( ) ) ; } } return - 1 ; }
|
returns the number of parameters slf4j or log4j2 is expecting to inject into the format string
|
3,043
|
@ SuppressWarnings ( "unchecked" ) private boolean hasExceptionOnStack ( ) { try { for ( int i = 0 ; i < ( stack . getStackDepth ( ) - 1 ) ; i ++ ) { OpcodeStack . Item item = stack . getStackItem ( i ) ; String sig = item . getSignature ( ) ; if ( sig . startsWith ( Values . SIG_QUALIFIED_CLASS_PREFIX ) ) { String name = SignatureUtils . stripSignature ( sig ) ; JavaClass cls = Repository . lookupClass ( name ) ; if ( cls . instanceOf ( throwableClass ) ) { return true ; } } else if ( sig . startsWith ( Values . SIG_ARRAY_PREFIX ) ) { LOUserValue < Integer > uv = ( LOUserValue < Integer > ) item . getUserValue ( ) ; if ( ( uv != null ) && ( uv . getType ( ) == LOUserValue . LOType . ARRAY_SIZE ) ) { Integer sz = uv . getValue ( ) ; if ( ( sz != null ) && ( sz . intValue ( ) < 0 ) ) { return true ; } } } } return false ; } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; return true ; } }
|
returns whether an exception object is on the stack slf4j will find this and not include it in the parm list so i we find one just don t report
|
3,044
|
public void visitClassContext ( final ClassContext classContext ) { JavaClass cls = classContext . getJavaClass ( ) ; Field [ ] flds = cls . getFields ( ) ; for ( Field f : flds ) { String sig = f . getSignature ( ) ; if ( sig . startsWith ( Values . SIG_QUALIFIED_CLASS_PREFIX ) ) { if ( sig . startsWith ( "Ljava/util/" ) && sig . endsWith ( "List;" ) ) { fieldsReported . put ( f . getName ( ) , new FieldInfo ( ) ) ; } } } if ( ! fieldsReported . isEmpty ( ) ) { super . visitClassContext ( classContext ) ; reportBugs ( ) ; } }
|
overrides the visitor to accept classes that define List based fields
|
3,045
|
public void visitCode ( final Code obj ) { try { stack . resetForMethodEntry ( this ) ; super . visitCode ( obj ) ; } catch ( StopOpcodeParsingException e ) { } }
|
overrides the visitor to reset the opcode stack object
|
3,046
|
public void sawOpcode ( final int seen ) { try { stack . precomputation ( this ) ; if ( seen == Const . INVOKEINTERFACE ) { processInvokeInterface ( ) ; } else if ( seen == Const . INVOKEVIRTUAL ) { processInvokeVirtual ( ) ; } else if ( ( seen == Const . ARETURN ) && ( stack . getStackDepth ( ) > 0 ) ) { OpcodeStack . Item item = stack . getStackItem ( 0 ) ; XField field = item . getXField ( ) ; if ( field != null ) { String fieldName = field . getName ( ) ; fieldsReported . remove ( fieldName ) ; if ( fieldsReported . isEmpty ( ) ) { throw new StopOpcodeParsingException ( ) ; } } } } finally { stack . sawOpcode ( this , seen ) ; } }
|
overrides the visitor to record all method calls on List fields . If a method is not a set based method remove it from further consideration
|
3,047
|
private static XField getFieldFromStack ( final OpcodeStack stk , final String signature ) { int parmCount = SignatureUtils . getNumParameters ( signature ) ; if ( stk . getStackDepth ( ) > parmCount ) { OpcodeStack . Item itm = stk . getStackItem ( parmCount ) ; return itm . getXField ( ) ; } return null ; }
|
return the field object that the current method was called on by finding the reference down in the stack based on the number of parameters
|
3,048
|
private void reportBugs ( ) { int major = getClassContext ( ) . getJavaClass ( ) . getMajor ( ) ; for ( Map . Entry < String , FieldInfo > entry : fieldsReported . entrySet ( ) ) { String field = entry . getKey ( ) ; FieldInfo fi = entry . getValue ( ) ; int cnt = fi . getSetCount ( ) ; if ( cnt > 0 ) { FieldAnnotation fa = getFieldAnnotation ( field ) ; if ( fa != null ) { bugReporter . reportBug ( new BugInstance ( this , BugType . DLC_DUBIOUS_LIST_COLLECTION . name ( ) , ( major >= Const . MAJOR_1_4 ) ? NORMAL_PRIORITY : LOW_PRIORITY ) . addSourceLine ( fi . getSourceLineAnnotation ( ) ) ) ; } } } }
|
implements the detector by reporting all remaining fields that only have set based access
|
3,049
|
private FieldAnnotation getFieldAnnotation ( final String fieldName ) { JavaClass cls = getClassContext ( ) . getJavaClass ( ) ; Field [ ] fields = cls . getFields ( ) ; for ( Field f : fields ) { if ( f . getName ( ) . equals ( fieldName ) ) { return new FieldAnnotation ( cls . getClassName ( ) , fieldName , f . getSignature ( ) , f . isStatic ( ) ) ; } } return null ; }
|
builds a field annotation by finding the field in the classes field list
|
3,050
|
public void sawOpcode ( int seen ) { try { stack . precomputation ( this ) ; if ( seen == Const . INVOKEVIRTUAL ) { String clsName = getClassConstantOperand ( ) ; if ( "java/util/Properties" . equals ( clsName ) ) { String methodName = getNameConstantOperand ( ) ; if ( "put" . equals ( methodName ) ) { String sig = getSigConstantOperand ( ) ; if ( SignatureBuilder . SIG_TWO_OBJECTS_TO_OBJECT . equals ( sig ) && ( stack . getStackDepth ( ) >= 3 ) ) { OpcodeStack . Item valueItem = stack . getStackItem ( 0 ) ; String valueSig = valueItem . getSignature ( ) ; if ( Values . SIG_JAVA_LANG_STRING . equals ( valueSig ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY . name ( ) , LOW_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } else if ( Values . SIG_JAVA_LANG_OBJECT . equals ( valueSig ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } else { bugReporter . reportBug ( new BugInstance ( this , BugType . IPU_IMPROPER_PROPERTIES_USE . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } } } } } } finally { stack . sawOpcode ( this , seen ) ; } }
|
implements the visitor to look for calls to java . utils . Properties . put where the value is a non String . Reports both cases where if it is a string at a lower lever .
|
3,051
|
public void visitClassContext ( ClassContext classContext ) { try { if ( ( runtimeExceptionClass != null ) && ( exceptionClass != null ) ) { stack = new OpcodeStack ( ) ; declaredCheckedExceptions = new HashSet < > ( 6 ) ; JavaClass cls = classContext . getJavaClass ( ) ; classIsFinal = cls . isFinal ( ) ; classIsAnonymous = cls . isAnonymous ( ) ; super . visitClassContext ( classContext ) ; } } finally { declaredCheckedExceptions = null ; stack = null ; } }
|
overrides the visitor to create the opcode stack
|
3,052
|
public void visitCode ( Code obj ) { Method method = getMethod ( ) ; if ( method . isSynthetic ( ) ) { return ; } declaredCheckedExceptions . clear ( ) ; stack . resetForMethodEntry ( this ) ; ExceptionTable et = method . getExceptionTable ( ) ; if ( et != null ) { if ( classIsFinal || classIsAnonymous || method . isStatic ( ) || method . isPrivate ( ) || method . isFinal ( ) || ( ( Values . CONSTRUCTOR . equals ( method . getName ( ) ) && ! isAnonymousInnerCtor ( method , getThisClass ( ) ) ) ) ) { String [ ] exNames = et . getExceptionNames ( ) ; for ( String exName : exNames ) { try { JavaClass exCls = Repository . lookupClass ( exName ) ; if ( ! exCls . instanceOf ( runtimeExceptionClass ) ) { declaredCheckedExceptions . add ( exName ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } } if ( ! declaredCheckedExceptions . isEmpty ( ) ) { try { super . visitCode ( obj ) ; if ( ! declaredCheckedExceptions . isEmpty ( ) ) { BugInstance bi = new BugInstance ( this , BugType . BED_BOGUS_EXCEPTION_DECLARATION . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this , 0 ) ; for ( String ex : declaredCheckedExceptions ) { bi . addString ( ex . replaceAll ( "/" , "." ) ) ; } bugReporter . reportBug ( bi ) ; } } catch ( StopOpcodeParsingException e ) { } } } String [ ] exNames = et . getExceptionNames ( ) ; for ( int i = 0 ; i < ( exNames . length - 1 ) ; i ++ ) { try { JavaClass exCls1 = Repository . lookupClass ( exNames [ i ] ) ; for ( int j = i + 1 ; j < exNames . length ; j ++ ) { JavaClass exCls2 = Repository . lookupClass ( exNames [ j ] ) ; JavaClass childEx ; JavaClass parentEx ; if ( exCls1 . instanceOf ( exCls2 ) ) { childEx = exCls1 ; parentEx = exCls2 ; } else if ( exCls2 . instanceOf ( exCls1 ) ) { childEx = exCls2 ; parentEx = exCls1 ; } else { continue ; } if ( ! parentEx . equals ( exceptionClass ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . BED_HIERARCHICAL_EXCEPTION_DECLARATION . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addString ( childEx . getClassName ( ) + " derives from " + parentEx . getClassName ( ) ) ) ; return ; } } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } } } }
|
implements the visitor to see if the method declares that it throws any checked exceptions .
|
3,053
|
public void sawOpcode ( int seen ) { try { stack . precomputation ( this ) ; if ( OpcodeUtils . isStandardInvoke ( seen ) ) { String clsName = getClassConstantOperand ( ) ; if ( ! safeClasses . contains ( clsName ) ) { try { JavaClass cls = Repository . lookupClass ( clsName ) ; Method [ ] methods = cls . getMethods ( ) ; String methodName = getNameConstantOperand ( ) ; String signature = getSigConstantOperand ( ) ; boolean found = false ; for ( Method m : methods ) { if ( m . getName ( ) . equals ( methodName ) && m . getSignature ( ) . equals ( signature ) ) { if ( isAnonymousInnerCtor ( m , cls ) ) { break ; } ExceptionTable et = m . getExceptionTable ( ) ; if ( et != null ) { String [ ] thrownExceptions = et . getExceptionNames ( ) ; for ( String thrownException : thrownExceptions ) { removeThrownExceptionHierarchy ( thrownException ) ; } } found = true ; break ; } } if ( ! found ) { clearExceptions ( ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; clearExceptions ( ) ; } } else if ( "wait" . equals ( getNameConstantOperand ( ) ) ) { removeException ( "java.lang.InterruptedException" ) ; } } else if ( seen == Const . ATHROW ) { if ( stack . getStackDepth ( ) > 0 ) { OpcodeStack . Item item = stack . getStackItem ( 0 ) ; String exSig = item . getSignature ( ) ; String thrownException = SignatureUtils . stripSignature ( exSig ) ; removeThrownExceptionHierarchy ( thrownException ) ; } else { clearExceptions ( ) ; } } } finally { stack . sawOpcode ( this , seen ) ; } }
|
implements the visitor to look for method calls that could throw the exceptions that are listed in the declaration .
|
3,054
|
private void removeThrownExceptionHierarchy ( String thrownException ) { try { if ( Values . DOTTED_JAVA_LANG_EXCEPTION . equals ( thrownException ) || Values . DOTTED_JAVA_LANG_THROWABLE . equals ( thrownException ) ) { clearExceptions ( ) ; } else { removeException ( thrownException ) ; JavaClass exCls = Repository . lookupClass ( thrownException ) ; String clsName ; do { exCls = exCls . getSuperClass ( ) ; if ( exCls == null ) { break ; } clsName = exCls . getClassName ( ) ; removeException ( clsName ) ; } while ( ! declaredCheckedExceptions . isEmpty ( ) && ! Values . DOTTED_JAVA_LANG_EXCEPTION . equals ( clsName ) && ! Values . DOTTED_JAVA_LANG_ERROR . equals ( clsName ) ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; clearExceptions ( ) ; } }
|
removes this thrown exception the list of declared thrown exceptions including all exceptions in this exception s hierarchy . If an exception class is found that can t be loaded then just clear the list of declared checked exceptions and get out .
|
3,055
|
public void visitClassContext ( ClassContext classContext ) { try { JavaClass cls = classContext . getJavaClass ( ) ; if ( ( serializableClass != null ) && cls . implementationOf ( serializableClass ) ) { super . visitClassContext ( classContext ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } }
|
overrides the method to check for Serializable
|
3,056
|
public void visitCode ( Code obj ) { String nameAndSignature = getMethod ( ) . getName ( ) + getMethod ( ) . getSignature ( ) ; if ( SignatureBuilder . SIG_READ_OBJECT . equals ( nameAndSignature ) ) { inReadObject = true ; inWriteObject = false ; } else if ( SIG_WRITE_OBJECT . equals ( nameAndSignature ) ) { inReadObject = false ; inWriteObject = true ; } if ( inReadObject || inWriteObject ) { state = State . SEEN_NOTHING ; super . visitCode ( obj ) ; if ( state != State . SEEN_INVALID ) { bugReporter . reportBug ( new BugInstance ( this , BugType . NCS_NEEDLESS_CUSTOM_SERIALIZATION . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this , 1 ) ) ; } } }
|
overrides the method to check for either readObject or writeObject methods and if so reset the stack
|
3,057
|
public void visitClassContext ( ClassContext clsContext ) { try { stack = new OpcodeStack ( ) ; clsVersion = clsContext . getJavaClass ( ) . getMajor ( ) ; unendedZLIBs = new HashMap < > ( ) ; super . visitClassContext ( clsContext ) ; } finally { unendedZLIBs = null ; stack = null ; } }
|
implements the visitor to create and tear down the opcode stack
|
3,058
|
public void visitMethod ( Method obj ) { methodSignatureIsConstrained = false ; String methodName = obj . getName ( ) ; if ( ! Values . CONSTRUCTOR . equals ( methodName ) && ! Values . STATIC_INITIALIZER . equals ( methodName ) ) { String methodSig = obj . getSignature ( ) ; methodSignatureIsConstrained = methodIsSpecial ( methodName , methodSig ) || methodHasSyntheticTwin ( methodName , methodSig ) ; if ( ! methodSignatureIsConstrained ) { for ( AnnotationEntry entry : obj . getAnnotationEntries ( ) ) { if ( CONVERSION_ANNOTATIONS . contains ( entry . getAnnotationType ( ) ) ) { methodSignatureIsConstrained = true ; break ; } } } if ( ! methodSignatureIsConstrained ) { String parms = methodSig . split ( "\\(|\\)" ) [ 1 ] ; if ( parms . indexOf ( Values . SIG_QUALIFIED_CLASS_SUFFIX_CHAR ) >= 0 ) { outer : for ( JavaClass constrainCls : constrainingClasses ) { Method [ ] methods = constrainCls . getMethods ( ) ; for ( Method m : methods ) { if ( methodName . equals ( m . getName ( ) ) && methodSig . equals ( m . getSignature ( ) ) ) { methodSignatureIsConstrained = true ; break outer ; } } } } } } }
|
implements the visitor to look to see if this method is constrained by a superclass or interface .
|
3,059
|
public void visitCode ( final Code obj ) { try { if ( methodSignatureIsConstrained ) { return ; } if ( obj . getCode ( ) == null ) { return ; } Method m = getMethod ( ) ; if ( m . isSynthetic ( ) ) { return ; } if ( m . getName ( ) . startsWith ( "access$" ) ) { return ; } methodIsStatic = m . isStatic ( ) ; parmCount = m . getArgumentTypes ( ) . length ; if ( parmCount == 0 ) { return ; } parameterDefiners . clear ( ) ; usedParameters . clear ( ) ; stack . resetForMethodEntry ( this ) ; if ( buildParameterDefiners ( ) ) { try { super . visitCode ( obj ) ; reportBugs ( ) ; } catch ( StopOpcodeParsingException e ) { } } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } }
|
implements the visitor to collect information about the parameters of a this method
|
3,060
|
private static boolean methodIsSpecial ( String methodName , String methodSig ) { return SignatureBuilder . SIG_READ_OBJECT . equals ( methodName + methodSig ) ; }
|
determines whether the method is a baked in special method of the jdk
|
3,061
|
private boolean methodHasSyntheticTwin ( String methodName , String methodSig ) { for ( Method m : cls . getMethods ( ) ) { if ( m . isSynthetic ( ) && m . getName ( ) . equals ( methodName ) && ! m . getSignature ( ) . equals ( methodSig ) ) { return true ; } } return false ; }
|
returns whether this method has an equivalent method that is synthetic which implies this method is constrained by some Generified interface . We could compare parameters but that is a bunch of work that probably doesn t make this test any more precise so just return true if method name and synthetic is found .
|
3,062
|
private void reportBugs ( ) { Iterator < Map . Entry < Integer , Map < JavaClass , List < MethodInfo > > > > it = parameterDefiners . entrySet ( ) . iterator ( ) ; while ( it . hasNext ( ) ) { try { Map . Entry < Integer , Map < JavaClass , List < MethodInfo > > > entry = it . next ( ) ; Integer reg = entry . getKey ( ) ; if ( ! usedParameters . get ( reg . intValue ( ) ) ) { it . remove ( ) ; continue ; } Map < JavaClass , List < MethodInfo > > definers = entry . getValue ( ) ; definers . remove ( objectClass ) ; if ( definers . size ( ) > 1 ) { removeInheritedInterfaces ( definers ) ; } if ( ( definers . size ( ) == 1 ) && ! hasOverloadedMethod ( ) ) { String name = "" ; LocalVariableTable lvt = getMethod ( ) . getLocalVariableTable ( ) ; if ( lvt != null ) { LocalVariable lv = lvt . getLocalVariable ( reg . intValue ( ) , 0 ) ; if ( lv != null ) { name = lv . getName ( ) ; } } int parm = reg . intValue ( ) ; if ( ! methodIsStatic ) { parm -- ; } parm ++ ; String infName = definers . keySet ( ) . iterator ( ) . next ( ) . getClassName ( ) ; bugReporter . reportBug ( new BugInstance ( this , BugType . OCP_OVERLY_CONCRETE_PARAMETER . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this , 0 ) . addString ( getCardinality ( parm ) + " parameter '" + name + "' could be declared as " + infName + " instead" ) ) ; } } catch ( ClassNotFoundException e ) { bugReporter . reportMissingClass ( e ) ; } } }
|
implements the post processing steps to report the remaining unremoved parameter definers ie those that can be defined more abstractly .
|
3,063
|
private boolean hasOverloadedMethod ( ) { Method thisMethod = getMethod ( ) ; String name = thisMethod . getName ( ) ; String sig = thisMethod . getSignature ( ) ; int numArgs = SignatureUtils . getNumParameters ( sig ) ; for ( Method m : cls . getMethods ( ) ) { if ( m . getName ( ) . equals ( name ) ) { if ( ! m . getSignature ( ) . equals ( sig ) && ( numArgs == SignatureUtils . getNumParameters ( m . getSignature ( ) ) ) ) { return true ; } } } return false ; }
|
looks to see if this method has an overloaded method in actuality this isn t precise and returns true for methods that are named the same and have same number of args This will miss some cases but in practicality doesn t matter .
|
3,064
|
private boolean buildParameterDefiners ( ) throws ClassNotFoundException { Method m = getMethod ( ) ; Type [ ] parms = m . getArgumentTypes ( ) ; if ( parms . length == 0 ) { return false ; } ParameterAnnotationEntry [ ] annotations = m . getParameterAnnotationEntries ( ) ; boolean hasPossiblyOverlyConcreteParm = false ; for ( int i = 0 ; i < parms . length ; i ++ ) { if ( ( annotations . length <= i ) || ( annotations [ i ] == null ) || ( annotations [ i ] . getAnnotationEntries ( ) . length == 0 ) ) { String parm = parms [ i ] . getSignature ( ) ; if ( parm . startsWith ( Values . SIG_QUALIFIED_CLASS_PREFIX ) ) { String clsName = SignatureUtils . stripSignature ( parm ) ; if ( clsName . startsWith ( "java.lang." ) ) { continue ; } JavaClass clz = Repository . lookupClass ( clsName ) ; if ( ( clz . isClass ( ) && ( ! clz . isAbstract ( ) ) && ( ! cls . isEnum ( ) ) ) || OVERLY_CONCRETE_INTERFACES . contains ( clsName ) ) { Map < JavaClass , List < MethodInfo > > definers = getClassDefiners ( clz ) ; if ( ! definers . isEmpty ( ) ) { parameterDefiners . put ( Integer . valueOf ( i + ( methodIsStatic ? 0 : 1 ) ) , definers ) ; hasPossiblyOverlyConcreteParm = true ; } } } } } return hasPossiblyOverlyConcreteParm ; }
|
builds a map of method information for each method of each interface that each parameter implements of this method
|
3,065
|
private static Map < JavaClass , List < MethodInfo > > getClassDefiners ( final JavaClass cls ) throws ClassNotFoundException { Map < JavaClass , List < MethodInfo > > definers = new HashMap < > ( ) ; for ( JavaClass ci : cls . getAllInterfaces ( ) ) { if ( cls . equals ( ci ) || ! cls . isPublic ( ) || "java.lang.Comparable" . equals ( ci . getClassName ( ) ) ) { continue ; } List < MethodInfo > methodInfos = getPublicMethodInfos ( ci ) ; if ( ! methodInfos . isEmpty ( ) ) { definers . put ( ci , methodInfos ) ; } } return definers ; }
|
returns a map of method information for each public method for each interface this class implements
|
3,066
|
private static List < MethodInfo > getPublicMethodInfos ( final JavaClass cls ) { List < MethodInfo > methodInfos = new ArrayList < > ( ) ; Method [ ] methods = cls . getMethods ( ) ; for ( Method m : methods ) { if ( ( m . getAccessFlags ( ) & ( Const . ACC_PUBLIC | Const . ACC_PROTECTED ) ) != 0 ) { ExceptionTable et = m . getExceptionTable ( ) ; methodInfos . add ( new MethodInfo ( m . getName ( ) , m . getSignature ( ) , et == null ? null : et . getExceptionNames ( ) ) ) ; } } return methodInfos ; }
|
returns a list of method information of all public or protected methods in this class
|
3,067
|
private void removeUselessDefiners ( final int reg ) { Map < JavaClass , List < MethodInfo > > definers = parameterDefiners . get ( Integer . valueOf ( reg ) ) ; if ( CollectionUtils . isEmpty ( definers ) ) { return ; } String methodSig = getSigConstantOperand ( ) ; String methodName = getNameConstantOperand ( ) ; MethodInfo methodInfo = new MethodInfo ( methodName , methodSig ) ; Iterator < List < MethodInfo > > it = definers . values ( ) . iterator ( ) ; while ( it . hasNext ( ) ) { boolean methodDefined = false ; List < MethodInfo > methodSigs = it . next ( ) ; for ( MethodInfo mi : methodSigs ) { if ( methodInfo . equals ( mi ) ) { methodDefined = true ; String [ ] exceptions = mi . getMethodExceptions ( ) ; if ( exceptions != null ) { for ( String ex : exceptions ) { if ( ! isExceptionHandled ( ex ) ) { methodDefined = false ; break ; } } } break ; } } if ( ! methodDefined ) { it . remove ( ) ; } } if ( definers . isEmpty ( ) ) { parameterDefiners . remove ( Integer . valueOf ( reg ) ) ; } }
|
parses through the interface that may define a parameter defined by reg and look to see if we can rule it out because a method is called on the object that can t be satisfied by the interface if so remove that candidate interface .
|
3,068
|
private boolean isaConversionClass ( JavaClass conversionCls ) { for ( AnnotationEntry entry : conversionCls . getAnnotationEntries ( ) ) { if ( CONVERSION_ANNOTATIONS . contains ( entry . getAnnotationType ( ) ) ) { return true ; } if ( CONVERSION_SUPER_CLASSES . contains ( conversionCls . getSuperclassName ( ) ) ) { return true ; } } return false ; }
|
returns whether this class is used to convert types of some sort such that you don t want to suggest reducing the class specified to be more generic
|
3,069
|
public void visitClassContext ( ClassContext classContext ) { try { stack = new OpcodeStack ( ) ; possibleForLoops = new HashSet < > ( ) ; super . visitClassContext ( classContext ) ; } finally { stack = null ; possibleForLoops = null ; } }
|
overrides the interface to create and clear the stack and loops tracker
|
3,070
|
private boolean prescreen ( Method method ) { BitSet bytecodeSet = getClassContext ( ) . getBytecodeSet ( method ) ; return ( bytecodeSet != null ) && ( bytecodeSet . get ( Const . IINC ) ) && ( bytecodeSet . get ( Const . GOTO ) || bytecodeSet . get ( Const . GOTO_W ) ) ; }
|
looks for methods that contain a IINC and GOTO or GOTO_W opcodes
|
3,071
|
public void visitCode ( final Code obj ) { Method m = getMethod ( ) ; if ( prescreen ( m ) ) { sawListSize = false ; stack . resetForMethodEntry ( this ) ; state = State . SAW_NOTHING ; stage = Stage . FIND_LOOP_STAGE ; super . visitCode ( obj ) ; if ( sawListSize && ! possibleForLoops . isEmpty ( ) ) { stack . resetForMethodEntry ( this ) ; state = State . SAW_NOTHING ; stage = Stage . FIND_BUG_STAGE ; super . visitCode ( obj ) ; } } }
|
overrides the visitor to reset the opcode stack
|
3,072
|
public void sawOpcode ( final int seen ) { if ( stage == Stage . FIND_LOOP_STAGE ) { sawOpcodeLoop ( seen ) ; } else { sawOpcodeBug ( seen ) ; } }
|
overrides the visitor to find list indexed iterating
|
3,073
|
private void sawOpcodeLoop ( final int seen ) { try { stack . mergeJumps ( this ) ; switch ( state ) { case SAW_NOTHING : if ( ( seen == Const . IINC ) && ( getIntConstant ( ) == 1 ) ) { loopReg = getRegisterOperand ( ) ; state = State . SAW_IINC ; } break ; case SAW_IINC : if ( ( seen == Const . GOTO ) || ( seen == Const . GOTO_W ) ) { int branchTarget = getBranchTarget ( ) ; int pc = getPC ( ) ; if ( branchTarget < pc ) { possibleForLoops . add ( new ForLoop ( branchTarget , pc , loopReg ) ) ; } } state = State . SAW_NOTHING ; break ; } if ( ( seen == Const . INVOKEINTERFACE ) && Values . SLASHED_JAVA_UTIL_LIST . equals ( getClassConstantOperand ( ) ) && "size" . equals ( getNameConstantOperand ( ) ) && SignatureBuilder . SIG_VOID_TO_INT . equals ( getSigConstantOperand ( ) ) ) { sawListSize = true ; } } finally { stack . sawOpcode ( this , seen ) ; } }
|
the first pass of the method opcode to collet for loops information
|
3,074
|
public void visitClassContext ( ClassContext classContext ) { try { JavaClass cls = classContext . getJavaClass ( ) ; JavaClass [ ] superClasses = cls . getSuperClasses ( ) ; for ( JavaClass superCls : superClasses ) { if ( tagClasses . contains ( superCls . getClassName ( ) ) ) { attributes = getAttributes ( cls ) ; if ( ! attributes . isEmpty ( ) ) { methodWrites = new HashMap < > ( ) ; fieldAnnotations = new HashMap < > ( ) ; super . visitClassContext ( classContext ) ; reportBugs ( ) ; } break ; } } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } finally { attributes = null ; methodWrites = null ; fieldAnnotations = null ; } }
|
implements the visitor to look for classes that extend the TagSupport or BodyTagSupport class
|
3,075
|
private static Map < QMethod , String > getAttributes ( JavaClass cls ) { Map < QMethod , String > atts = new HashMap < > ( ) ; Method [ ] methods = cls . getMethods ( ) ; for ( Method m : methods ) { String name = m . getName ( ) ; if ( name . startsWith ( "set" ) && m . isPublic ( ) && ! m . isStatic ( ) ) { String sig = m . getSignature ( ) ; List < String > args = SignatureUtils . getParameterSignatures ( sig ) ; if ( ( args . size ( ) == 1 ) && Values . SIG_VOID . equals ( SignatureUtils . getReturnSignature ( sig ) ) ) { String parmSig = args . get ( 0 ) ; if ( validAttrTypes . contains ( parmSig ) ) { Code code = m . getCode ( ) ; if ( ( code != null ) && ( code . getCode ( ) . length < MAX_ATTRIBUTE_CODE_LENGTH ) ) { atts . put ( new QMethod ( name , sig ) , parmSig ) ; } } } } } return atts ; }
|
collect all possible attributes given the name of methods available .
|
3,076
|
public void visitCode ( Code obj ) { Method m = getMethod ( ) ; if ( ! Values . CONSTRUCTOR . equals ( m . getName ( ) ) ) { super . visitCode ( obj ) ; } }
|
implements the visitor to
|
3,077
|
public void sawOpcode ( int seen ) { if ( seen == Const . PUTFIELD ) { QMethod methodInfo = new QMethod ( getMethodName ( ) , getMethodSig ( ) ) ; Map < Map . Entry < String , String > , SourceLineAnnotation > fields = methodWrites . get ( methodInfo ) ; if ( fields == null ) { fields = new HashMap < > ( ) ; methodWrites . put ( methodInfo , fields ) ; } String fieldName = getNameConstantOperand ( ) ; String fieldSig = getSigConstantOperand ( ) ; FieldAnnotation fa = new FieldAnnotation ( getDottedClassName ( ) , fieldName , fieldSig , false ) ; fieldAnnotations . put ( fieldName , fa ) ; fields . put ( new AbstractMap . SimpleImmutableEntry ( fieldName , fieldSig ) , SourceLineAnnotation . fromVisitedInstruction ( this ) ) ; } }
|
implements the visitor to record storing of fields and where they occur
|
3,078
|
private void reportBugs ( ) { for ( Map . Entry < QMethod , String > attEntry : attributes . entrySet ( ) ) { QMethod methodInfo = attEntry . getKey ( ) ; String attType = attEntry . getValue ( ) ; Map < Map . Entry < String , String > , SourceLineAnnotation > fields = methodWrites . get ( methodInfo ) ; if ( ( fields == null ) || ( fields . size ( ) != 1 ) ) { continue ; } Map . Entry < String , String > fieldInfo = fields . keySet ( ) . iterator ( ) . next ( ) ; String fieldType = fieldInfo . getValue ( ) ; if ( ! attType . equals ( fieldType ) ) { continue ; } String fieldName = fieldInfo . getKey ( ) ; for ( Map . Entry < QMethod , Map < Map . Entry < String , String > , SourceLineAnnotation > > fwEntry : methodWrites . entrySet ( ) ) { if ( fwEntry . getKey ( ) . equals ( methodInfo ) ) { continue ; } SourceLineAnnotation sla = fwEntry . getValue ( ) . get ( fieldInfo ) ; if ( sla != null ) { bugReporter . reportBug ( new BugInstance ( this , BugType . NRTL_NON_RECYCLEABLE_TAG_LIB . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addField ( fieldAnnotations . get ( fieldName ) ) . addSourceLine ( sla ) ) ; break ; } } } }
|
generates all the bug reports for attributes that are not recycleable
|
3,079
|
public void visitClassContext ( ClassContext classContext ) { try { JavaClass cls = classContext . getJavaClass ( ) ; if ( cls . isAbstract ( ) || cls . isInterface ( ) ) { return ; } if ( Values . DOTTED_JAVA_LANG_OBJECT . equals ( cls . getSuperclassName ( ) ) ) { return ; } JavaClass [ ] infs = cls . getInterfaces ( ) ; if ( infs . length > 0 ) { Set < QMethod > clsMethods = buildMethodSet ( cls ) ; for ( JavaClass inf : infs ) { Set < QMethod > infMethods = buildMethodSet ( inf ) ; if ( ! infMethods . isEmpty ( ) ) { infMethods . removeAll ( clsMethods ) ; if ( ! infMethods . isEmpty ( ) ) { JavaClass superCls = cls . getSuperClass ( ) ; filterSuperInterfaceMethods ( inf , infMethods , superCls ) ; if ( ! infMethods . isEmpty ( ) && ! superCls . implementationOf ( inf ) ) { int priority = AnalysisContext . currentAnalysisContext ( ) . isApplicationClass ( superCls ) ? NORMAL_PRIORITY : LOW_PRIORITY ; BugInstance bi = new BugInstance ( this , BugType . SCII_SPOILED_CHILD_INTERFACE_IMPLEMENTOR . name ( ) , priority ) . addClass ( cls ) . addString ( "Implementing interface: " + inf . getClassName ( ) ) . addString ( "Methods:" ) ; for ( QMethod methodInfo : infMethods ) { bi . addString ( '\t' + methodInfo . toString ( ) ) ; } bugReporter . reportBug ( bi ) ; return ; } } } } } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } }
|
looks for classes that implement interfaces but don t provide those methods
|
3,080
|
private void filterSuperInterfaceMethods ( JavaClass inf , Set < QMethod > infMethods , JavaClass cls ) { try { if ( infMethods . isEmpty ( ) ) { return ; } JavaClass [ ] superInfs = inf . getInterfaces ( ) ; for ( JavaClass superInf : superInfs ) { if ( cls . implementationOf ( superInf ) ) { Set < QMethod > superInfMethods = buildMethodSet ( superInf ) ; infMethods . removeAll ( superInfMethods ) ; if ( infMethods . isEmpty ( ) ) { return ; } } filterSuperInterfaceMethods ( superInf , infMethods , cls ) ; } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; infMethods . clear ( ) ; } }
|
removes methods found in an interface when a super interface having the same methods is implemented in a parent . While this is somewhat hinky we ll allow it .
|
3,081
|
public void visitCode ( Code obj ) { try { userValues = new HashMap < > ( ) ; loops = new HashMap < > ( ) ; isInstanceMethod = ! getMethod ( ) . isStatic ( ) ; super . visitCode ( obj ) ; } finally { userValues = null ; loops = null ; } }
|
implements the visitor to reset the userValues and loops
|
3,082
|
private String isFieldCollection ( OpcodeStack . Item item ) throws ClassNotFoundException { Comparable < ? > aliasReg = ( Comparable < ? > ) item . getUserValue ( ) ; if ( aliasReg instanceof String ) { return ( String ) aliasReg ; } XField field = item . getXField ( ) ; if ( field == null ) { return null ; } JavaClass cls = item . getJavaClass ( ) ; if ( ( cls != null ) && cls . implementationOf ( collectionClass ) ) { return field . getName ( ) ; } return null ; }
|
determines if the stack item refers to a collection that is stored in a field
|
3,083
|
public String getDisplay ( HashSet < String > s , String a , String b ) { if ( s . contains ( a ) ) { s . add ( b ) ; } else { s . add ( a + b ) ; } StringBuilder sb = new StringBuilder ( ) ; Iterator < String > it = s . iterator ( ) ; while ( it . hasNext ( ) ) { sb . append ( it . next ( ) ) ; } return sb . toString ( ) ; }
|
tag OCP hashset could be Set instead
|
3,084
|
public String max ( List < String > s ) { String max = "" ; for ( String p : s ) { if ( p . length ( ) > max . length ( ) ) { max = p ; } } return max ; }
|
List should be declared as Collection where possible
|
3,085
|
public void parse ( DefaultHandler dh , File f ) throws SAXException , ParserConfigurationException , IOException { SAXParserFactory spf = SAXParserFactory . newInstance ( ) ; SAXParser sp = spf . newSAXParser ( ) ; XMLReader xr = sp . getXMLReader ( ) ; xr . setContentHandler ( dh ) ; xr . parse ( new InputSource ( new FileInputStream ( f ) ) ) ; }
|
tag OCP dh could be a ContentHandler instead
|
3,086
|
public static void httpComponentWithTryFalseNegative ( HttpPut request , String auth ) { auth = auth + "password" ; try { request . addHeader ( "Authorization" , Base64 . encodeBase64String ( auth . getBytes ( "UTF-8" ) ) ) ; } catch ( UnsupportedEncodingException e ) { logger . fatal ( "There was a problem encoding " + auth , e ) ; } }
|
should tag OCP request - > HTTPMessage but doesnt
|
3,087
|
public int compare ( GregorianCalendar c1 , GregorianCalendar c2 ) { if ( c2 . getGregorianChange ( ) == null ) { return ( int ) ( c1 . getTime ( ) . getTime ( ) - c2 . getTime ( ) . getTime ( ) ) ; } else { return 0 ; } }
|
this method is really suspect but will ignore this case
|
3,088
|
public void visitClassContext ( final ClassContext context ) { try { stack = new OpcodeStack ( ) ; super . visitClassContext ( context ) ; } finally { stack = null ; } }
|
overrides the visitor to create and clear the opcode stack
|
3,089
|
private boolean prescreen ( Method obj ) { BitSet bytecodeSet = getClassContext ( ) . getBytecodeSet ( obj ) ; return ( bytecodeSet != null ) && ( ( bytecodeSet . get ( Const . LDC ) || ( bytecodeSet . get ( Const . LDC_W ) ) ) ) ; }
|
looks for methods that contain a LDC opcode
|
3,090
|
public void visitCode ( Code obj ) { if ( prescreen ( getMethod ( ) ) ) { stack . resetForMethodEntry ( this ) ; super . visitCode ( obj ) ; } }
|
prescreens the method and reset the stack
|
3,091
|
public void sawOpcode ( int seen ) { try { stack . precomputation ( this ) ; if ( ( seen == Const . INVOKEVIRTUAL ) || ( seen == Const . INVOKEINTERFACE ) ) { FQMethod key = new FQMethod ( getClassConstantOperand ( ) , getNameConstantOperand ( ) , getSigConstantOperand ( ) ) ; Object posObject = characterMethods . get ( key ) ; if ( posObject instanceof Integer ) { if ( checkSingleParamMethod ( ( ( Integer ) posObject ) . intValue ( ) ) && ! isInlineAppend ( key ) ) { reportBug ( ) ; } } else if ( ( posObject instanceof IntPair ) && checkDoubleParamMethod ( ( IntPair ) posObject ) ) { reportBug ( ) ; } } else if ( seen == Const . DUP ) { if ( stack . getStackDepth ( ) > 0 ) { OpcodeStack . Item itm = stack . getStackItem ( 0 ) ; String duppedSig = itm . getSignature ( ) ; if ( Values . SIG_JAVA_UTIL_STRINGBUILDER . equals ( duppedSig ) || Values . SIG_JAVA_UTIL_STRINGBUFFER . equals ( duppedSig ) ) { itm . setUserValue ( UCPMUserValue . INLINE ) ; } } } else if ( ( seen == Const . PUTFIELD ) || ( ( ( seen == Const . PUTSTATIC ) || OpcodeUtils . isAStore ( seen ) ) && ( stack . getStackDepth ( ) > 0 ) ) ) { OpcodeStack . Item itm = stack . getStackItem ( 0 ) ; itm . setUserValue ( null ) ; } } finally { UCPMUserValue uv = callHasInline ( seen ) ; stack . sawOpcode ( this , seen ) ; if ( ( uv == UCPMUserValue . INLINE ) && ( stack . getStackDepth ( ) > 0 ) ) { OpcodeStack . Item itm = stack . getStackItem ( 0 ) ; itm . setUserValue ( uv ) ; } } }
|
implements the visitor to look for method calls that pass a constant string as a parameter when the string is only one character long and there is an alternate method passing a character .
|
3,092
|
private UCPMUserValue callHasInline ( int seen ) { if ( seen != Const . INVOKEVIRTUAL ) { return null ; } String sig = getSigConstantOperand ( ) ; String returnSig = SignatureUtils . getReturnSignature ( sig ) ; if ( Values . SIG_JAVA_UTIL_STRINGBUILDER . equals ( returnSig ) || Values . SIG_JAVA_UTIL_STRINGBUFFER . equals ( returnSig ) ) { int parmCount = SignatureUtils . getNumParameters ( sig ) ; if ( stack . getStackDepth ( ) > parmCount ) { OpcodeStack . Item itm = stack . getStackItem ( parmCount ) ; return ( UCPMUserValue ) itm . getUserValue ( ) ; } } return null ; }
|
checks to see if the current opcode is an INVOKEVIRTUAL call that has a INLINE userValue on the caller and a return value . If so return it .
|
3,093
|
public void visitClassContext ( ClassContext classContext ) { try { refClasses = new HashSet < > ( ) ; refClasses . add ( classContext . getJavaClass ( ) . getClassName ( ) ) ; state = State . COLLECT ; super . visitClassContext ( classContext ) ; state = State . SEEN_NOTHING ; super . visitClassContext ( classContext ) ; } finally { refClasses = null ; } }
|
overrides the visitor to collect all class references
|
3,094
|
public void sawOpcode ( int seen ) { switch ( state ) { case COLLECT : if ( ( seen == Const . INVOKESTATIC ) || ( seen == Const . INVOKEVIRTUAL ) || ( seen == Const . INVOKEINTERFACE ) || ( seen == Const . INVOKESPECIAL ) ) { refClasses . add ( getClassConstantOperand ( ) ) ; String signature = getSigConstantOperand ( ) ; Type [ ] argTypes = Type . getArgumentTypes ( signature ) ; for ( Type t : argTypes ) { addType ( t ) ; } Type resultType = Type . getReturnType ( signature ) ; addType ( resultType ) ; } break ; case SEEN_NOTHING : if ( ( seen == Const . LDC ) || ( seen == Const . LDC_W ) ) { Constant c = getConstantRefOperand ( ) ; if ( c instanceof ConstantString ) { clsName = ( ( ConstantString ) c ) . getBytes ( getConstantPool ( ) ) ; state = State . SEEN_LDC ; } } break ; case SEEN_LDC : if ( ( seen == Const . INVOKESTATIC ) && "forName" . equals ( getNameConstantOperand ( ) ) && "java/lang/Class" . equals ( getClassConstantOperand ( ) ) && refClasses . contains ( clsName ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . SCR_SLOPPY_CLASS_REFLECTION . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } state = State . SEEN_NOTHING ; break ; } }
|
overrides the visitor to find class loading that is non obfuscation proof
|
3,095
|
private void addType ( Type t ) { String signature = t . getSignature ( ) ; if ( signature . startsWith ( Values . SIG_QUALIFIED_CLASS_PREFIX ) ) { refClasses . add ( SignatureUtils . trimSignature ( signature ) ) ; } }
|
add the type string represented by the type to the refClasses set if it is a reference
|
3,096
|
public void sawOpcode ( int seen ) { try { stack . precomputation ( this ) ; if ( seen == Const . INVOKEVIRTUAL ) { String clsName = getClassConstantOperand ( ) ; if ( "java/io/ObjectOutputStream" . equals ( clsName ) ) { String name = getNameConstantOperand ( ) ; if ( "writeObject" . equals ( name ) && ( stack . getStackDepth ( ) > 0 ) ) { OpcodeStack . Item item = stack . getStackItem ( 0 ) ; JavaClass cls = item . getJavaClass ( ) ; if ( ( cls != null ) && ( cls . getClassName ( ) . indexOf ( Values . INNER_CLASS_SEPARATOR ) >= 0 ) && hasOuterClassSyntheticReference ( cls ) ) { bugReporter . reportBug ( new BugInstance ( this , BugType . PUS_POSSIBLE_UNSUSPECTED_SERIALIZATION . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) . addSourceLine ( this ) ) ; } } } } } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } finally { stack . sawOpcode ( this , seen ) ; } }
|
implements the visitor to look for serialization of an object that is an non - static inner class .
|
3,097
|
public void visitClassContext ( ClassContext classContext ) { try { JavaClass cls = classContext . getJavaClass ( ) ; if ( cls . getMajor ( ) >= Const . MAJOR_1_4 ) { stack = new OpcodeStack ( ) ; regValueType = new HashMap < Integer , State > ( ) ; super . visitClassContext ( classContext ) ; } } finally { stack = null ; regValueType = null ; } }
|
implements the visitor to make sure the class is at least java 1 . 4 and to reset the opcode stack
|
3,098
|
public void visitClassContext ( ClassContext classContext ) { try { clsContext = classContext ; JavaClass cls = classContext . getJavaClass ( ) ; if ( cls . isInterface ( ) ) return ; superClasses = cls . getSuperClasses ( ) ; cls . accept ( this ) ; } catch ( ClassNotFoundException cnfe ) { bugReporter . reportMissingClass ( cnfe ) ; } finally { clsContext = null ; superClasses = null ; } }
|
implements the detector to collect the super classes
|
3,099
|
public void visitMethod ( Method obj ) { if ( ! obj . isAbstract ( ) ) return ; String methodName = obj . getName ( ) ; String methodSig = obj . getSignature ( ) ; outer : for ( JavaClass cls : superClasses ) { Method [ ] methods = cls . getMethods ( ) ; for ( Method m : methods ) { if ( m . isPrivate ( ) || m . isAbstract ( ) ) continue ; if ( methodName . equals ( m . getName ( ) ) && methodSig . equals ( m . getSignature ( ) ) ) { BugInstance bug = new BugInstance ( this , BugType . AOM_ABSTRACT_OVERRIDDEN_METHOD . name ( ) , NORMAL_PRIORITY ) . addClass ( this ) . addMethod ( this ) ; Code code = obj . getCode ( ) ; if ( code != null ) bug . addSourceLineRange ( clsContext , this , 0 , code . getLength ( ) - 1 ) ; bugReporter . reportBug ( bug ) ; break outer ; } } } }
|
overrides the visitor to find abstract methods that override concrete ones
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.