├── .gitattributes ├── .gitignore ├── .mailmap ├── .travis.yml ├── LICENSE ├── README.md ├── findbugs.txt ├── huntbugs-ant-plugin ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ └── main │ ├── java │ └── one │ │ └── util │ │ └── huntbugs │ │ └── ant │ │ └── HuntBugsTask.java │ └── resources │ └── one │ └── util │ └── huntbugs │ └── ant │ └── antlib.xml ├── huntbugs-maven-plugin ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.jdt.ui.prefs │ ├── org.eclipse.m2e.core.prefs │ └── org.eclipse.pde.core.prefs ├── pom.xml └── src │ └── main │ └── java │ └── one │ └── util │ └── huntbugs │ └── maven │ └── plugin │ └── HuntBugsMojo.java ├── huntbugs ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.jdt.ui.prefs │ ├── org.eclipse.m2e.core.prefs │ └── org.eclipse.pde.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── one │ │ │ └── util │ │ │ └── huntbugs │ │ │ ├── HuntBugs.java │ │ │ ├── analysis │ │ │ ├── AnalysisListener.java │ │ │ ├── AnalysisOptions.java │ │ │ ├── Context.java │ │ │ ├── ErrorMessage.java │ │ │ └── HuntBugsResult.java │ │ │ ├── assertions │ │ │ ├── AssertionData.java │ │ │ └── MemberAsserter.java │ │ │ ├── db │ │ │ ├── DeclaredAnnotations.java │ │ │ ├── FieldStats.java │ │ │ ├── Hierarchy.java │ │ │ ├── MethodStats.java │ │ │ └── Mutability.java │ │ │ ├── detect │ │ │ ├── AbandonedStream.java │ │ │ ├── AppendObjectOutputStream.java │ │ │ ├── ArrayRangeCheck.java │ │ │ ├── AtomicConcurrent.java │ │ │ ├── AverageComputation.java │ │ │ ├── BadMath.java │ │ │ ├── BadMethodCalls.java │ │ │ ├── BadMethodReferences.java │ │ │ ├── BadMonitorObject.java │ │ │ ├── CheckReturnValue.java │ │ │ ├── CloneContract.java │ │ │ ├── CompareContract.java │ │ │ ├── CompareUsage.java │ │ │ ├── ConditionChain.java │ │ │ ├── CovariantArrays.java │ │ │ ├── DeadLocalStore.java │ │ │ ├── DroppedExceptionObject.java │ │ │ ├── DubiousCatch.java │ │ │ ├── DuplicateAssignment.java │ │ │ ├── EasyMockProblems.java │ │ │ ├── EmptySync.java │ │ │ ├── EqualsContract.java │ │ │ ├── ExceptionalExpression.java │ │ │ ├── ExclusiveConditions.java │ │ │ ├── ExposeRepresentation.java │ │ │ ├── FieldAccess.java │ │ │ ├── FinalizerContract.java │ │ │ ├── FloatingPointComparison.java │ │ │ ├── FloatingPointNaN.java │ │ │ ├── IgnoredException.java │ │ │ ├── IncorrectVarArg.java │ │ │ ├── InfiniteLoop.java │ │ │ ├── InfiniteRecursion.java │ │ │ ├── InitializerRefersSubclass.java │ │ │ ├── Internationalization.java │ │ │ ├── InvalidMinMax.java │ │ │ ├── IteratorContract.java │ │ │ ├── JcipProblems.java │ │ │ ├── KnownComparison.java │ │ │ ├── LockProblems.java │ │ │ ├── MinValueHandling.java │ │ │ ├── MutableServletField.java │ │ │ ├── Naming.java │ │ │ ├── NegativeRemainder.java │ │ │ ├── NewGetClass.java │ │ │ ├── NoRuntimeRetention.java │ │ │ ├── NonShortCircuit.java │ │ │ ├── NullCheck.java │ │ │ ├── NumberConstructor.java │ │ │ ├── NumericComparison.java │ │ │ ├── NumericPromotion.java │ │ │ ├── RandomUsage.java │ │ │ ├── RedundantInterfaces.java │ │ │ ├── RedundantStreamCalls.java │ │ │ ├── RegexProblems.java │ │ │ ├── ReturnNull.java │ │ │ ├── RoughConstant.java │ │ │ ├── SameBranches.java │ │ │ ├── SameIfChain.java │ │ │ ├── SelfAssignment.java │ │ │ ├── SelfComputation.java │ │ │ ├── SerializationIdiom.java │ │ │ ├── SpinLoop.java │ │ │ ├── SqlBadArgument.java │ │ │ ├── StartInConstructor.java │ │ │ ├── StaticFieldFromInstanceMethod.java │ │ │ ├── StaticFieldNonThreadSafe.java │ │ │ ├── StringConcatInLoop.java │ │ │ ├── StringIndex.java │ │ │ ├── StringUsage.java │ │ │ ├── SwingProblems.java │ │ │ ├── SyncGetClass.java │ │ │ ├── SyncOnUpdatedField.java │ │ │ ├── ToArrayDowncast.java │ │ │ ├── UncalledPrivateMethod.java │ │ │ ├── UnnecessaryBoxing.java │ │ │ ├── UnnecessaryInstanceOf.java │ │ │ ├── UnreachableCatch.java │ │ │ ├── UnsafeGetResource.java │ │ │ ├── UnsupportedCall.java │ │ │ ├── UnusedParameter.java │ │ │ ├── UselessVoidMethod.java │ │ │ ├── VolatileArray.java │ │ │ ├── VolatileIncrement.java │ │ │ ├── WaitContract.java │ │ │ └── WrongMapIterator.java │ │ │ ├── filter │ │ │ └── AnnotationFilters.java │ │ │ ├── flow │ │ │ ├── Annotator.java │ │ │ ├── Annotators.java │ │ │ ├── BackLinkAnnotator.java │ │ │ ├── CFG.java │ │ │ ├── ClassFields.java │ │ │ ├── CodeBlock.java │ │ │ ├── ConstAnnotator.java │ │ │ ├── Dataflow.java │ │ │ ├── ETypeAnnotator.java │ │ │ ├── EnumConstant.java │ │ │ ├── Exceptional.java │ │ │ ├── FrameContext.java │ │ │ ├── GraphSearch.java │ │ │ ├── Inf.java │ │ │ ├── NullAnnotator.java │ │ │ ├── Nullness.java │ │ │ ├── PurityAnnotator.java │ │ │ ├── SourceAnnotator.java │ │ │ ├── TrueFalse.java │ │ │ ├── ValuesFlow.java │ │ │ └── etype │ │ │ │ ├── AndType.java │ │ │ │ ├── ComplexType.java │ │ │ │ ├── EType.java │ │ │ │ ├── OrType.java │ │ │ │ └── SingleType.java │ │ │ ├── input │ │ │ └── XmlReportReader.java │ │ │ ├── output │ │ │ ├── CombinedReportWriter.java │ │ │ ├── HtmlReportWriter.java │ │ │ ├── ReportWriter.java │ │ │ ├── Reports.java │ │ │ └── XmlReportWriter.java │ │ │ ├── registry │ │ │ ├── AbstractTypeDatabase.java │ │ │ ├── ClassContext.java │ │ │ ├── ClassData.java │ │ │ ├── DatabaseRegistry.java │ │ │ ├── Detector.java │ │ │ ├── DetectorRegistry.java │ │ │ ├── ElementContext.java │ │ │ ├── FieldContext.java │ │ │ ├── FieldData.java │ │ │ ├── MethodContext.java │ │ │ ├── MethodData.java │ │ │ └── anno │ │ │ │ ├── AssertNoWarning.java │ │ │ │ ├── AssertWarning.java │ │ │ │ ├── AstNodes.java │ │ │ │ ├── AstVisitor.java │ │ │ │ ├── ClassVisitor.java │ │ │ │ ├── FieldVisitor.java │ │ │ │ ├── MethodVisitor.java │ │ │ │ ├── TypeDatabase.java │ │ │ │ ├── TypeDatabaseItem.java │ │ │ │ ├── VisitOrder.java │ │ │ │ ├── WarningDefinition.java │ │ │ │ └── WarningDefinitions.java │ │ │ ├── repo │ │ │ ├── AuxRepository.java │ │ │ ├── CompositeRepository.java │ │ │ ├── DirRepository.java │ │ │ ├── FilteredRepository.java │ │ │ ├── JarRepository.java │ │ │ ├── Repository.java │ │ │ └── RepositoryVisitor.java │ │ │ ├── spi │ │ │ ├── DataTests.java │ │ │ └── HuntBugsPlugin.java │ │ │ ├── util │ │ │ ├── AccessLevel.java │ │ │ ├── Annotations.java │ │ │ ├── Equi.java │ │ │ ├── ExpressionFormatter.java │ │ │ ├── Exprs.java │ │ │ ├── Iterables.java │ │ │ ├── Maps.java │ │ │ ├── Methods.java │ │ │ ├── NodeChain.java │ │ │ ├── Nodes.java │ │ │ ├── Types.java │ │ │ ├── Variables.java │ │ │ ├── Xml.java │ │ │ └── YesNoMaybe.java │ │ │ └── warning │ │ │ ├── Formatter.java │ │ │ ├── Messages.java │ │ │ ├── Role.java │ │ │ ├── Roles.java │ │ │ ├── Warning.java │ │ │ ├── WarningAnnotation.java │ │ │ ├── WarningStatus.java │ │ │ ├── WarningType.java │ │ │ └── rule │ │ │ ├── CategoryRule.java │ │ │ ├── CompositeRule.java │ │ │ ├── RegexRule.java │ │ │ └── Rule.java │ └── resources │ │ └── huntbugs │ │ ├── messages.xml │ │ ├── messages.xsd │ │ ├── report.xsd │ │ └── report.xsl │ └── test │ └── java │ └── one │ └── util │ └── huntbugs │ ├── AsserterTest.java │ ├── DataTest.java │ ├── FilteredRepositoryTest.java │ ├── MessagesTest.java │ ├── OptionsTest.java │ ├── ReportsTest.java │ ├── RuleTest.java │ ├── asserter │ └── TestAsserter.java │ ├── registry │ └── DetectorRegistryTest.java │ ├── testdata │ ├── TestAbandonedStream.java │ ├── TestAppendObjectOutputStream.java │ ├── TestArrayRangeCheck.java │ ├── TestAtomicConcurrent.java │ ├── TestAverageComputation.java │ ├── TestBadMath.java │ ├── TestBadMethodCalls.java │ ├── TestBadMethodReferences.java │ ├── TestBadMonitorObject.java │ ├── TestCheckReturnValue.java │ ├── TestCloneContract.java │ ├── TestCompareContract.java │ ├── TestCompareUsage.java │ ├── TestConditionChain.java │ ├── TestCovariantArrays.java │ ├── TestDeadLocalStore.java │ ├── TestDroppedExceptionObject.java │ ├── TestDubiousCatch.java │ ├── TestDuplicateAssignment.java │ ├── TestEasyMockProblems.java │ ├── TestEmptySync.java │ ├── TestEqualsContract.java │ ├── TestExceptionalExpression.java │ ├── TestExclusiveConditions.java │ ├── TestExposeRepresentation.java │ ├── TestFieldAccess.java │ ├── TestFinalizer.java │ ├── TestFloatComparison.java │ ├── TestFloatNaN.java │ ├── TestIgnoredException.java │ ├── TestIncorrectVarArg.java │ ├── TestInfiniteLoop.java │ ├── TestInfiniteRecursion.java │ ├── TestInitializerRefersSubclass.java │ ├── TestInternationalization.java │ ├── TestInvalidMinMax.java │ ├── TestIteratorContract.java │ ├── TestJcipProblems.java │ ├── TestKnownComparison.java │ ├── TestLockProblems.java │ ├── TestMinValueHandling.java │ ├── TestMutableServletField.java │ ├── TestNaming.java │ ├── TestNegativeRemainder.java │ ├── TestNewGetClass.java │ ├── TestNoRuntimeRetention.java │ ├── TestNonShortCircuit.java │ ├── TestNullCheck.java │ ├── TestNumberConstructor.java │ ├── TestNumericComparison.java │ ├── TestNumericPromotion.java │ ├── TestRandomUsage.java │ ├── TestRedundantInterfaces.java │ ├── TestRedundantStreamCalls.java │ ├── TestRegexProblems.java │ ├── TestReturnNull.java │ ├── TestRoughConstants.java │ ├── TestSameBranches.java │ ├── TestSameIfChain.java │ ├── TestSelfAssignment.java │ ├── TestSelfComputation.java │ ├── TestSerializationIdiom.java │ ├── TestSpinLoop.java │ ├── TestSqlBadArgument.java │ ├── TestStartInConstructor.java │ ├── TestStaticFieldFromInstanceMethod.java │ ├── TestStaticFieldNonThreadSafe.java │ ├── TestStringConcatInLoop.java │ ├── TestStringIndex.java │ ├── TestStringUsage.java │ ├── TestSwingProblems.java │ ├── TestSyncGetClass.java │ ├── TestSyncOnUpdatedField.java │ ├── TestToArrayDowncast.java │ ├── TestUncalledPrivateMethod.java │ ├── TestUnnecessaryBoxing.java │ ├── TestUnnecessaryInstanceOf.java │ ├── TestUnreachableCatch.java │ ├── TestUnsafeGetResource.java │ ├── TestUnsupportedCall.java │ ├── TestUnusedParameter.java │ ├── TestUselessVoidMethod.java │ ├── TestValuesFlow.java │ ├── TestVolatileArray.java │ ├── TestVolatileIncrement.java │ ├── TestWaitContract.java │ ├── TestWrongMapIterator.java │ └── sub │ │ └── SubFieldAccess.java │ └── util │ └── TestIterables.java ├── pom.xml ├── sample-huntbugs-custom-detector ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── one │ │ │ └── util │ │ │ └── huntbugs │ │ │ └── sample │ │ │ ├── SampleHuntBugsPlugin.java │ │ │ └── detect │ │ │ └── SampleCustomDetector.java │ └── resources │ │ ├── META-INF │ │ └── services │ │ │ └── one.util.huntbugs.spi.HuntBugsPlugin │ │ └── huntbugs │ │ └── messages.xml │ └── test │ └── java │ └── one │ └── util │ └── huntbugs │ └── sample │ ├── DataTest.java │ └── testdata │ └── TestSampleCustomDetector.java └── sample-project ├── .gitignore ├── pom.xml └── src ├── main └── java │ └── one │ └── util │ └── huntbugs │ └── sample │ └── MyProductionCode.java └── test └── java └── one └── util └── huntbugs └── sample └── MyProductionCodeTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.java text 3 | *.md text 4 | *.xml text 5 | *.xsd text 6 | *.xsl text 7 | *.bat text eol=crlf 8 | *.sh text eol=lf 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /huntbugstest 2 | /target 3 | 4 | # Eclipse workspace 5 | .metadata 6 | 7 | # Merge files 8 | *.orig 9 | 10 | # IntelliJ IDEA 11 | .idea 12 | 13 | *.releaseBackup -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Tagir Valeev -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | install: 3 | - mvn install -DskipTests=true -B -V 4 | jdk: 5 | - oraclejdk8 6 | after_success: 7 | - cd huntbugs && mvn jacoco:report coveralls:report -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | huntbugs-ant-plugin 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.8 6 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | 5 | one.util 6 | huntbugs-all 7 | 0.0.12-SNAPSHOT 8 | 9 | huntbugs-ant-plugin 10 | jar 11 | 12 | huntbugs-ant-plugin 13 | Ant plugin for HuntBugs Java bytecode static analysis tool 14 | 15 | 16 | 17 | one.util 18 | huntbugs 19 | ${project.version} 20 | 21 | 22 | org.apache.ant 23 | ant 24 | 1.7.1 25 | provided 26 | 27 | 28 | 29 | 30 | 31 | 32 | org.apache.maven.plugins 33 | maven-jar-plugin 34 | 2.4 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-shade-plugin 39 | 2.4.3 40 | 41 | 42 | package 43 | 44 | shade 45 | 46 | 47 | true 48 | nodeps 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | scm:git:https://github.com/amaembo/huntbugs.git 58 | scm:git:https://github.com/amaembo/huntbugs.git 59 | https://github.com/amaembo/huntbugs.git 60 | HEAD 61 | 62 | 63 | -------------------------------------------------------------------------------- /huntbugs-ant-plugin/src/main/resources/one/util/huntbugs/ant/antlib.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | huntbugs-maven-plugin 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /huntbugs-maven-plugin/.settings/org.eclipse.pde.core.prefs: -------------------------------------------------------------------------------- 1 | BUNDLE_ROOT_PATH=target/classes 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /huntbugs/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /huntbugs/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /huntbugs*.txt 3 | /huntbugs*.xml 4 | /huntbugs*.html 5 | /run.bat 6 | /procyon-decompiler-*.jar 7 | /dependency-reduced-pom.xml 8 | /dep 9 | /projects 10 | -------------------------------------------------------------------------------- /huntbugs/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | huntbugs 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /huntbugs/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /huntbugs/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /huntbugs/.settings/org.eclipse.pde.core.prefs: -------------------------------------------------------------------------------- 1 | BUNDLE_ROOT_PATH=target/classes 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/analysis/AnalysisListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.analysis; 17 | 18 | /** 19 | * @author Tagir Valeev 20 | * 21 | */ 22 | @FunctionalInterface 23 | public interface AnalysisListener { 24 | /** 25 | * @param stepName 26 | * @param className 27 | * @param count number of classes processed 28 | * @param total number of classes to process 29 | * @return false if cancel is requested 30 | */ 31 | public boolean eventOccurred(String stepName, String className, int count, int total); 32 | } 33 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/analysis/HuntBugsResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.analysis; 17 | 18 | import java.util.stream.Stream; 19 | 20 | import one.util.huntbugs.warning.Messages; 21 | import one.util.huntbugs.warning.Warning; 22 | 23 | /** 24 | * @author lan 25 | * 26 | */ 27 | public interface HuntBugsResult { 28 | /** 29 | * @return messages defined for this result 30 | */ 31 | Messages getMessages(); 32 | 33 | /** 34 | * @return warnings reported by this result 35 | */ 36 | Stream warnings(); 37 | 38 | /** 39 | * @return internal errors reported by this result 40 | */ 41 | Stream errors(); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/db/Mutability.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.db; 17 | 18 | import com.strobel.assembler.metadata.FieldDefinition; 19 | import com.strobel.assembler.metadata.TypeDefinition; 20 | import com.strobel.assembler.metadata.TypeReference; 21 | 22 | import one.util.huntbugs.registry.AbstractTypeDatabase; 23 | import one.util.huntbugs.registry.anno.TypeDatabase; 24 | 25 | /** 26 | * @author shustkost 27 | * 28 | */ 29 | @TypeDatabase 30 | public class Mutability extends AbstractTypeDatabase { 31 | public Mutability() { 32 | super(type -> Boolean.TRUE); 33 | } 34 | 35 | @Override 36 | protected void visitType(TypeDefinition td) { 37 | if(!td.isPublic()) 38 | return; 39 | for(FieldDefinition fd : td.getDeclaredFields()) { 40 | if(!fd.isStatic() && !fd.isFinal() && fd.isPublic()) { 41 | getOrCreate(td); 42 | return; 43 | } 44 | } 45 | } 46 | 47 | public boolean isKnownMutable(TypeReference tr) { 48 | return get(tr.getInternalName()) != null; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/AverageComputation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.decompiler.ast.AstCode; 19 | import com.strobel.decompiler.ast.Expression; 20 | 21 | import one.util.huntbugs.registry.MethodContext; 22 | import one.util.huntbugs.registry.anno.AstNodes; 23 | import one.util.huntbugs.registry.anno.AstVisitor; 24 | import one.util.huntbugs.registry.anno.WarningDefinition; 25 | import one.util.huntbugs.util.Exprs; 26 | import one.util.huntbugs.util.Nodes; 27 | 28 | /** 29 | * @author Tagir Valeev 30 | * 31 | */ 32 | @WarningDefinition(category = "BadPractice", name = "AverageComputationCouldOverflow", maxScore = 35) 33 | public class AverageComputation { 34 | @AstVisitor(nodes = AstNodes.EXPRESSIONS) 35 | public void visitExpression(Expression expr, MethodContext mc) { 36 | if (expr.getCode() != AstCode.LoadElement && expr.getCode() != AstCode.StoreElement) 37 | return; 38 | Expression idx = Exprs.getChild(expr, 1); 39 | if (!isDiv2(idx)) 40 | return; 41 | Expression orig = Exprs.getChild(idx, 0); 42 | if (orig.getCode() != AstCode.Add) 43 | return; 44 | mc.report("AverageComputationCouldOverflow", 0, idx); 45 | } 46 | 47 | private boolean isDiv2(Expression idx) { 48 | return (idx.getCode() == AstCode.Div && Integer.valueOf(2).equals(Nodes.getConstant(idx.getArguments().get(1)))) 49 | || (idx.getCode() == AstCode.Shr && Integer.valueOf(1).equals(Nodes.getConstant(idx.getArguments().get(1)))); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/DroppedExceptionObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.assembler.metadata.MethodReference; 19 | import com.strobel.decompiler.ast.AstCode; 20 | import com.strobel.decompiler.ast.Block; 21 | import com.strobel.decompiler.ast.Expression; 22 | 23 | import one.util.huntbugs.registry.MethodContext; 24 | import one.util.huntbugs.registry.anno.AstNodes; 25 | import one.util.huntbugs.registry.anno.AstVisitor; 26 | import one.util.huntbugs.registry.anno.WarningDefinition; 27 | import one.util.huntbugs.util.NodeChain; 28 | import one.util.huntbugs.util.Types; 29 | import one.util.huntbugs.warning.Roles; 30 | 31 | /** 32 | * @author Tagir Valeev 33 | * 34 | */ 35 | @WarningDefinition(category="Correctness", name="DroppedException", maxScore=60) 36 | public class DroppedExceptionObject { 37 | @AstVisitor(nodes=AstNodes.EXPRESSIONS) 38 | public void visit(Expression expr, NodeChain nc, MethodContext mc) { 39 | if (expr.getCode() == AstCode.InitObject || expr.getCode() == AstCode.InvokeSpecial 40 | && expr.getArguments().get(0).getCode() == AstCode.__New) { // Probably procyon bug: invokespecial(__new) is not collapsed to InitObject 41 | MethodReference mr = (MethodReference) expr.getOperand(); 42 | if(Types.isInstance(mr.getDeclaringType(), "java/lang/Throwable") || 43 | Types.isInstance(mr.getDeclaringType(), "java/lang/Exception")) { 44 | if(nc.getNode() instanceof Block) { 45 | mc.report("DroppedException", 0, expr, Roles.EXCEPTION.create(mr.getDeclaringType())); 46 | } 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/EmptySync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import java.util.List; 19 | 20 | import com.strobel.decompiler.ast.AstCode; 21 | import com.strobel.decompiler.ast.Block; 22 | import com.strobel.decompiler.ast.Node; 23 | import com.strobel.decompiler.ast.TryCatchBlock; 24 | 25 | import one.util.huntbugs.registry.MethodContext; 26 | import one.util.huntbugs.registry.anno.AstVisitor; 27 | import one.util.huntbugs.registry.anno.WarningDefinition; 28 | import one.util.huntbugs.util.Nodes; 29 | 30 | /** 31 | * @author Tagir Valeev 32 | * 33 | */ 34 | @WarningDefinition(category="Multithreading", name="EmptySynchronizeBlock", maxScore=50) 35 | public class EmptySync { 36 | @AstVisitor 37 | public void visit(Node node, MethodContext mc) { 38 | if(node instanceof Block) { 39 | List body = ((Block) node).getBody(); 40 | for(int i=0; i { 39 | if(constant instanceof Float && Float.isNaN((float) constant)) { 40 | ctx.report("FloatCompareToNaN", 0, arg, 41 | Roles.REPLACEMENT_METHOD.create("java/lang/Float", "isNaN", "(F)Z"), 42 | USED_TYPE.create("float")); 43 | } else if(constant instanceof Double && Double.isNaN((double) constant)) { 44 | ctx.report("FloatCompareToNaN", 0, arg, Roles.REPLACEMENT_METHOD.create("java/lang/Double", "isNaN", "(D)Z"), 45 | USED_TYPE.create("double")); 46 | } 47 | }); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/JcipProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.assembler.metadata.FieldDefinition; 19 | import com.strobel.assembler.metadata.Flags; 20 | import com.strobel.assembler.metadata.TypeDefinition; 21 | import com.strobel.assembler.metadata.annotations.CustomAnnotation; 22 | 23 | import one.util.huntbugs.registry.FieldContext; 24 | import one.util.huntbugs.registry.anno.ClassVisitor; 25 | import one.util.huntbugs.registry.anno.FieldVisitor; 26 | import one.util.huntbugs.registry.anno.WarningDefinition; 27 | 28 | /** 29 | * @author lan 30 | * 31 | */ 32 | @WarningDefinition(category="BadPractice", name="NonFinalFieldInImmutableClass", maxScore=60) 33 | public class JcipProblems { 34 | @ClassVisitor 35 | public boolean checkClass(TypeDefinition td) { 36 | for(CustomAnnotation ca : td.getAnnotations()) { 37 | String name = ca.getAnnotationType().getInternalName(); 38 | if(name.equals("net/jcip/annotations/Immutable") || name.equals("javax/annotation/concurrent/Immutable")) 39 | return true; 40 | } 41 | return false; 42 | } 43 | 44 | @FieldVisitor 45 | public void visitField(FieldDefinition fd, FieldContext fc) { 46 | if(!Flags.testAny(fd.getFlags(), Flags.VOLATILE | Flags.FINAL | Flags.TRANSIENT | Flags.STATIC)) { 47 | fc.report("NonFinalFieldInImmutableClass", 0); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/NewGetClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.assembler.metadata.MethodReference; 19 | import com.strobel.decompiler.ast.AstCode; 20 | import com.strobel.decompiler.ast.Expression; 21 | 22 | import one.util.huntbugs.registry.MethodContext; 23 | import one.util.huntbugs.registry.anno.AstNodes; 24 | import one.util.huntbugs.registry.anno.AstVisitor; 25 | import one.util.huntbugs.registry.anno.WarningDefinition; 26 | import one.util.huntbugs.util.Methods; 27 | import one.util.huntbugs.warning.Role.TypeRole; 28 | 29 | /** 30 | * @author Tagir Valeev 31 | * 32 | */ 33 | @WarningDefinition(category="Performance", name="NewForGetClass", maxScore=50) 34 | public class NewGetClass { 35 | private static final TypeRole OBJECT_TYPE = TypeRole.forName("OBJECT_TYPE"); 36 | 37 | @AstVisitor(nodes=AstNodes.EXPRESSIONS) 38 | public void visit(Expression node, MethodContext ctx) { 39 | if(node.getCode() == AstCode.InvokeVirtual) { 40 | MethodReference ref = (MethodReference) node.getOperand(); 41 | if (Methods.isGetClass(ref) && node.getArguments().get(0).getCode() == AstCode.InitObject) { 42 | ctx.report("NewForGetClass", 0, node, OBJECT_TYPE.create(ref.getDeclaringType())); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/RedundantInterfaces.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.assembler.metadata.TypeDefinition; 19 | import com.strobel.assembler.metadata.TypeReference; 20 | 21 | import one.util.huntbugs.registry.ClassContext; 22 | import one.util.huntbugs.registry.anno.ClassVisitor; 23 | import one.util.huntbugs.registry.anno.WarningDefinition; 24 | import one.util.huntbugs.util.Types; 25 | import one.util.huntbugs.warning.Roles; 26 | 27 | /** 28 | * @author Tagir Valeev 29 | * 30 | */ 31 | @WarningDefinition(category = "CodeStyle", name = "RedundantInterface", maxScore = 20) 32 | public class RedundantInterfaces { 33 | @ClassVisitor 34 | public void visit(TypeDefinition td, ClassContext cc) { 35 | TypeDefinition baseType = td.getBaseType().resolve(); 36 | if(baseType == null || Types.isObject(baseType)) 37 | return; 38 | for(TypeReference tr : td.getExplicitInterfaces()) { 39 | if(tr.getInternalName().equals("java/io/Serializable")) { 40 | continue; 41 | } 42 | if(Types.isInstance(baseType, tr)) { 43 | cc.report("RedundantInterface", td.isPublic() ? 0 : 10, Roles.INTERFACE.create(tr)); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/UnreachableCatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import com.strobel.decompiler.ast.CatchBlock; 19 | import com.strobel.decompiler.ast.Expression; 20 | import com.strobel.decompiler.ast.Node; 21 | 22 | import one.util.huntbugs.registry.MethodContext; 23 | import one.util.huntbugs.registry.anno.AstVisitor; 24 | import one.util.huntbugs.registry.anno.WarningDefinition; 25 | import one.util.huntbugs.util.Nodes; 26 | 27 | /** 28 | * @author lan 29 | * 30 | */ 31 | @WarningDefinition(category="RedundantCode", name="UnreachableCatch", maxScore=50) 32 | public class UnreachableCatch { 33 | @AstVisitor 34 | public void visit(Node node, MethodContext mc) { 35 | if(node instanceof CatchBlock) { 36 | Expression firstExpr = (Expression) Nodes.find(node, Expression.class::isInstance); 37 | if(firstExpr != null && !mc.isReachable(firstExpr)) { 38 | mc.report("UnreachableCatch", 0, firstExpr); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/detect/UselessVoidMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.detect; 17 | 18 | import java.util.List; 19 | 20 | import com.strobel.assembler.metadata.MethodDefinition; 21 | import com.strobel.decompiler.ast.AstCode; 22 | import com.strobel.decompiler.ast.Block; 23 | import com.strobel.decompiler.ast.Node; 24 | 25 | import one.util.huntbugs.db.MethodStats; 26 | import one.util.huntbugs.db.MethodStats.MethodData; 27 | import one.util.huntbugs.registry.MethodContext; 28 | import one.util.huntbugs.registry.anno.AstNodes; 29 | import one.util.huntbugs.registry.anno.AstVisitor; 30 | import one.util.huntbugs.registry.anno.WarningDefinition; 31 | import one.util.huntbugs.util.Nodes; 32 | 33 | /** 34 | * @author lan 35 | * 36 | */ 37 | @WarningDefinition(category="RedundantCode", name="UselessVoidMethod", maxScore=50) 38 | public class UselessVoidMethod { 39 | @AstVisitor(nodes=AstNodes.ROOT) 40 | public void checkMethod(Block root, MethodDefinition md, MethodStats ms, MethodContext mc) { 41 | if(md.getReturnType().isVoid()) { 42 | MethodData stats = ms.getStats(md); 43 | if (stats != null && !stats.mayHaveSideEffect(true) && !stats.testAny(MethodStats.METHOD_MAY_THROW, true)) { 44 | List body = root.getBody(); 45 | if(body.isEmpty()) 46 | return; 47 | if(md.isConstructor() && body.size() == 1 && Nodes.isOp(body.get(0), AstCode.InvokeSpecial)) 48 | return; 49 | int codeSize = Nodes.estimateCodeSize(root); 50 | int priority = codeSize < 10 ? 20 : codeSize < 20 ? 10 : codeSize < 30 ? 5 : 0; 51 | mc.report("UselessVoidMethod", priority); 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/Annotator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import java.util.function.Consumer; 19 | 20 | import one.util.huntbugs.util.Nodes; 21 | 22 | import com.strobel.decompiler.ast.Expression; 23 | import com.strobel.decompiler.ast.Node; 24 | 25 | /** 26 | * @author shustkost 27 | * 28 | */ 29 | abstract class Annotator { 30 | private final T defValue; 31 | private final int idx; 32 | 33 | protected Annotator(String name, T defValue) { 34 | this.defValue = defValue; 35 | this.idx = Annotators.register(name); 36 | } 37 | 38 | protected static void forExpressions(Node node, Consumer cons) { 39 | for(Node child : Nodes.getChildren(node)) { 40 | if(child instanceof Expression) { 41 | cons.accept((Expression)child); 42 | } else { 43 | forExpressions(child, cons); 44 | } 45 | } 46 | } 47 | 48 | protected T get(Expression expr) { 49 | @SuppressWarnings("unchecked") 50 | T data = (T) Annotators.get(expr, idx); 51 | return data == null ? defValue : data; 52 | } 53 | 54 | protected void putIfAbsent(Expression expr, T data) { 55 | Annotators.replace(expr, idx, null, data); 56 | } 57 | 58 | protected void put(Expression expr, T data) { 59 | Annotators.put(expr, idx, data); 60 | } 61 | 62 | protected void remove(Expression expr) { 63 | Annotators.remove(expr, idx); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/CodeBlock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import com.strobel.decompiler.ast.Expression; 19 | 20 | public class CodeBlock { 21 | public final Expression startExpr; 22 | public final int length; 23 | public final boolean isExceptional; 24 | 25 | public CodeBlock(Expression startExpr, int length, boolean isExceptional) { 26 | this.startExpr = startExpr; 27 | this.length = length; 28 | this.isExceptional = isExceptional; 29 | } 30 | } -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/Dataflow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import com.strobel.decompiler.ast.Expression; 19 | 20 | /** 21 | * @author lan 22 | * 23 | */ 24 | interface Dataflow { 25 | public STATE makeEntryState(); 26 | 27 | public STATE transferState(STATE src, Expression expr); 28 | 29 | public STATE transferExceptionalState(STATE src, Expression expr); 30 | 31 | public TrueFalse transferConditionalState(STATE src, Expression expr); 32 | 33 | public STATE mergeStates(STATE s1, STATE s2); 34 | 35 | public boolean sameState(STATE s1, STATE s2); 36 | 37 | public FACT makeFact(STATE state, Expression expr); 38 | 39 | public FACT makeUnknownFact(); 40 | 41 | public FACT mergeFacts(FACT f1, FACT f2); 42 | 43 | public boolean sameFact(FACT f1, FACT f2); 44 | 45 | public default void onSuccess(STATE exitState) {}; 46 | 47 | public default void onFail(STATE exitState) {}; 48 | } 49 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/EnumConstant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import java.util.Objects; 19 | 20 | import one.util.huntbugs.warning.WarningAnnotation; 21 | 22 | /** 23 | * @author Tagir Valeev 24 | * 25 | */ 26 | public final class EnumConstant { 27 | private final String typeName; 28 | private final String name; 29 | 30 | EnumConstant(String typeName, String name) { 31 | super(); 32 | this.typeName = Objects.requireNonNull(typeName); 33 | this.name = Objects.requireNonNull(name); 34 | } 35 | 36 | public String getTypeName() { 37 | return typeName; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | return typeName.hashCode() * 31 + name.hashCode(); 47 | } 48 | 49 | @Override 50 | public boolean equals(Object obj) { 51 | if (this == obj) 52 | return true; 53 | if (obj == null || getClass() != obj.getClass()) 54 | return false; 55 | EnumConstant other = (EnumConstant) obj; 56 | return name.equals(other.name) && typeName.equals(other.typeName); 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return new WarningAnnotation.TypeInfo(typeName).getSimpleName()+"."+name; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/Exceptional.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import one.util.huntbugs.warning.WarningAnnotation.TypeInfo; 19 | 20 | /** 21 | * @author lan 22 | * 23 | */ 24 | public class Exceptional { 25 | private final TypeInfo type; 26 | 27 | Exceptional(Throwable t) { 28 | this.type = new TypeInfo(t.getClass().getName().replace('.', '/')); 29 | } 30 | 31 | public TypeInfo getType() { 32 | return type; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return type.hashCode(); 38 | } 39 | 40 | @Override 41 | public boolean equals(Object obj) { 42 | if (this == obj) 43 | return true; 44 | if (obj == null || getClass() != obj.getClass()) 45 | return false; 46 | Exceptional other = (Exceptional) obj; 47 | return type.equals(other.type); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "Exception["+type+"]"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/GraphSearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | import com.strobel.decompiler.ast.Expression; 19 | 20 | import one.util.huntbugs.flow.CFG.EdgeType; 21 | 22 | /** 23 | * @author lan 24 | * 25 | */ 26 | public interface GraphSearch { 27 | T markStart(Expression expr, boolean isEntry); 28 | T transfer(T orig, Expression from, EdgeType edge, Expression to); 29 | T merge(T f1, T f2); 30 | } 31 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/Inf.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | /** 19 | * Publicly available annotators 20 | * 21 | * @author Tagir Valeev 22 | */ 23 | public class Inf { 24 | /** 25 | * Annotator which can determine the sources of given expression 26 | */ 27 | public static final SourceAnnotator SOURCE = new SourceAnnotator(); 28 | 29 | /** 30 | * Annotator which can determine the statically known constant value of given expression (if any) 31 | */ 32 | public static final ConstAnnotator CONST = new ConstAnnotator(); 33 | 34 | /** 35 | * Annotator which can determine whether given expression is subtype of given type or not 36 | */ 37 | public static final ETypeAnnotator ETYPE = new ETypeAnnotator(); 38 | 39 | /** 40 | * Annotator which can determine whether given expression can be null 41 | */ 42 | public static final NullAnnotator NULL = new NullAnnotator(); 43 | 44 | /** 45 | * Annotator which can find the usages of given expression 46 | */ 47 | public static final BackLinkAnnotator BACKLINK = new BackLinkAnnotator(); 48 | 49 | /** 50 | * Annotator which can determine the purity of given expression 51 | */ 52 | public static final PurityAnnotator PURITY = new PurityAnnotator(); 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/TrueFalse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow; 17 | 18 | /** 19 | * @author lan 20 | * 21 | */ 22 | class TrueFalse { 23 | final STATE trueState, falseState; 24 | 25 | TrueFalse(STATE sameState) { 26 | this(sameState, sameState); 27 | } 28 | 29 | TrueFalse(STATE trueState, STATE falseState) { 30 | this(trueState, falseState, false); 31 | } 32 | 33 | TrueFalse(STATE trueState, STATE falseState, boolean invert) { 34 | this.trueState = invert ? falseState : trueState; 35 | this.falseState = invert ? trueState : falseState; 36 | } 37 | 38 | public TrueFalse invert() { 39 | return new TrueFalse<>(falseState, trueState); 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "TRUE: "+trueState+"\nFALSE: "+falseState; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/flow/etype/ComplexType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.flow.etype; 17 | 18 | import java.util.Set; 19 | import java.util.stream.Collectors; 20 | 21 | /** 22 | * @author shustkost 23 | * 24 | */ 25 | abstract class ComplexType implements EType { 26 | Set types; 27 | 28 | public ComplexType(Set types) { 29 | this.types = types; 30 | } 31 | 32 | abstract EType reduce(); 33 | 34 | abstract EType append(SingleType st); 35 | 36 | abstract EType appendAny(EType type); 37 | 38 | @Override 39 | public int hashCode() { 40 | return types.hashCode(); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | if (this == obj) 46 | return true; 47 | if (obj == null || getClass() != obj.getClass()) 48 | return false; 49 | return types.equals(((ComplexType) obj).types); 50 | } 51 | 52 | String toString(String delimiter) { 53 | return types.stream().map(EType::toString).map(typeStr -> typeStr.contains(" ") ? "(" + typeStr + ")" : typeStr) 54 | .sorted().collect(Collectors.joining(delimiter)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/output/CombinedReportWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.output; 17 | 18 | import java.util.List; 19 | 20 | import org.w3c.dom.Document; 21 | 22 | /** 23 | * @author isopov 24 | * 25 | */ 26 | class CombinedReportWriter implements ReportWriter { 27 | 28 | private final List writers; 29 | 30 | public CombinedReportWriter(List writers) { 31 | this.writers = writers; 32 | } 33 | 34 | @Override 35 | public void write(Document dom) { 36 | writers.forEach(writer -> writer.write(dom)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/output/ReportWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.output; 17 | 18 | import org.w3c.dom.Document; 19 | 20 | /** 21 | * @author isopov 22 | * 23 | */ 24 | interface ReportWriter { 25 | void write(Document dom); 26 | } 27 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/output/XmlReportWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.output; 17 | 18 | import java.io.Writer; 19 | import javax.xml.transform.Transformer; 20 | import javax.xml.transform.TransformerException; 21 | import javax.xml.transform.TransformerFactory; 22 | import javax.xml.transform.TransformerFactoryConfigurationError; 23 | import javax.xml.transform.dom.DOMSource; 24 | import javax.xml.transform.stream.StreamResult; 25 | import org.w3c.dom.Document; 26 | 27 | /** 28 | * @author Tagir Valeev 29 | * 30 | */ 31 | class XmlReportWriter implements ReportWriter { 32 | private final Writer target; 33 | 34 | public XmlReportWriter(Writer target) { 35 | this.target = target; 36 | } 37 | 38 | @Override 39 | public void write(Document dom) { 40 | try { 41 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); 42 | DOMSource source = new DOMSource(dom); 43 | 44 | StreamResult result = new StreamResult(target); 45 | transformer.setOutputProperty(javax.xml.transform.OutputKeys.MEDIA_TYPE, "text/xml"); 46 | transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); 47 | transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 48 | transformer.transform(source, result); 49 | } catch (TransformerFactoryConfigurationError | TransformerException e) { 50 | throw new RuntimeException(e); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/AbstractTypeDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | import java.util.Objects; 21 | import java.util.function.Function; 22 | 23 | import com.strobel.assembler.metadata.TypeDefinition; 24 | import com.strobel.assembler.metadata.TypeReference; 25 | 26 | /** 27 | * @author Tagir Valeev 28 | * 29 | */ 30 | public abstract class AbstractTypeDatabase { 31 | private final Map map = new HashMap<>(); 32 | private final Function fn; 33 | 34 | protected AbstractTypeDatabase(Function elementSupplier) { 35 | this.fn = Objects.requireNonNull(elementSupplier); 36 | } 37 | 38 | protected void visitType(TypeDefinition td) { 39 | // Default implementation is empty, should be subclasses 40 | } 41 | 42 | protected E getOrCreate(TypeReference ref) { 43 | return map.computeIfAbsent(ref.getInternalName(), fn); 44 | } 45 | 46 | protected E getOrCreate(String internalName) { 47 | return map.computeIfAbsent(internalName, fn); 48 | } 49 | 50 | public E get(TypeReference ref) { 51 | return map.get(ref.getInternalName()); 52 | } 53 | 54 | public E get(String internalName) { 55 | return map.get(internalName); 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return "Database <"+getClass().getName()+">"; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/ElementContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry; 17 | 18 | import one.util.huntbugs.analysis.Context; 19 | import one.util.huntbugs.warning.WarningType; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | abstract class ElementContext { 26 | protected final Context ctx; 27 | protected final Detector detector; 28 | 29 | public ElementContext(Context ctx, Detector detector) { 30 | this.ctx = ctx; 31 | this.detector = detector; 32 | } 33 | 34 | /** 35 | * Report an internal analysis error. Alternatively detector may just throw any exception instead. 36 | * 37 | * @param message message to report 38 | */ 39 | abstract public void error(String message); 40 | 41 | protected WarningType resolveWarningType(String warning, int priority) { 42 | WarningType wt = detector.getWarningType(warning); 43 | if (wt == null) { 44 | error("Tries to report a warning of non-declared type: " + warning); 45 | return null; 46 | } 47 | if (priority < 0) { 48 | error("Tries to report a warning " + warning + " with negative priority " + priority); 49 | return null; 50 | } 51 | if (wt.getMaxScore() - priority < ctx.getOptions().minScore) { 52 | return null; 53 | } 54 | return wt; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/FieldData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry; 17 | 18 | import com.strobel.assembler.metadata.FieldDefinition; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | final class FieldData { 25 | final FieldDefinition fd; 26 | 27 | public FieldData(FieldDefinition fd) { 28 | this.fd = fd; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return fd.getDeclaringType() + "." + fd.getName()+" : "+fd.getSignature(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/AssertNoWarning.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR}) 29 | public @interface AssertNoWarning { 30 | String value(); 31 | } 32 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/AssertWarning.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.warning.Warning; 24 | 25 | /** 26 | * @author Tagir Valeev 27 | * 28 | */ 29 | @Retention(RetentionPolicy.RUNTIME) 30 | @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR}) 31 | public @interface AssertWarning { 32 | String value(); 33 | 34 | int minScore() default Warning.MIN_SCORE; 35 | 36 | int maxScore() default Warning.MAX_SCORE; 37 | } 38 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/AstNodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import one.util.huntbugs.util.NodeChain; 19 | 20 | import com.strobel.decompiler.ast.Block; 21 | import com.strobel.decompiler.ast.Expression; 22 | import com.strobel.decompiler.ast.Node; 23 | 24 | /** 25 | * Type of the nodes visited by {@link AstVisitor} 26 | */ 27 | public enum AstNodes { 28 | /** 29 | * Visits all method nodes. Additional allowed argument types: {@link Node}, {@link NodeChain}. 30 | * May return void or boolean: if false then the rest of the method will be skipped. 31 | */ 32 | ALL, 33 | /** 34 | * Visits only expressions. Additional allowed argument types: {@link Expression}, {@link NodeChain}. 35 | * May return void or boolean: if false then the rest of the method will be skipped. 36 | */ 37 | EXPRESSIONS, 38 | /** 39 | * Visits only method root node. Additional allowed argument type: {@link Block}. 40 | * Returns void. 41 | */ 42 | ROOT 43 | } 44 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/AstVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.registry.MethodContext; 24 | 25 | import com.strobel.assembler.metadata.MethodDefinition; 26 | import com.strobel.assembler.metadata.TypeDefinition; 27 | 28 | /** 29 | * Method in detector class which called for AST nodes. 30 | * 31 | *

32 | * Allowed parameter types (no repeats): {@link MethodContext}, 33 | * {@link MethodDefinition}, {@link TypeDefinition} or any registered databases 34 | * (see {@link TypeDatabase}, {@link TypeDatabaseItem}) 35 | * 36 | *

37 | * For additional allowed types and allowed return values see the {@link AstNodes} description. 38 | */ 39 | @Retention(RetentionPolicy.RUNTIME) 40 | @Target(ElementType.METHOD) 41 | public @interface AstVisitor { 42 | AstNodes nodes() default AstNodes.ALL; 43 | 44 | String methodName() default ""; 45 | 46 | String methodSignature() default ""; 47 | 48 | int minVersion() default 0; 49 | } 50 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/ClassVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.registry.ClassContext; 24 | 25 | import com.strobel.assembler.metadata.TypeDefinition; 26 | 27 | /** 28 | * Method in detector class which called for every visited class. 29 | * 30 | *

31 | * Allowed parameter types (no repeats): {@link ClassContext}, 32 | * {@link TypeDefinition} or any registered databases (see {@link TypeDatabase}, 33 | * {@link TypeDatabaseItem}) 34 | * 35 | *

36 | * May return boolean or void. If returns false, any other visitors (e.g. 37 | * {@link MethodVisitor}, {@link AstVisitor}) defined in this detector will be 38 | * skipped for this class. 39 | */ 40 | @Retention(RetentionPolicy.RUNTIME) 41 | @Target(ElementType.METHOD) 42 | public @interface ClassVisitor { 43 | VisitOrder order() default VisitOrder.BEFORE; 44 | } 45 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/FieldVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.registry.FieldContext; 24 | 25 | import com.strobel.assembler.metadata.FieldDefinition; 26 | import com.strobel.assembler.metadata.TypeDefinition; 27 | 28 | /** 29 | * Method in detector class which called for every method in the class. 30 | * 31 | *

32 | * Allowed parameter types (no repeats): {@link FieldContext}, 33 | * {@link FieldDefinition}, {@link TypeDefinition} or any registered databases 34 | * (see {@link TypeDatabase}, {@link TypeDatabaseItem}) 35 | * 36 | *

37 | * Must return void. 38 | */ 39 | @Retention(RetentionPolicy.RUNTIME) 40 | @Target(ElementType.METHOD) 41 | public @interface FieldVisitor { 42 | } 43 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/MethodVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.registry.MethodContext; 24 | 25 | import com.strobel.assembler.metadata.MethodDefinition; 26 | import com.strobel.assembler.metadata.TypeDefinition; 27 | 28 | /** 29 | * Method in detector class which called for every method in the class. 30 | * 31 | *

32 | * Allowed parameter types (no repeats): {@link MethodContext}, 33 | * {@link MethodDefinition}, {@link TypeDefinition} or any registered databases 34 | * (see {@link TypeDatabase}, {@link TypeDatabaseItem}) 35 | * 36 | *

37 | * May return boolean or void. If returns false, any other visitors (e.g. 38 | * {@link AstVisitor}) defined in this detector will be skipped. 39 | */ 40 | @Retention(RetentionPolicy.RUNTIME) 41 | @Target(ElementType.METHOD) 42 | public @interface MethodVisitor { 43 | VisitOrder order() default VisitOrder.BEFORE; 44 | } 45 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/TypeDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.TYPE) 29 | public @interface TypeDatabase { 30 | } 31 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/TypeDatabaseItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | import one.util.huntbugs.registry.AbstractTypeDatabase; 24 | 25 | /** 26 | * @author Tagir Valeev 27 | * 28 | */ 29 | @Retention(RetentionPolicy.RUNTIME) 30 | @Target(ElementType.TYPE) 31 | public @interface TypeDatabaseItem { 32 | Class> parentDatabase(); 33 | } 34 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/VisitOrder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | /** 19 | * Specifies when visitor should be called 20 | */ 21 | public enum VisitOrder { 22 | /** 23 | * Call this visitor before nested objects are visited 24 | */ 25 | BEFORE, 26 | /** 27 | * Call this visitor after nested objects are visited 28 | */ 29 | AFTER 30 | } 31 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/WarningDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Repeatable; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.TYPE) 26 | @Repeatable(WarningDefinitions.class) 27 | public @interface WarningDefinition { 28 | String category(); 29 | 30 | String name(); 31 | 32 | int maxScore(); 33 | } 34 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/registry/anno/WarningDefinitions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry.anno; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | @Retention(RetentionPolicy.RUNTIME) 24 | @Target({ElementType.TYPE}) 25 | public @interface WarningDefinitions { 26 | WarningDefinition[] value(); 27 | } 28 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/repo/AuxRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.repo; 17 | 18 | import com.strobel.assembler.metadata.ITypeLoader; 19 | 20 | /** 21 | * Repository for auxiliary classes which should not be visited normally but should be accessible 22 | * 23 | * @author Tagir Valeev 24 | * 25 | */ 26 | public class AuxRepository implements Repository { 27 | private final ITypeLoader loader; 28 | 29 | public AuxRepository(ITypeLoader loader) { 30 | this.loader = loader; 31 | } 32 | 33 | @Override 34 | public ITypeLoader createTypeLoader() { 35 | return loader; 36 | } 37 | 38 | @Override 39 | public void visit(String rootPackage, RepositoryVisitor visitor) { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/repo/CompositeRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.repo; 17 | 18 | import java.util.List; 19 | import java.util.Objects; 20 | import com.strobel.assembler.metadata.CompositeTypeLoader; 21 | import com.strobel.assembler.metadata.ITypeLoader; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class CompositeRepository implements Repository { 28 | private final List repos; 29 | 30 | public CompositeRepository(List repos) { 31 | this.repos = Objects.requireNonNull(repos); 32 | } 33 | 34 | @Override 35 | public ITypeLoader createTypeLoader() { 36 | return new CompositeTypeLoader(repos.stream().map(Repository::createTypeLoader).toArray(ITypeLoader[]::new)); 37 | } 38 | 39 | @Override 40 | public void visit(String rootPackage, RepositoryVisitor visitor) { 41 | for(Repository repo : repos) 42 | repo.visit(rootPackage, visitor); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/repo/FilteredRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.repo; 17 | 18 | import java.util.function.Predicate; 19 | 20 | import com.strobel.assembler.metadata.ITypeLoader; 21 | 22 | /** 23 | * Repository which filters visited classes with given predicate 24 | * (though all classes still could be loaded). 25 | * 26 | * @author Tagir Valeev 27 | */ 28 | public class FilteredRepository implements Repository { 29 | private final Predicate classFilter; 30 | private final Repository repository; 31 | 32 | /** 33 | * @param repository parent repository 34 | * @param classFilter predicate which accepts internal class name (like "a/b/c/d") and returns true if it should be visited 35 | */ 36 | public FilteredRepository(Repository repository, Predicate classFilter) { 37 | this.repository = repository; 38 | this.classFilter = classFilter; 39 | } 40 | 41 | @Override 42 | public ITypeLoader createTypeLoader() { 43 | return repository.createTypeLoader(); 44 | } 45 | 46 | @Override 47 | public void visit(String rootPackage, RepositoryVisitor visitor) { 48 | repository.visit(rootPackage, new RepositoryVisitor() { 49 | 50 | @Override 51 | public boolean visitPackage(String packageName) { 52 | return visitor.visitPackage(packageName); 53 | } 54 | 55 | @Override 56 | public void visitClass(String className) { 57 | if(classFilter.test(className)) 58 | visitor.visitClass(className); 59 | } 60 | }); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/repo/RepositoryVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.repo; 17 | 18 | /** 19 | * @author Tagir Valeev 20 | * 21 | */ 22 | public interface RepositoryVisitor { 23 | /** 24 | * @param packageName 25 | * @return true if this package and its subpackages must be visited 26 | */ 27 | public boolean visitPackage(String packageName); 28 | 29 | public void visitClass(String className); 30 | } 31 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/spi/HuntBugsPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.spi; 17 | 18 | /** 19 | * This is extension point for 3-rd party detector providers. 20 | * 21 | * @author Mihails Volkovs 22 | * 23 | */ 24 | public interface HuntBugsPlugin { 25 | 26 | String name(); 27 | 28 | String detectorPackage(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/util/Annotations.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | import com.strobel.assembler.metadata.IAnnotationsProvider; 19 | import com.strobel.assembler.metadata.TypeReference; 20 | import com.strobel.assembler.metadata.annotations.CustomAnnotation; 21 | 22 | import one.util.huntbugs.registry.anno.AssertWarning; 23 | 24 | /** 25 | * @author shustkost 26 | * 27 | */ 28 | public class Annotations { 29 | private static final String INTERNAL_ANNOTATION_PACKAGE = AssertWarning.class.getPackage().getName(); 30 | 31 | public static boolean hasAnnotation(IAnnotationsProvider fd, boolean ignoreDeprecated) { 32 | for (CustomAnnotation ca : fd.getAnnotations()) { 33 | TypeReference annoType = ca.getAnnotationType(); 34 | if (annoType.getPackageName().equals(INTERNAL_ANNOTATION_PACKAGE)) 35 | continue; 36 | if (ignoreDeprecated && annoType.getInternalName().equals("java/lang/Deprecated")) 37 | continue; 38 | String simpleName = annoType.getSimpleName(); 39 | if (simpleName.startsWith("Suppress") && simpleName.endsWith("Warning")) 40 | continue; 41 | if (simpleName.equalsIgnoreCase("nonnull") || simpleName.equalsIgnoreCase("notnull") || simpleName 42 | .equalsIgnoreCase("nullable") || simpleName.equalsIgnoreCase("checkfornull")) 43 | continue; 44 | return true; 45 | } 46 | return false; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/util/Iterables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | import java.util.ArrayList; 19 | import java.util.Iterator; 20 | import java.util.List; 21 | 22 | /** 23 | * @author lan 24 | * 25 | */ 26 | public class Iterables { 27 | public static Iterable concat(Iterable it1, Iterable it2) { 28 | return () -> new Iterator() { 29 | boolean first = true; 30 | Iterator it = it1.iterator(); 31 | 32 | @Override 33 | public boolean hasNext() { 34 | boolean hasNext = it.hasNext(); 35 | if(hasNext) 36 | return true; 37 | if(first) { 38 | first = false; 39 | it = it2.iterator(); 40 | return hasNext(); 41 | } 42 | return false; 43 | } 44 | 45 | @Override 46 | public T next() { 47 | if(first) { 48 | hasNext(); 49 | } 50 | return it.next(); 51 | } 52 | }; 53 | } 54 | 55 | public static List toList(Iterable iterable) { 56 | List list = new ArrayList<>(); 57 | iterable.forEach(list::add); 58 | return list; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/util/Maps.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | import java.util.Collections; 19 | import java.util.Map; 20 | import java.util.Map.Entry; 21 | 22 | /** 23 | * @author lan 24 | * 25 | */ 26 | public class Maps { 27 | 28 | public static Map compactify(Map map) { 29 | if (map.isEmpty()) 30 | return Collections.emptyMap(); 31 | if (map.size() == 1) { 32 | Entry entry = map.entrySet().iterator().next(); 33 | return Collections.singletonMap(entry.getKey(), entry.getValue()); 34 | } 35 | return map; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/util/Variables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | import java.lang.reflect.Field; 19 | import java.security.AccessController; 20 | import java.security.PrivilegedAction; 21 | 22 | import com.strobel.assembler.metadata.MethodDefinition; 23 | import com.strobel.assembler.metadata.VariableDefinition; 24 | 25 | /** 26 | * @author Tagir Valeev 27 | * 28 | */ 29 | public class Variables { 30 | static final Field variableMethodDefinitionField; 31 | 32 | static { 33 | variableMethodDefinitionField = AccessController.doPrivileged((PrivilegedAction) () -> { 34 | try { 35 | Field f = VariableDefinition.class.getDeclaredField("_declaringMethod"); 36 | f.setAccessible(true); 37 | return f; 38 | } catch (NoSuchFieldException | SecurityException e) { 39 | throw new InternalError(e); 40 | } 41 | }); 42 | } 43 | 44 | public static MethodDefinition getMethodDefinition(VariableDefinition vd) { 45 | try { 46 | return (MethodDefinition) variableMethodDefinitionField.get(vd); 47 | } catch (IllegalArgumentException | IllegalAccessException e) { 48 | throw new InternalError(e); 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/util/YesNoMaybe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | /** 19 | * @author shustkost 20 | * 21 | */ 22 | public enum YesNoMaybe { 23 | YES, MAYBE, NO; 24 | 25 | public boolean yes() { 26 | return this == YES; 27 | } 28 | 29 | public boolean no() { 30 | return this == NO; 31 | } 32 | 33 | public static YesNoMaybe of(boolean b) { 34 | return b ? YES : NO; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/WarningStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning; 17 | 18 | /** 19 | * Warning status suitable to compare new build with old build 20 | * 21 | * @author Tagir Valeev 22 | */ 23 | public enum WarningStatus { 24 | /** 25 | * Default status: either comparison is not performed or bug appears both in new and old builds 26 | */ 27 | DEFAULT, 28 | 29 | /** 30 | * Newly-discovered warning 31 | */ 32 | ADDED, 33 | 34 | /** 35 | * Changed warning annotation (and probably priority) 36 | */ 37 | CHANGED, 38 | 39 | /** 40 | * Warning is unchanged, but its score became higher 41 | */ 42 | SCORE_RAISED, 43 | 44 | /** 45 | * Warning is unchanged, but its score became lower 46 | */ 47 | SCORE_LOWERED, 48 | 49 | /** 50 | * Fixed warning (does not appear in the new build) 51 | */ 52 | FIXED 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/WarningType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning; 17 | 18 | import one.util.huntbugs.registry.anno.WarningDefinition; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | public class WarningType { 25 | private final String category; 26 | private final String name; 27 | private final int maxScore; 28 | 29 | public WarningType(String category, String name, int maxScore) { 30 | this.category = category; 31 | this.name = name; 32 | this.maxScore = maxScore; 33 | } 34 | 35 | public WarningType(WarningDefinition def) { 36 | this(def.category(), def.name(), def.maxScore()); 37 | } 38 | 39 | public String getCategory() { 40 | return category; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public int getMaxScore() { 48 | return maxScore; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "WarningType [category=" + category + ", name=" + name + ", maxScore=" + maxScore + "]"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/rule/CategoryRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning.rule; 17 | 18 | import one.util.huntbugs.warning.WarningType; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | public class CategoryRule implements Rule { 25 | private final int adjustment; 26 | private final String category; 27 | 28 | public CategoryRule(String category, int adjustment) { 29 | this.category = category; 30 | this.adjustment = adjustment; 31 | } 32 | 33 | @Override 34 | public WarningType adjust(WarningType wt) { 35 | if(wt.getCategory().equalsIgnoreCase(category)) 36 | return new WarningType(wt.getCategory(), wt.getName(), wt.getMaxScore()+adjustment); 37 | return wt; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/rule/CompositeRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning.rule; 17 | 18 | import java.util.List; 19 | 20 | import one.util.huntbugs.warning.WarningType; 21 | 22 | /** 23 | * @author Tagir Valeev 24 | * 25 | */ 26 | public class CompositeRule implements Rule { 27 | private final List rules; 28 | 29 | public CompositeRule(List rules) { 30 | super(); 31 | this.rules = rules; 32 | } 33 | 34 | @Override 35 | public WarningType adjust(WarningType wt) { 36 | for(Rule rule : rules) { 37 | wt = rule.adjust(wt); 38 | } 39 | return wt; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/rule/RegexRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning.rule; 17 | 18 | import java.util.regex.Pattern; 19 | 20 | import one.util.huntbugs.warning.WarningType; 21 | 22 | /** 23 | * @author Tagir Valeev 24 | * 25 | */ 26 | public class RegexRule implements Rule { 27 | private final Pattern regex; 28 | private final int adjustment; 29 | 30 | public RegexRule(String regex, int adjustment) { 31 | this.regex = Pattern.compile(regex); 32 | this.adjustment = adjustment; 33 | } 34 | 35 | @Override 36 | public WarningType adjust(WarningType wt) { 37 | if(regex.matcher(wt.getName()).matches()) 38 | return new WarningType(wt.getCategory(), wt.getName(), wt.getMaxScore()+adjustment); 39 | return wt; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /huntbugs/src/main/java/one/util/huntbugs/warning/rule/Rule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.warning.rule; 17 | 18 | import one.util.huntbugs.warning.WarningType; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | public interface Rule { 25 | WarningType adjust(WarningType wt); 26 | 27 | public static final Rule NULL = new Rule() { 28 | @Override 29 | public WarningType adjust(WarningType wt) { 30 | return wt; 31 | } 32 | }; 33 | } 34 | -------------------------------------------------------------------------------- /huntbugs/src/main/resources/huntbugs/messages.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HuntBugs warning descriptions 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/DataTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs; 17 | 18 | import one.util.huntbugs.spi.DataTests; 19 | import org.junit.Test; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | */ 24 | public class DataTest { 25 | 26 | @Test 27 | public void test() throws Exception { 28 | DataTests.test("one/util/huntbugs/testdata"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/FilteredRepositoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs; 17 | 18 | import static org.junit.Assert.*; 19 | 20 | import java.util.stream.Collectors; 21 | 22 | import one.util.huntbugs.analysis.AnalysisOptions; 23 | import one.util.huntbugs.analysis.Context; 24 | import one.util.huntbugs.repo.FilteredRepository; 25 | import one.util.huntbugs.repo.Repository; 26 | 27 | import org.junit.Test; 28 | 29 | /** 30 | * @author Tagir Valeev 31 | * 32 | */ 33 | public class FilteredRepositoryTest { 34 | @Test 35 | public void filteredRepoTest() { 36 | Repository parent = Repository.createSelfRepository(); 37 | Repository repo = new FilteredRepository(parent, cn -> cn.endsWith("/TestSyncGetClass")); 38 | Context ctx = new Context(repo, new AnalysisOptions()); 39 | ctx.analyzePackage("one/util/huntbugs/testdata"); 40 | assertEquals("", ctx.errors().map(Object::toString).collect(Collectors.joining())); 41 | assertTrue(ctx.warnings().count() > 0); 42 | ctx.warnings().allMatch(w -> w.getType().getName().equals("SyncOnGetClass")); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/MessagesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs; 17 | 18 | import static org.junit.Assert.*; 19 | 20 | import java.util.Arrays; 21 | 22 | import one.util.huntbugs.warning.Formatter; 23 | import one.util.huntbugs.warning.Messages; 24 | import one.util.huntbugs.warning.Warning; 25 | import one.util.huntbugs.warning.WarningType; 26 | import one.util.huntbugs.warning.Messages.Message; 27 | import one.util.huntbugs.warning.Roles; 28 | 29 | import org.junit.Test; 30 | 31 | /** 32 | * @author Tagir Valeev 33 | * 34 | */ 35 | public class MessagesTest { 36 | @Test 37 | public void testMessages() { 38 | Messages m = Messages.load(); 39 | Message msg = m.getMessagesForType("RoughConstantValue"); 40 | assertEquals("Rough value of known constant is used", msg.getTitle()); 41 | assertEquals("Constant $NUMBER$ should be replaced with $REPLACEMENT$", msg.getDescription()); 42 | } 43 | 44 | @Test 45 | public void testFormatter() { 46 | Formatter f = new Formatter(); 47 | WarningType type = new WarningType("BadPractice", "RoughConstantValue", 60); 48 | Warning w = new Warning(type, 0, Arrays.asList(Roles.NUMBER.create(3.1415), Roles.REPLACEMENT_STRING.create( 49 | "Math.PI"))); 50 | assertEquals("Rough value of known constant is used", f.getTitle(w)); 51 | assertEquals("Constant 3.1415 should be replaced with Math.PI", f.getDescription(w)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/OptionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs; 17 | 18 | import static org.junit.Assert.*; 19 | import one.util.huntbugs.analysis.AnalysisOptions; 20 | 21 | import org.junit.Test; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class OptionsTest { 28 | @Test 29 | public void testAnalysisOptions() { 30 | AnalysisOptions opt = new AnalysisOptions(); 31 | assertTrue(opt.addBootClassPath); 32 | opt.set("minScore", "40"); 33 | opt.set("addBootClassPath", "False"); 34 | assertEquals(40, opt.minScore); 35 | assertFalse(opt.addBootClassPath); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/RuleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs; 17 | 18 | import static org.junit.Assert.*; 19 | 20 | import java.util.Arrays; 21 | 22 | import one.util.huntbugs.analysis.AnalysisOptions; 23 | import one.util.huntbugs.analysis.Context; 24 | import one.util.huntbugs.repo.Repository; 25 | import one.util.huntbugs.warning.rule.CategoryRule; 26 | import one.util.huntbugs.warning.rule.CompositeRule; 27 | import one.util.huntbugs.warning.rule.RegexRule; 28 | 29 | import org.junit.Test; 30 | 31 | /** 32 | * @author Tagir Valeev 33 | * 34 | */ 35 | public class RuleTest { 36 | @Test 37 | public void testRules() { 38 | AnalysisOptions options = new AnalysisOptions(); 39 | Context ctx = new Context(Repository.createNullRepository(), options); 40 | assertEquals(60, ctx.getWarningType("RoughConstantValue").getMaxScore()); 41 | options.setRule(new CategoryRule("BadPractice", -10)); 42 | ctx = new Context(Repository.createNullRepository(), options); 43 | assertEquals(50, ctx.getWarningType("RoughConstantValue").getMaxScore()); 44 | options.setRule(new CompositeRule(Arrays.asList(options.getRule(), new RegexRule("Rough.+", -20)))); 45 | ctx = new Context(Repository.createNullRepository(), options); 46 | assertEquals(30, ctx.getWarningType("RoughConstantValue").getMaxScore()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/asserter/TestAsserter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.asserter; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | @AssertWarning("BBB") 26 | @AssertNoWarning("RoughConstantValue") 27 | public class TestAsserter { 28 | @AssertWarning("CCC") 29 | @AssertNoWarning("BadNameOfField") 30 | int TheField = 123; 31 | 32 | @AssertWarning("BadName*") 33 | @SuppressMyWarning("BadNameOfField") 34 | int TheFieldSuppressed = 123; 35 | 36 | @AssertWarning("AAA") 37 | @AssertNoWarning("Rough*") 38 | public double test() { 39 | return 3.1415*TheField; 40 | } 41 | 42 | @AssertWarning("ParameterOverwritte*") 43 | @SuppressMyWarning("Param*") 44 | public double testSuppress(int x) { 45 | x = 10; 46 | return x*2; 47 | } 48 | 49 | @AssertWarning("ParameterOverwritt*") 50 | public double testSuppressParam(@SuppressMyWarning("all") int x) { 51 | x = 10; 52 | return x*2; 53 | } 54 | 55 | @AssertNoWarning("UncalledPrivateMethod") 56 | private void uncalled() { 57 | System.out.println("Uncalled"); 58 | } 59 | 60 | public void testLocalClass() { 61 | class X { 62 | @AssertNoWarning("ParameterOverwritten") 63 | public void print(int x) { 64 | x = 10; 65 | System.out.println(x); 66 | } 67 | } 68 | new X().print(5); 69 | } 70 | 71 | @interface SuppressMyWarning { 72 | String value(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/registry/DetectorRegistryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.registry; 17 | 18 | import one.util.huntbugs.analysis.AnalysisOptions; 19 | import one.util.huntbugs.analysis.Context; 20 | import one.util.huntbugs.registry.anno.WarningDefinition; 21 | import one.util.huntbugs.repo.Repository; 22 | import org.junit.Before; 23 | import org.junit.Test; 24 | 25 | import static org.junit.Assert.assertEquals; 26 | import static org.junit.Assert.assertFalse; 27 | import static org.junit.Assert.assertTrue; 28 | 29 | /** 30 | * @author Mihails Volkovs 31 | */ 32 | public class DetectorRegistryTest { 33 | 34 | private Context context; 35 | 36 | private DetectorRegistry detectorRegistry; 37 | 38 | @Before 39 | public void setUp() { 40 | context = new Context(Repository.createNullRepository(), new AnalysisOptions()); 41 | detectorRegistry = new DetectorRegistry(context); 42 | } 43 | 44 | @Test 45 | public void addDetector() { 46 | final long WARNINGS = getWarnings(); 47 | assertTrue(detectorRegistry.addDetector(TestDetector.class)); 48 | assertEquals(WARNINGS + 2, getWarnings()); 49 | } 50 | 51 | @Test 52 | public void addFakeDetector() { 53 | final long WARNINGS = getWarnings(); 54 | assertFalse(detectorRegistry.addDetector(DetectorRegistryTest.class)); 55 | assertEquals(WARNINGS, getWarnings()); 56 | } 57 | 58 | private long getWarnings() { 59 | return context.getStat("WarningTypes.Total"); 60 | } 61 | 62 | @WarningDefinition(category="DetectorRegistryTest", name="DetectorRegistryTest", maxScore=80) 63 | @WarningDefinition(category="DetectorRegistryTest", name="DetectorRegistryTest", maxScore=80) 64 | private static class TestDetector { 65 | 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestAppendObjectOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.BufferedOutputStream; 19 | import java.io.FileOutputStream; 20 | import java.io.IOException; 21 | import java.io.ObjectOutputStream; 22 | import java.io.OutputStream; 23 | 24 | import one.util.huntbugs.registry.anno.AssertNoWarning; 25 | import one.util.huntbugs.registry.anno.AssertWarning; 26 | 27 | /** 28 | * @author Tagir Valeev 29 | * 30 | */ 31 | public class TestAppendObjectOutputStream { 32 | @AssertWarning("AppendObjectOutputStream") 33 | public ObjectOutputStream createStream() throws IOException { 34 | return new ObjectOutputStream(new FileOutputStream("/tmp/file", true)); 35 | } 36 | 37 | @AssertWarning("AppendObjectOutputStream") 38 | public ObjectOutputStream createStreamComplex() throws IOException { 39 | OutputStream out = new FileOutputStream("/tmp/file", true); 40 | out = new BufferedOutputStream(out); 41 | return new ObjectOutputStream(out); 42 | } 43 | 44 | @AssertWarning("AppendObjectOutputStream") 45 | public ObjectOutputStream createStreamConditional(boolean buffered) throws IOException { 46 | OutputStream out = new FileOutputStream("/tmp/file", true); 47 | if(buffered) 48 | out = new BufferedOutputStream(out); 49 | return new ObjectOutputStream(out); 50 | } 51 | 52 | @AssertNoWarning("AppendObjectOutputStream") 53 | public ObjectOutputStream createStreamNoAppend() throws IOException { 54 | return new ObjectOutputStream(new FileOutputStream("/tmp/file", false)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestAverageComputation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestAverageComputation { 26 | @AssertWarning("AverageComputationCouldOverflow") 27 | static int bsearch(int[] a, int low, int high, int key) { 28 | while (low <= high) { 29 | int mid = (low + high) >> 1; 30 | int midVal = a[mid]; 31 | 32 | if (midVal < key) 33 | low = mid + 1; 34 | else if (midVal > key) 35 | high = mid - 1; 36 | else 37 | return mid; 38 | } 39 | return -1; 40 | } 41 | 42 | @AssertWarning("AverageComputationCouldOverflow") 43 | static int bsearch2(int[] a, int low, int high, int key) { 44 | while (low <= high) { 45 | int mid = (low + high) / 2; 46 | int midVal = a[mid]; 47 | 48 | if (midVal < key) 49 | low = mid + 1; 50 | else if (midVal > key) 51 | high = mid - 1; 52 | else 53 | return mid; 54 | } 55 | return -1; 56 | } 57 | 58 | @AssertNoWarning("AverageComputationCouldOverflow") 59 | static int bsearchOk(int[] a, int low, int high, int key) { 60 | while (low <= high) { 61 | int mid = (low + high) >>> 1; 62 | int midVal = a[mid]; 63 | 64 | if (midVal < key) 65 | low = mid + 1; 66 | else if (midVal > key) 67 | high = mid - 1; 68 | else 69 | return mid; 70 | } 71 | return -1; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestBadMethodReferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Random; 19 | 20 | import one.util.huntbugs.registry.anno.AssertWarning; 21 | 22 | /** 23 | * @author Tagir Valeev 24 | * 25 | */ 26 | public class TestBadMethodReferences { 27 | @AssertWarning("MaxMinMethodReferenceForComparator") 28 | public void test() { 29 | new Random().ints(1000).boxed().sorted(Integer::max).forEach(System.out::println); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestBadMonitorObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertWarning; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | public class TestBadMonitorObject { 25 | @AssertWarning("SynchronizationOnBoxedNumber") 26 | public void syncNumber() { 27 | Integer num = 1; 28 | synchronized(num) { 29 | System.out.println("In lock"); 30 | } 31 | } 32 | 33 | @AssertWarning("SynchronizationOnUnsharedBoxed") 34 | public void syncUnsharedNumber() { 35 | synchronized(new Integer(1)) { 36 | System.out.println("In lock"); 37 | } 38 | } 39 | 40 | @AssertWarning("SynchronizationOnUnsharedBoxed") 41 | public void syncUnsharedBoolean() { 42 | synchronized(new Boolean(true)) { 43 | System.out.println("In lock"); 44 | } 45 | } 46 | 47 | @AssertWarning("SynchronizationOnBoolean") 48 | public void syncBoolean() { 49 | synchronized(Boolean.TRUE) { 50 | System.out.println("In lock"); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestCheckReturnValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.io.Reader; 21 | 22 | import one.util.huntbugs.registry.anno.AssertNoWarning; 23 | import one.util.huntbugs.registry.anno.AssertWarning; 24 | 25 | /** 26 | * @author Tagir Valeev 27 | * 28 | */ 29 | public class TestCheckReturnValue { 30 | @AssertWarning("ReturnValueOfRead") 31 | public void read(InputStream is, byte[] arr) throws IOException { 32 | is.read(arr); 33 | } 34 | 35 | @AssertWarning("ReturnValueOfSkip") 36 | public void skipTen(Reader r) throws IOException { 37 | r.skip(10); 38 | } 39 | 40 | @AssertNoWarning("ReturnValueOfSkip") 41 | public void skipTenOk(Reader r) throws IOException { 42 | long skip = 10; 43 | while(skip > 0) { 44 | skip -= r.skip(skip); 45 | } 46 | } 47 | 48 | @AssertNoWarning("*") 49 | public void readOk(InputStream is, byte[] arr) throws IOException { 50 | if(is.read(arr) != arr.length) { 51 | throw new IOException("Not fuly read"); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestCloneContract.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestCloneContract { 26 | @AssertWarning("CloneableDoesNotImplementClone") 27 | public class Clone0 implements Cloneable { 28 | // empty 29 | } 30 | 31 | public class Clone1 implements Cloneable { 32 | public int f = 0; 33 | 34 | @Override 35 | @AssertWarning(value="CloneableNoSuperCall", minScore=45) 36 | protected Object clone() throws CloneNotSupportedException { 37 | return new Clone1(); 38 | } 39 | } 40 | 41 | public class Clone2 extends Clone1 { 42 | @Override 43 | @AssertWarning(value="CloneableNoSuperCall", minScore=35, maxScore=44) 44 | protected Object clone() throws CloneNotSupportedException { 45 | return new Clone2(); 46 | } 47 | } 48 | 49 | @Override 50 | @AssertWarning("NotCloneableHasClone") 51 | public TestCloneContract clone() { 52 | return new TestCloneContract(); 53 | } 54 | 55 | public class NotCloneable { 56 | @Override 57 | @AssertNoWarning("*") 58 | public Object clone() { 59 | throw new UnsupportedOperationException(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestCompareContract.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Comparator; 19 | 20 | import one.util.huntbugs.registry.anno.AssertWarning; 21 | 22 | /** 23 | * @author shustkost 24 | * 25 | */ 26 | public class TestCompareContract implements Comparable{ 27 | 28 | @Override 29 | @AssertWarning("CompareReturnsMinValue") 30 | public int compareTo(TestCompareContract o) { 31 | return o == this ? 0 : Integer.MIN_VALUE; 32 | } 33 | 34 | Comparator CMP = new Comparator() { 35 | @Override 36 | @AssertWarning("CompareReturnsMinValue") 37 | public int compare(String o1, String o2) { 38 | if(o1.isEmpty()) 39 | return Integer.MIN_VALUE; 40 | if(o2.isEmpty()) 41 | return Integer.MAX_VALUE; 42 | return o1.compareTo(o2); 43 | } 44 | }; 45 | } 46 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestDroppedExceptionObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestDroppedExceptionObject { 26 | @AssertWarning("DroppedException") 27 | public void test(String s) { 28 | if(s.isEmpty()) 29 | new IllegalArgumentException(s); 30 | System.out.println(s); 31 | } 32 | 33 | @AssertNoWarning("DroppedException") 34 | public void testOk(String s) { 35 | if(s.isEmpty()) 36 | throw new IllegalArgumentException(s); 37 | System.out.println(s); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestDubiousCatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.ConcurrentModificationException; 19 | import java.util.List; 20 | 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class TestDubiousCatch { 28 | Object obj = new Object(); 29 | 30 | @AssertWarning("CatchIllegalMonitorStateException") 31 | public boolean catchIllegalState() { 32 | try { 33 | obj.wait(); 34 | return true; 35 | } catch (InterruptedException | IllegalMonitorStateException e) { 36 | return false; 37 | } 38 | } 39 | 40 | @AssertWarning("CatchConcurrentModificationException") 41 | public int catchCME(List list) { 42 | try { 43 | int i = 0; 44 | for (String s : list) { 45 | if (s.equals("test")) 46 | i++; 47 | } 48 | return i; 49 | } catch (ConcurrentModificationException e) { 50 | return 0; 51 | } 52 | } 53 | 54 | @AssertWarning("CatchConcurrentModificationException") 55 | public void catchCMEVoid(List list) { 56 | try { 57 | for (String s : list) { 58 | if (s.equals("test")) 59 | System.out.println(s); 60 | } 61 | } catch (ConcurrentModificationException e) { 62 | // ignore 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestEasyMockProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import org.easymock.EasyMock; 19 | 20 | import one.util.huntbugs.registry.anno.AssertNoWarning; 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class TestEasyMockProblems { 28 | @AssertWarning("UselessEasyMockCall") 29 | public void testEasyMock() { 30 | EasyMock.replay(); 31 | } 32 | 33 | @AssertWarning("UselessEasyMockCall") 34 | public void testEasyMock2() { 35 | Object[] data = {}; 36 | EasyMock.resetToDefault(data); 37 | } 38 | 39 | @AssertNoWarning("*") 40 | public void testEasyMockOk() { 41 | Object[] data = {""}; 42 | EasyMock.verify(data); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestEmptySync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestEmptySync { 26 | @AssertWarning("EmptySynchronizeBlock") 27 | public void emptySync() { 28 | System.out.println("Before"); 29 | synchronized (this) { 30 | // empty 31 | } 32 | System.out.println("After"); 33 | } 34 | 35 | @AssertNoWarning("EmptySynchronizeBlock") 36 | public void nonEmptySync() { 37 | System.out.println("Before"); 38 | synchronized (this) { 39 | System.out.println("Inside"); 40 | } 41 | System.out.println("After"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestExceptionalExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertWarning; 19 | 20 | /** 21 | * @author lan 22 | * 23 | */ 24 | public class TestExceptionalExpression { 25 | @AssertWarning("ExceptionalExpression") 26 | public void testDivisionByZero() { 27 | int a = 1; 28 | int b = 0; 29 | System.out.println(a/b); 30 | } 31 | 32 | @AssertWarning(value="ExceptionalExpression", maxScore=60) 33 | public void testNFE() { 34 | String s = "test"; 35 | try { 36 | System.out.println(Integer.parseInt(s)); 37 | } 38 | catch(NumberFormatException nfe) { 39 | System.out.println("Well..."); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestFloatNaN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertWarning; 19 | 20 | /** 21 | * @author Tagir Valeev 22 | * 23 | */ 24 | public class TestFloatNaN { 25 | @AssertWarning("UnusedLocalVariable") 26 | void testLambda() { 27 | Runnable r = () -> System.out.println(); 28 | Runnable r2 = System.out::println; 29 | } 30 | 31 | @AssertWarning("FloatCompareToNaN") 32 | void testFloat(float a) { 33 | if(a == Float.NaN) 34 | System.out.println("NaN!"); 35 | } 36 | 37 | @AssertWarning("FloatCompareToNaN") 38 | void testNotFloat(float a) { 39 | if(Float.NaN != a) 40 | System.out.println("NaN!"); 41 | } 42 | 43 | @AssertWarning("FloatCompareToNaN") 44 | void testDouble(double a) { 45 | if(a == Double.NaN) 46 | System.out.println("NaN!"); 47 | } 48 | 49 | @AssertWarning("FloatCompareToNaN") 50 | void testNotDouble(double a) { 51 | if(Double.NaN != a) 52 | System.out.println("NaN!"); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestIgnoredException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestIgnoredException { 26 | @AssertWarning("IgnoredException") 27 | public void test1() { 28 | try { 29 | System.out.println("Test"); 30 | } 31 | catch(Exception ex) { 32 | 33 | } 34 | } 35 | 36 | @AssertNoWarning("IgnoredException") 37 | public Exception testOk() { 38 | try { 39 | System.out.println("Test"); 40 | } 41 | catch(Exception ex) { 42 | return ex; 43 | } 44 | return null; 45 | } 46 | 47 | @AssertWarning("IgnoredException") 48 | public void test2() { 49 | try { 50 | System.out.println("Test"); 51 | } 52 | catch(Throwable t) { 53 | return; 54 | } 55 | System.out.println("Passed"); 56 | } 57 | 58 | @AssertWarning("IgnoredException") 59 | public void test3() { 60 | for(int i=0; i<10; i++) { 61 | try { 62 | System.out.println("Test"); 63 | } 64 | catch(RuntimeException ex) { 65 | continue; 66 | } 67 | } 68 | } 69 | 70 | @AssertWarning("IgnoredException") 71 | public Object test4() { 72 | try { 73 | System.out.println("Test"); 74 | } 75 | catch(Throwable t) { 76 | return "xyz".trim(); 77 | } 78 | System.out.println("Passed"); 79 | return ""; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestIncorrectVarArg.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Arrays; 19 | import java.util.stream.IntStream; 20 | import one.util.huntbugs.registry.anno.AssertNoWarning; 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class TestIncorrectVarArg { 28 | @AssertWarning("PrimitiveArrayPassedAsVarArg") 29 | public void testAsList(int[] data) { 30 | Arrays.asList(data).forEach(System.out::println); 31 | } 32 | 33 | private void custom(Object... data) { 34 | System.out.println(data.length); 35 | } 36 | 37 | private void noVarArg(Object[] data) { 38 | System.out.println(data.length); 39 | } 40 | 41 | @AssertWarning("PrimitiveArrayPassedAsVarArg") 42 | public void testCustom(int[] data) { 43 | custom(data); 44 | } 45 | 46 | @AssertNoWarning("PrimitiveArrayPassedAsVarArg") 47 | public void testOk(int[] data) { 48 | noVarArg(new Object[] {data}); 49 | } 50 | 51 | @AssertNoWarning("*") 52 | public void testIntStream(int[] data) { 53 | IntStream.of(data).forEach(System.out::println); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestInitializerRefersSubclass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | @AssertWarning("InitializerRefersSubclass") 26 | public class TestInitializerRefersSubclass { 27 | static final TestInitializerRefersSubclass instance = new SubClass(); 28 | 29 | public static class SubClass extends TestInitializerRefersSubclass { 30 | 31 | } 32 | 33 | @AssertNoWarning("InitializerRefersSubclass") 34 | static class AnonymousOk { 35 | static final AnonymousOk instance = new AnonymousOk() {}; 36 | } 37 | 38 | @AssertWarning("InitializerRefersSubclass") 39 | static class Anonymous { 40 | static final AnonymousSubClass instance = new AnonymousSubClass() {}; 41 | } 42 | 43 | static class AnonymousSubClass extends Anonymous { 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestInternationalization.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.OutputStream; 19 | import java.io.PrintWriter; 20 | import java.nio.charset.StandardCharsets; 21 | import java.util.Locale; 22 | 23 | import one.util.huntbugs.registry.anno.AssertNoWarning; 24 | import one.util.huntbugs.registry.anno.AssertWarning; 25 | 26 | /** 27 | * @author Tagir Valeev 28 | * 29 | */ 30 | public class TestInternationalization { 31 | @AssertWarning("ConvertCaseWithDefaultLocale") 32 | public String toLowerCase(String s) { 33 | return s.toLowerCase(); 34 | } 35 | 36 | @AssertWarning("ConvertCaseWithDefaultLocale") 37 | public String toUpperCase(String s) { 38 | return s.toUpperCase(); 39 | } 40 | 41 | @AssertNoWarning("*") 42 | public String toLowerCaseOk(String s) { 43 | return s.toLowerCase(Locale.ENGLISH); 44 | } 45 | 46 | @AssertNoWarning("*") 47 | public String toUpperCaseOk(String s) { 48 | return s.toUpperCase(Locale.ENGLISH); 49 | } 50 | 51 | @AssertWarning(value="MethodReliesOnDefaultEncoding", minScore = 40, maxScore = 45) 52 | public byte[] toBytes(String s) { 53 | return s.getBytes(); 54 | } 55 | 56 | @AssertWarning(value="MethodReliesOnDefaultEncoding", minScore = 28, maxScore = 35) 57 | public PrintWriter printWriter(OutputStream os) { 58 | return new PrintWriter(os); 59 | } 60 | 61 | @AssertNoWarning("*") 62 | public PrintWriter printWriter() { 63 | return new PrintWriter(System.out); 64 | } 65 | 66 | @AssertNoWarning("*") 67 | public byte[] toBytesOk(String s) { 68 | return s.getBytes(StandardCharsets.UTF_8); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestIteratorContract.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Arrays; 19 | import java.util.Iterator; 20 | import java.util.NoSuchElementException; 21 | 22 | import one.util.huntbugs.registry.anno.AssertNoWarning; 23 | import one.util.huntbugs.registry.anno.AssertWarning; 24 | 25 | /** 26 | * @author Tagir Valeev 27 | * 28 | */ 29 | public class TestIteratorContract implements Iterator { 30 | Iterator input = Arrays.asList("foo", "bar").iterator(); 31 | String nextElement; 32 | 33 | @Override 34 | @AssertWarning("IteratorHasNextCallsNext") 35 | public boolean hasNext() { 36 | try { 37 | nextElement = next(); 38 | } catch (NoSuchElementException e) { 39 | return false; 40 | } 41 | return true; 42 | } 43 | 44 | @Override 45 | @AssertNoWarning("*") 46 | public String next() { 47 | return nextElement == null ? input.next() : nextElement; 48 | } 49 | 50 | public class Iterator2 implements Iterator { 51 | int state = 0; 52 | 53 | @Override 54 | public boolean hasNext() { 55 | return state == 0; 56 | } 57 | 58 | @Override 59 | @AssertWarning("IteratorNoThrow") 60 | public String next() { 61 | return ++state == 1 ? "Hello!".toLowerCase() : null; 62 | } 63 | 64 | } 65 | 66 | @AssertNoWarning("*") 67 | public class NonIterator { 68 | public boolean hasNext() { 69 | return next() == null; 70 | } 71 | 72 | public String next() { 73 | return null; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestJcipProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | import net.jcip.annotations.Immutable; 21 | 22 | /** 23 | * @author lan 24 | * 25 | */ 26 | @Immutable 27 | public class TestJcipProblems { 28 | @AssertWarning("NonFinalFieldInImmutableClass") 29 | private int a; 30 | 31 | @AssertNoWarning("NonFinalFieldInImmutableClass") 32 | private transient int b; 33 | 34 | @AssertNoWarning("NonFinalFieldInImmutableClass") 35 | private volatile int c; 36 | 37 | @AssertNoWarning("NonFinalFieldInImmutableClass") 38 | private final int d = 1; 39 | } 40 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestLockProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.concurrent.CountDownLatch; 19 | import java.util.concurrent.locks.Condition; 20 | 21 | import one.util.huntbugs.registry.anno.AssertNoWarning; 22 | import one.util.huntbugs.registry.anno.AssertWarning; 23 | 24 | /** 25 | * @author Tagir Valeev 26 | * 27 | */ 28 | public class TestLockProblems { 29 | @AssertWarning("IncorrectConcurrentMethod") 30 | public void waitForCondition(Condition cond) throws InterruptedException { 31 | cond.wait(1); 32 | } 33 | 34 | @AssertWarning("IncorrectConcurrentMethod") 35 | public void notifyCondition(Condition cond) { 36 | cond.notify(); 37 | } 38 | 39 | @AssertNoWarning("IncorrectConcurrentMethod") 40 | public void waitForObject(Object cond) throws InterruptedException { 41 | cond.wait(); 42 | } 43 | 44 | @AssertNoWarning("*") 45 | public void waitForConditionOk(Condition cond) throws InterruptedException { 46 | cond.await(); 47 | } 48 | 49 | @AssertWarning("IncorrectConcurrentMethod") 50 | public void notifyCountDown(CountDownLatch latch) { 51 | latch.notifyAll(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestMutableServletField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.IOException; 19 | 20 | import javax.servlet.GenericServlet; 21 | import javax.servlet.ServletException; 22 | import javax.servlet.ServletRequest; 23 | import javax.servlet.ServletResponse; 24 | 25 | import one.util.huntbugs.registry.anno.AssertNoWarning; 26 | import one.util.huntbugs.registry.anno.AssertWarning; 27 | 28 | /** 29 | * @author lan 30 | * 31 | */ 32 | public class TestMutableServletField { 33 | 34 | @AssertNoWarning("*") 35 | public static class TestServletOk extends GenericServlet { 36 | private static final long serialVersionUID = 1L; 37 | 38 | long data; 39 | 40 | public TestServletOk() { 41 | data = System.currentTimeMillis(); 42 | } 43 | 44 | public long getData() { 45 | return data; 46 | } 47 | 48 | @Override 49 | public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 50 | } 51 | } 52 | 53 | public static class TestServlet extends GenericServlet { 54 | private static final long serialVersionUID = 1L; 55 | 56 | @AssertWarning("MutableServletField") 57 | long data; 58 | 59 | public long getData() { 60 | return data; 61 | } 62 | 63 | @Override 64 | public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 65 | data = System.currentTimeMillis(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestNewGetClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestNewGetClass { 26 | @AssertWarning("NewForGetClass") 27 | public void test() { 28 | System.out.println(new TestNewGetClass().getClass()); 29 | } 30 | 31 | @AssertNoWarning("NewForGetClass") 32 | public void testOk() { 33 | TestNewGetClass x; 34 | System.out.println((x = new TestNewGetClass()).getClass()); 35 | System.out.println(x); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestNoRuntimeRetention.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | import java.lang.reflect.Method; 23 | 24 | import one.util.huntbugs.registry.anno.AssertNoWarning; 25 | import one.util.huntbugs.registry.anno.AssertWarning; 26 | 27 | /** 28 | * @author Tagir Valeev 29 | * 30 | */ 31 | public class TestNoRuntimeRetention { 32 | @Target({ElementType.TYPE, ElementType.METHOD}) 33 | public @interface MyAnno {} 34 | 35 | @Target({ElementType.TYPE, ElementType.METHOD}) 36 | @Retention(RetentionPolicy.RUNTIME) 37 | public @interface MyAnnoRuntime {} 38 | 39 | @AssertWarning("AnnotationNoRuntimeRetention") 40 | public boolean testNoRuntimeRetention(Object obj) { 41 | return obj.getClass().isAnnotationPresent(MyAnno.class); 42 | } 43 | 44 | @AssertWarning("AnnotationNoRuntimeRetention") 45 | public boolean testNoRuntimeRetention(Method m) { 46 | return m.getDeclaredAnnotation(MyAnno.class) != null; 47 | } 48 | 49 | @AssertNoWarning("*") 50 | public boolean testRuntimeRetention(Method m) { 51 | return m.getDeclaredAnnotation(MyAnnoRuntime.class) != null; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestNumberConstructor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestNumberConstructor { 26 | @AssertWarning(value="NumberConstructor", minScore = 42) 27 | public Integer testInteger() { 28 | return new Integer(123); 29 | } 30 | 31 | @AssertWarning(value="NumberConstructor", minScore = 20, maxScore = 35) 32 | public Integer testInteger2() { 33 | return new Integer(130); 34 | } 35 | 36 | @AssertWarning(value="NumberConstructor", maxScore = 15) 37 | public Long testLong() { 38 | return new Long(130); 39 | } 40 | 41 | @AssertWarning(value="NumberConstructor", maxScore = 42) 42 | public Integer testInteger3(int x) { 43 | return new Integer(x); 44 | } 45 | 46 | @AssertWarning(value="NumberConstructor", maxScore = 42) 47 | public Character testChar(char x) { 48 | return new Character(x); 49 | } 50 | 51 | @AssertWarning(value="BooleanConstructor", minScore = 42, maxScore=55) 52 | public Boolean testBoolean(boolean x) { 53 | return new Boolean(x); 54 | } 55 | 56 | @AssertNoWarning("NumberConstructor") 57 | public Float testFloat(float x) { 58 | return new Float(x); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestRedundantInterfaces.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.Serializable; 19 | import java.util.Collection; 20 | import java.util.LinkedList; 21 | import java.util.RandomAccess; 22 | import java.util.Set; 23 | import java.util.TreeSet; 24 | 25 | import one.util.huntbugs.registry.anno.AssertNoWarning; 26 | import one.util.huntbugs.registry.anno.AssertWarning; 27 | 28 | /** 29 | * @author Tagir Valeev 30 | * 31 | */ 32 | @AssertWarning("RedundantInterface") 33 | public class TestRedundantInterfaces extends TreeSet implements Set, Collection { 34 | private static final long serialVersionUID = 1L; 35 | 36 | @AssertNoWarning("RedundantInterface") 37 | public static class NoRedundantInterfaces extends LinkedList implements RandomAccess, Serializable { 38 | private static final long serialVersionUID = 1L; 39 | } 40 | 41 | @AssertNoWarning("RedundantInterface") 42 | public static class NoRedundantInterfaces2 extends NoRedundantInterfaces implements Serializable { 43 | private static final long serialVersionUID = 1L; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestRegexProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.File; 19 | import java.nio.file.FileSystems; 20 | 21 | import one.util.huntbugs.registry.anno.AssertNoWarning; 22 | import one.util.huntbugs.registry.anno.AssertWarning; 23 | 24 | /** 25 | * @author Tagir Valeev 26 | * 27 | */ 28 | public class TestRegexProblems { 29 | @AssertWarning("RegexBadSyntax") 30 | public void testBadRegex(String test) { 31 | if(test.matches("***")) { 32 | System.out.println("Matches"); 33 | } 34 | } 35 | 36 | @AssertWarning("RegexUnintended") 37 | public void testPipe(String test) { 38 | for(String part : test.split("|")) { 39 | System.out.println(part); 40 | } 41 | } 42 | 43 | @AssertWarning("RegexUnintended") 44 | public void testDot(String test) { 45 | for(String part : test.split(".")) { 46 | System.out.println(part); 47 | } 48 | } 49 | 50 | @AssertNoWarning("RegexUnintended") 51 | public String testDotReplace(String test) { 52 | return test.replaceAll(".", " "); 53 | } 54 | 55 | @AssertWarning("RegexUnintended") 56 | public String testDotReplaceFirst(String test) { 57 | return test.replaceFirst(".", " "); 58 | } 59 | 60 | @AssertWarning("RegexFileSeparator") 61 | public String[] testFileSeparator(String test) { 62 | return test.split(File.separator); 63 | } 64 | 65 | @AssertWarning("RegexFileSeparator") 66 | public String[] testFileSeparator2(String test) { 67 | return test.split(FileSystems.getDefault().getSeparator()); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestReturnNull.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Optional; 19 | import java.util.function.Function; 20 | 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class TestReturnNull { 28 | @AssertWarning("OptionalReturnNull") 29 | Optional testOptional(int a) { 30 | Optional x = null; 31 | if(a > 5) 32 | return Optional.of(String.valueOf(a)); 33 | return x; 34 | } 35 | 36 | @AssertWarning("BooleanReturnNull") 37 | Boolean testBoolean(int a) { 38 | if(a > 5) 39 | return true; 40 | return null; 41 | } 42 | 43 | @AssertWarning(value="ArrayReturnNull", minScore = 35) 44 | public int[] testArray(int a) { 45 | if(a > 5) 46 | return new int[] {a}; 47 | return null; 48 | } 49 | 50 | @AssertWarning(value="ArrayReturnNull", maxScore = 30) 51 | public int[] testArrayNull() { 52 | return null; 53 | } 54 | 55 | @AssertWarning("OptionalReturnNull") 56 | String testInLambda(String x) { 57 | Function> s = y -> null; 58 | return s.apply(x).get(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSameIfChain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestSameIfChain { 26 | int f = Math.random() > 0.5 ? 3 : 2; 27 | 28 | @AssertWarning("SameConditionChain") 29 | public void testSimple(int x) { 30 | if(x > 0) { 31 | System.out.println(1); 32 | } 33 | if(0 < x) { 34 | System.out.println(2); 35 | } 36 | } 37 | 38 | @AssertWarning("SameConditionChain") 39 | public void testField(int x) { 40 | if(x > f) { 41 | System.out.println(1); 42 | } 43 | if(f < x) { 44 | System.out.println(2); 45 | } 46 | } 47 | 48 | @AssertNoWarning("*") 49 | public void testOk(int x) { 50 | if(x > 0) { 51 | x--; 52 | } 53 | if(x > 0) { 54 | System.out.println(2); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSpinLoop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestSpinLoop { 26 | boolean flag; 27 | 28 | volatile boolean vflag; 29 | 30 | @AssertWarning("SpinLoopOnField") 31 | void waitForTrue() { 32 | while (flag) 33 | ; 34 | } 35 | 36 | @AssertNoWarning("SpinLoopOnField") 37 | void waitForVolatileTrue() { 38 | while (vflag) 39 | ; 40 | } 41 | 42 | TestSpinLoop foo; 43 | 44 | TestSpinLoop bar; 45 | 46 | @AssertWarning("SpinLoopOnField") 47 | void waitForNonNull() { 48 | while (foo == null) 49 | ; 50 | } 51 | 52 | @AssertWarning("SpinLoopOnField") 53 | static void waitForNonNullIndirect(int x, TestSpinLoop baz) { 54 | while (baz.foo == null) 55 | ; 56 | } 57 | 58 | @AssertWarning("SpinLoopOnField") 59 | static void waitForNonNullIndirect2(int x, TestSpinLoop baz) { 60 | while (baz.foo.bar == null) 61 | ; 62 | } 63 | 64 | static boolean sflag; 65 | 66 | @AssertWarning("SpinLoopOnField") 67 | static void waitForStatic() { 68 | while (!sflag) 69 | ; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSqlBadArgument.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.sql.PreparedStatement; 19 | import java.sql.ResultSet; 20 | import java.sql.SQLException; 21 | 22 | import one.util.huntbugs.registry.anno.AssertNoWarning; 23 | import one.util.huntbugs.registry.anno.AssertWarning; 24 | 25 | /** 26 | * @author lan 27 | * 28 | */ 29 | public class TestSqlBadArgument { 30 | @AssertWarning(value="BadResultSetArgument", minScore=70) 31 | public String get(ResultSet rs) throws SQLException { 32 | return rs.getString(0); 33 | } 34 | 35 | @AssertWarning(value="BadResultSetArgument", maxScore=60) 36 | public String getConditional(ResultSet rs, boolean b) throws SQLException { 37 | int pos = b ? 2 : 0; 38 | return rs.getString(pos); 39 | } 40 | 41 | @AssertWarning("BadPreparedStatementArgument") 42 | public void set(PreparedStatement ps) throws SQLException { 43 | ps.setInt(0, 10); 44 | } 45 | 46 | @AssertNoWarning("*") 47 | public void setOk(PreparedStatement ps) throws SQLException { 48 | ps.setInt(10, 0); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestStartInConstructor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestStartInConstructor { 26 | @AssertWarning(value="StartInConstructor", minScore=45, maxScore=55) 27 | public TestStartInConstructor() { 28 | new Thread(() -> System.out.println()).start(); 29 | System.out.println("Started!"); 30 | } 31 | 32 | @AssertWarning(value="StartInConstructor", minScore=45, maxScore=55) 33 | public TestStartInConstructor(String s, int x) { 34 | new Thread() { 35 | @Override 36 | public void run() { 37 | System.out.println("Thread!"); 38 | } 39 | }.start(); 40 | System.out.println("Started!"); 41 | } 42 | 43 | @AssertWarning(value="StartInConstructor", minScore=35, maxScore=45) 44 | public TestStartInConstructor(int x) { 45 | new Thread(() -> System.out.println()).start(); 46 | } 47 | 48 | @AssertNoWarning("StartInConstructor") 49 | private TestStartInConstructor(String s) { 50 | new Thread(() -> System.out.println()).start(); 51 | } 52 | 53 | public class SubClass extends TestStartInConstructor { 54 | @AssertWarning(value="StartInConstructor", minScore=25, maxScore=35) 55 | public SubClass(int x) { 56 | new Thread(() -> System.out.println()).start(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestStaticFieldNonThreadSafe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.text.DateFormat; 19 | import java.text.SimpleDateFormat; 20 | import java.util.Calendar; 21 | import java.util.Date; 22 | 23 | import one.util.huntbugs.registry.anno.AssertNoWarning; 24 | import one.util.huntbugs.registry.anno.AssertWarning; 25 | 26 | /** 27 | * @author shustkost 28 | * 29 | */ 30 | public class TestStaticFieldNonThreadSafe { 31 | @AssertWarning("StaticNotThreadSafeField") 32 | public static final Calendar CALENDAR = Calendar.getInstance(); 33 | 34 | @AssertWarning(value="StaticNotThreadSafeField", maxScore=55) 35 | protected static final SimpleDateFormat SDF = new SimpleDateFormat("dd"); 36 | 37 | @AssertNoWarning("*") 38 | private static final SimpleDateFormat privateSDF = new SimpleDateFormat("dd"); 39 | 40 | @AssertNoWarning("*") 41 | private static final String date = privateSDF.format(new Date()); 42 | 43 | private static final DateFormat usedSDF = new SimpleDateFormat("dd"); 44 | 45 | static { 46 | System.out.println(date); 47 | } 48 | 49 | @AssertWarning("StaticNotThreadSafeFieldInvoke") 50 | public String format(Date date) { 51 | return usedSDF.format(date); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestStringUsage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestStringUsage { 26 | 27 | @AssertWarning(value="StringConstructor", maxScore=35) 28 | public static final String STRING = new String("abc"); 29 | 30 | @AssertWarning("StringConstructorEmpty") 31 | public String testStringCtor() { 32 | return new String(); 33 | } 34 | 35 | @AssertWarning("StringConstructor") 36 | public String testStringCtor2() { 37 | return new String("test"); 38 | } 39 | 40 | @AssertNoWarning("*") 41 | public String testStringCtor3() { 42 | return new String(new char[] {'c'}); 43 | } 44 | 45 | @AssertWarning("StringToString") 46 | public String testStringToString(String s) { 47 | return s.toString(); 48 | } 49 | 50 | @AssertNoWarning("*") 51 | public String testObjectToString(Object s) { 52 | if(s instanceof String) 53 | return s.toString(); 54 | return null; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSwingProblems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import javax.swing.JPanel; 19 | 20 | import one.util.huntbugs.registry.anno.AssertNoWarning; 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author Tagir Valeev 25 | * 26 | */ 27 | public class TestSwingProblems { 28 | @AssertNoWarning("SwingMethodNotInSwingThread") 29 | private static void run() { 30 | JPanel panel = new JPanel(); 31 | panel.setVisible(true); 32 | } 33 | 34 | @AssertNoWarning("SwingMethodNotInSwingThread") 35 | public static void main(String[] args, boolean x) { 36 | JPanel panel = new JPanel(); 37 | panel.setVisible(x && args.length > 0); 38 | } 39 | 40 | @AssertWarning("SwingMethodNotInSwingThread") 41 | public static void main(String[] args) { 42 | JPanel panel = new JPanel(); 43 | panel.setVisible(true); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSyncGetClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author Tagir Valeev 23 | * 24 | */ 25 | public class TestSyncGetClass { 26 | private static int val; 27 | 28 | @AssertWarning(value="SyncOnGetClass", minScore=50) 29 | public void update(int x) { 30 | synchronized (getClass()) { 31 | val = x; 32 | System.out.println(val); 33 | } 34 | } 35 | 36 | @AssertNoWarning("*") 37 | public static void updateStatic(TestSyncGetClass obj, int x) { 38 | synchronized (obj.getClass()) { 39 | val = x; 40 | System.out.println(val); 41 | } 42 | } 43 | 44 | @AssertNoWarning("SyncOnGetClass") 45 | @AssertWarning("StaticFieldFromInstanceMethod") 46 | public void update(Object obj, int x) { 47 | synchronized (obj.getClass()) { 48 | val = x; 49 | System.out.println(val); 50 | } 51 | } 52 | 53 | @AssertWarning(value="SyncOnGetClass", minScore=40, maxScore=49) 54 | public void print(int x) { 55 | synchronized (getClass()) { 56 | System.out.println(x); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestSyncOnUpdatedField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestSyncOnUpdatedField { 26 | static Object globalLock = new Object(); 27 | 28 | Object lock = new Object(); 29 | 30 | @AssertWarning("SynchronizationOnUpdatedField") 31 | public void testSimpleSync() { 32 | synchronized (lock) { 33 | System.out.println("In lock"); 34 | lock = new Object(); 35 | } 36 | } 37 | 38 | @AssertWarning("SynchronizationOnUpdatedField") 39 | public void testSimpleStatic() { 40 | synchronized (globalLock) { 41 | System.out.println("In lock"); 42 | globalLock = new Object(); 43 | } 44 | } 45 | 46 | @AssertWarning("SynchronizationOnUpdatedField") 47 | public void testOtherSync(TestSyncOnUpdatedField tsouf) { 48 | synchronized (tsouf.lock) { 49 | System.out.println("In lock"); 50 | tsouf.lock = new Object(); 51 | } 52 | } 53 | 54 | @AssertNoWarning("*") 55 | public void testOtherSyncOk(TestSyncOnUpdatedField tsouf) { 56 | synchronized (tsouf.lock) { 57 | System.out.println("In lock"); 58 | lock = new Object(); 59 | } 60 | } 61 | 62 | @AssertNoWarning("*") 63 | public void testOtherSyncOk2(TestSyncOnUpdatedField tsouf) { 64 | synchronized (tsouf.lock) { 65 | tsouf = this; 66 | System.out.println("In lock"); 67 | tsouf.lock = new Object(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestToArrayDowncast.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.ArrayList; 19 | import java.util.Collection; 20 | 21 | import one.util.huntbugs.registry.anno.AssertNoWarning; 22 | import one.util.huntbugs.registry.anno.AssertWarning; 23 | 24 | /** 25 | * @author Tagir Valeev 26 | * 27 | */ 28 | public class TestToArrayDowncast { 29 | @AssertWarning("ImpossibleToArrayDowncast") 30 | public String[] toArray(ArrayList l) { 31 | return (String[]) l.toArray(); 32 | } 33 | 34 | @AssertWarning("ImpossibleToArrayDowncast") 35 | public String[] toArray(Collection l) { 36 | return (String[]) l.toArray(); 37 | } 38 | 39 | @AssertNoWarning("ImpossibleToArrayDowncast") 40 | public String[] toArrayOk(Collection l) { 41 | return l.toArray(new String[0]); 42 | } 43 | 44 | @SuppressWarnings("unchecked") 45 | // Probably some other warning should be issued here, but not this 46 | @AssertNoWarning("ImpossibleToArrayDowncast") 47 | public T[] toArrayGeneric(Collection l) { 48 | return (T[])l.toArray(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestUnreachableCatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestUnreachableCatch { 26 | @AssertNoWarning("*") 27 | public String testOk(Object x) { 28 | try { 29 | return (String)x; 30 | } 31 | catch(ClassCastException cce) { 32 | cce.printStackTrace(); 33 | return null; 34 | } 35 | } 36 | 37 | @AssertWarning("UnreachableCatch") 38 | public Object testUnreachable(Object x) { 39 | try { 40 | return x; 41 | } 42 | catch(ClassCastException cce) { 43 | cce.printStackTrace(); 44 | return null; 45 | } 46 | } 47 | 48 | @AssertWarning("UnreachableCatch") 49 | public Object testUnreachableMulti(Object x) { 50 | try { 51 | return x; 52 | } 53 | catch(ClassCastException | NullPointerException ex) { 54 | ex.printStackTrace(); 55 | return null; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestUnsafeGetResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.io.InputStream; 19 | import java.net.URL; 20 | 21 | import one.util.huntbugs.registry.anno.AssertNoWarning; 22 | import one.util.huntbugs.registry.anno.AssertWarning; 23 | 24 | /** 25 | * @author Tagir Valeev 26 | * 27 | */ 28 | public class TestUnsafeGetResource { 29 | @AssertWarning(value="UnsafeGetResource", minScore=50) 30 | public URL getURL() { 31 | Class myClass = getClass(); 32 | return myClass.getResource("foo"); 33 | } 34 | 35 | @AssertNoWarning("UnsafeGetResource") 36 | public URL getURLOk() { 37 | return TestUnsafeGetResource.class.getResource("foo"); 38 | } 39 | 40 | @AssertWarning(value="UnsafeGetResource", minScore=50) 41 | public InputStream openResource() { 42 | return getClass().getResourceAsStream("foo"); 43 | } 44 | 45 | @AssertWarning(value="UnsafeGetResource", maxScore=35) 46 | public InputStream openResourceFromRoot() { 47 | return getClass().getResourceAsStream("/foo"); 48 | } 49 | 50 | public static class NoSubClasses { 51 | @AssertWarning(value="UnsafeGetResource", minScore=40, maxScore=40) 52 | public URL getURL() { 53 | return getClass().getResource("foo"); 54 | } 55 | } 56 | 57 | public static final class SubClass extends TestUnsafeGetResource { 58 | @AssertNoWarning("UnsafeGetResource") 59 | public URL getURL2() { 60 | return getClass().getResource("foo"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestUnsupportedCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Set; 19 | 20 | import one.util.huntbugs.registry.anno.AssertNoWarning; 21 | import one.util.huntbugs.registry.anno.AssertWarning; 22 | 23 | /** 24 | * @author lan 25 | * 26 | */ 27 | public class TestUnsupportedCall { 28 | public TestUnsupportedCall() { 29 | throw Math.random() > 0.5 ? new UnsupportedOperationException() : new UnsupportedOperationException("With message"); 30 | } 31 | 32 | public TestUnsupportedCall(int x) { 33 | System.out.println(x); 34 | } 35 | 36 | @AssertWarning("UnsupportedCall") 37 | public void hello(int x) { 38 | doHello(x+1); 39 | } 40 | 41 | @AssertNoWarning("*") 42 | private void doHello(int i) { 43 | throw new UnsupportedOperationException(); 44 | } 45 | 46 | @AssertNoWarning("*") 47 | public void addAll(Set a, Set b) { 48 | a.addAll(b); 49 | } 50 | 51 | @AssertWarning("UnsupportedCall") 52 | public static void test() { 53 | new TestUnsupportedCall(); 54 | } 55 | 56 | @AssertNoWarning("UnsupportedCall") 57 | public static void testCatch() { 58 | try { 59 | new TestUnsupportedCall(); 60 | } catch (UnsupportedOperationException e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestUselessVoidMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestUselessVoidMethod { 26 | @AssertWarning("UselessVoidMethod") 27 | public void doNothing(int x) { 28 | int y = x; 29 | if (x > 0) { 30 | y = y * 2; 31 | } 32 | } 33 | 34 | @AssertWarning("UselessVoidMethod") 35 | public void uselessSwitch(int x) { 36 | switch(x) { 37 | case 1: 38 | break; 39 | } 40 | } 41 | 42 | boolean b; 43 | volatile boolean v; 44 | 45 | @AssertWarning("UselessVoidMethod") 46 | public void uselessSpinLoop() { 47 | while(b) {} 48 | } 49 | 50 | @AssertNoWarning("UselessVoidMethod") 51 | public void volatileSpinLoop() { 52 | while(v) {} 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestValuesFlow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import java.util.Hashtable; 19 | import java.util.Map; 20 | import java.util.Map.Entry; 21 | 22 | import one.util.huntbugs.registry.anno.AssertNoWarning; 23 | 24 | /** 25 | * @author Tagir Valeev 26 | * 27 | */ 28 | public class TestValuesFlow { 29 | @AssertNoWarning("*") 30 | void testForSwitch(Map mod) { 31 | Map mapTemp = new Hashtable<>(); 32 | for (Entry e : mod.entrySet()) { 33 | Character key = e.getKey(); 34 | switch (key) { 35 | case 'B': 36 | // we will phase these at the time of rotation, in setModRot 37 | break; 38 | case 'C': 39 | // not implemented 40 | continue; 41 | } 42 | mapTemp.put(key, e.getValue()); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/TestVolatileArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | /** 22 | * @author lan 23 | * 24 | */ 25 | public class TestVolatileArray { 26 | @AssertWarning("FieldIsAlwaysNull") 27 | @AssertNoWarning("VolatileArray") 28 | public volatile Object[] a = null; 29 | 30 | @AssertWarning(value = "VolatileArray", minScore = 50) 31 | public volatile int[] b = { 1, 2, 3 }; 32 | 33 | @AssertWarning(value = "VolatileArray", maxScore = 45) 34 | public volatile long[] c = { 1, 2, 3 }; 35 | 36 | public static class AnotherClass { 37 | public void set(TestVolatileArray tva, long[] arr) { 38 | tva.c = arr; 39 | if(tva.a != null) { 40 | System.out.println(tva.a); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/testdata/sub/SubFieldAccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.testdata.sub; 17 | 18 | import one.util.huntbugs.testdata.TestFieldAccess; 19 | 20 | /** 21 | * @author lan 22 | * 23 | */ 24 | public class SubFieldAccess { 25 | int val = TestFieldAccess.FieldInterface.usedArr[1]; 26 | 27 | Object val2 = TestFieldAccess.usedEverywhere; 28 | 29 | static { 30 | System.out.println(TestFieldAccess.FieldInterface.emptyStrings); 31 | System.out.println(TestFieldAccess.FieldInterface.strings); 32 | System.out.println(TestFieldAccess.FieldInterface.stringsList); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /huntbugs/src/test/java/one/util/huntbugs/util/TestIterables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.util; 17 | 18 | import static org.junit.Assert.*; 19 | 20 | import java.util.Arrays; 21 | 22 | import org.junit.Test; 23 | 24 | /** 25 | * @author lan 26 | * 27 | */ 28 | public class TestIterables { 29 | @Test 30 | public void testConcat() { 31 | assertEquals(Arrays.asList(1, 2, 3), Iterables 32 | .toList(Iterables.concat(Arrays.asList(1, 2, 3), Arrays.asList()))); 33 | assertEquals(Arrays.asList(1, 2, 3), Iterables 34 | .toList(Iterables.concat(Arrays.asList(), Arrays.asList(1, 2, 3)))); 35 | assertEquals(Arrays.asList(), Iterables.toList(Iterables.concat(Arrays.asList(), Arrays.asList()))); 36 | assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), Iterables.toList(Iterables.concat(Arrays.asList(1, 2, 3), Arrays 37 | .asList(4, 5, 6)))); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | 5 | one.util 6 | huntbugs-all 7 | 0.0.12-SNAPSHOT 8 | 9 | sample-huntbugs-custom-detector 10 | jar 11 | 12 | sample-huntbugs-custom-detector 13 | Demonstration of how HuntBugs custom detectors could be implemented 14 | 15 | 16 | 17 | one.util 18 | huntbugs 19 | ${project.version} 20 | provided 21 | 22 | 23 | junit 24 | junit 25 | 4.12 26 | test 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/main/java/one/util/huntbugs/sample/SampleHuntBugsPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample; 17 | 18 | import one.util.huntbugs.spi.HuntBugsPlugin; 19 | 20 | /** 21 | * @author Mihails Volkovs 22 | */ 23 | public class SampleHuntBugsPlugin implements HuntBugsPlugin { 24 | 25 | @Override 26 | public String name() { 27 | return "Sample Detectors"; 28 | } 29 | 30 | @Override 31 | public String detectorPackage() { 32 | return "one.util.huntbugs.sample.detect"; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/main/java/one/util/huntbugs/sample/detect/SampleCustomDetector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample.detect; 17 | 18 | import com.strobel.assembler.metadata.MethodDefinition; 19 | import com.strobel.assembler.metadata.TypeDefinition; 20 | import com.strobel.assembler.metadata.TypeReference; 21 | import one.util.huntbugs.registry.MethodContext; 22 | import one.util.huntbugs.registry.anno.MethodVisitor; 23 | import one.util.huntbugs.registry.anno.WarningDefinition; 24 | import one.util.huntbugs.util.Types; 25 | 26 | /** 27 | * @author Mihails Volkovs 28 | */ 29 | @WarningDefinition(category="Demo", name="SampleCustomDetector", maxScore=80) 30 | public class SampleCustomDetector { 31 | 32 | @MethodVisitor 33 | public void visit(MethodContext mc, MethodDefinition md, TypeDefinition td) { 34 | TypeReference returnType = md.getReturnType(); 35 | if (Types.isCollection(returnType)) { 36 | mc.report("SampleCustomDetector", 5); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/main/resources/META-INF/services/one.util.huntbugs.spi.HuntBugsPlugin: -------------------------------------------------------------------------------- 1 | one.util.huntbugs.sample.SampleHuntBugsPlugin 2 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/main/resources/huntbugs/messages.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | For some reason you can't return instance of java.util.Collection 9 | For some reason you can't return instance of java.util.Collection 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/test/java/one/util/huntbugs/sample/DataTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample; 17 | 18 | import one.util.huntbugs.spi.DataTests; 19 | import org.junit.Test; 20 | 21 | /** 22 | * @author Mihails Volkovs 23 | */ 24 | public class DataTest { 25 | 26 | @Test 27 | public void test() throws Exception { 28 | DataTests.test("one/util/huntbugs/sample/testdata"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /sample-huntbugs-custom-detector/src/test/java/one/util/huntbugs/sample/testdata/TestSampleCustomDetector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample.testdata; 17 | 18 | import one.util.huntbugs.registry.anno.AssertNoWarning; 19 | import one.util.huntbugs.registry.anno.AssertWarning; 20 | 21 | import java.util.ArrayList; 22 | import java.util.Collection; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Mihails Volkovs 28 | */ 29 | public class TestSampleCustomDetector { 30 | 31 | private static final String DEMO_CUSTOM_DETECTOR = "SampleCustomDetector"; 32 | 33 | @AssertWarning(DEMO_CUSTOM_DETECTOR) 34 | public Collection getCollection() { 35 | return new ArrayList(); 36 | } 37 | 38 | @AssertNoWarning(DEMO_CUSTOM_DETECTOR) 39 | public Map getMap() { 40 | return new HashMap(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /sample-project/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /sample-project/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | 5 | one.util 6 | huntbugs-all 7 | 0.0.12-SNAPSHOT 8 | 9 | sample-project 10 | jar 11 | 12 | sample-project 13 | Sample project to demonstrate HuntBugs configuration and features 14 | 15 | 16 | 17 | junit 18 | junit 19 | 4.12 20 | test 21 | 22 | 23 | 24 | 25 | 26 | 27 | one.util 28 | huntbugs-maven-plugin 29 | ${project.version} 30 | 31 | 32 | one.util 33 | sample-huntbugs-custom-detector 34 | ${project.version} 35 | 36 | 37 | 38 | 39 | huntbugs-check 40 | prepare-package 41 | 42 | huntbugs 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /sample-project/src/main/java/one/util/huntbugs/sample/MyProductionCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * @author Mihails Volkovs 23 | */ 24 | public class MyProductionCode { 25 | 26 | public List bugsAlive() { 27 | return new ArrayList<>(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /sample-project/src/test/java/one/util/huntbugs/sample/MyProductionCodeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 HuntBugs contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.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 one.util.huntbugs.sample; 17 | 18 | import org.junit.Before; 19 | import org.junit.Test; 20 | 21 | import static org.junit.Assert.assertTrue; 22 | 23 | /** 24 | * @author Mihails Volkovs 25 | */ 26 | public class MyProductionCodeTest { 27 | 28 | private MyProductionCode productionCode; 29 | 30 | @Before 31 | public void setUp() { 32 | productionCode = new MyProductionCode(); 33 | } 34 | 35 | @Test 36 | public void bugsAlive() { 37 | assertTrue(productionCode.bugsAlive().isEmpty()); 38 | } 39 | 40 | } --------------------------------------------------------------------------------