JDQuery: Query-Driven Defect Localization for Java Source Code Based on Code Knowledge Graphs
Abstract
1. Introduction
2. Related Works
2.1. Static Defect Detection Methodologies
2.2. Knowledge Graph Applications in Defect Detection
3. Methods
- 1.
- Specification-Guided Java Code Knowledge Graph Construction: Guided by the Java Language Specification, target source files are parsed into ASTs. Code entities, attributes, and relationships are then extracted from the ASTs and mapped onto a predefined ontology layer to build a persistent code knowledge graph.
- 2.
- Query-Driven Java Defect Localization: Java defect patterns are defined based on the structural and semantic characteristics of different defect types and are subsequently transformed into declarative graph queries. By executing these queries over the structured code knowledge graph, JDQuery identifies graph structures that satisfy the defect patterns, thereby enabling query-driven defect localization.
3.1. Java Code Knowledge Graph Construction
3.1.1. Ontology Definition
3.1.2. AST Generation
3.1.3. Instance Layer Construction
- Nodes are mapped to predefined ontology classes to instantiate graph entities.
- Node attributes are mapped to OWL data properties to define entity attributes.
- Syntax and semantic relationships between nodes are mapped to OWL object properties to establish graph relationships.
3.1.4. Persistent Storage
3.2. Query-Driven Defect Localization
3.2.1. Defect Pattern Analysis
| Listing 1. Exemplar code fragment exhibiting Abstract Class Exposing Public Constructors (ACP) defect. |
![]() |
- Code Entity: A graph entity of type ClassOrInterface exists.
- Attribute Constraint: The modifier attribute of the ClassOrInterface entity contains the keyword abstract.
- Relationship Constraint: The ClassOrInterface entity declares one or more ConstructorDeclaration entities whose modifier attribute contains the keyword public.
3.2.2. Query Rule Formulation
| Listing 2. Cypher query implementation for the ACP pattern. |
![]() |
3.2.3. Defect Detection Execution
3.2.4. Defect Localization Results Generation
3.3. Summary
4. Results
4.1. Dataset Setting
4.2. Defect Localization Effectiveness
- P1 (commons-cli): a small-scale project with 11,548 LoC. It contains 350 artificially injected defects and 104 native defects, resulting in 454 defects in total.
- P2 (commons-io): a medium-scale project with 92,490 LoC. It contains 1292 artificially injected defects and 112 native defects, resulting in 1404 defects in total.
4.3. Comparison Experiment
4.4. Efficiency Results
5. Discussion and Future Works
6. Conclusions
Author Contributions
Funding
Data Availability Statement
Conflicts of Interest
Abbreviations
| JDQuery | Java Defect Query |
| AST | Abstract Syntax Tree |
| LoCs | Lines of Code |
| TPs | True Positives |
| TNs | True Negatives |
| FPs | False Positives |
| FNs | False Negatives |
| FNR | False Negative Rate |
References
- Gupta, A.; Suri, B.; Misra, S. A systematic literature review: Code bad smells in java source code. In Proceedings of the International Conference on Computational Science and Its Applications; Springer: Berlin/Heidelberg, Germany, 2017; pp. 665–682. [Google Scholar]
- Huang, Q.; Liao, D.; Xing, Z.; Zuo, Z.; Wang, C.; Xia, X. Semantic-enriched code knowledge graph to reveal unknowns in smart contract code reuse. ACM Trans. Softw. Eng. Methodol. 2023, 32, 1–37. [Google Scholar] [CrossRef] [Scilit]
- Liang, L.; Li, Y.; Wen, M.; Liu, Y. KG4Py: A toolkit for generating Python knowledge graph and code semantic search. Connect. Sci. 2022, 34, 1384–1400. [Google Scholar] [CrossRef] [Scilit]
- Xiao, P.; Zhang, L.; Yan, Y.; Zhang, Z. Static detection method for multi-level network source code vulnerabilities based on knowledge graph technology. Discov. Artif. Intell. 2025, 5, 120. [Google Scholar] [CrossRef] [Scilit]
- Wang, L.; Sun, C.; Zhang, C.; Nie, W.; Huang, K. Application of knowledge graph in software engineering field: A systematic literature review. Inf. Softw. Technol. 2023, 164, 107327. [Google Scholar] [CrossRef] [Scilit]
- Rukmono, S.A.; Chaudron, M.R. Enabling analysis and reasoning on software systems through knowledge graph representation. In Proceedings of the 2023 IEEE/ACM 20th International Conference on Mining Software Repositories (MSR), Melbourne, Australia, 15–16 May 2023; pp. 120–124. [Google Scholar]
- Cornelissen, B.; Zaidman, A.; Van Deursen, A.; Moonen, L.; Koschke, R. A systematic survey of program comprehension through dynamic analysis. IEEE Trans. Softw. Eng. 2009, 35, 684–702. [Google Scholar] [CrossRef] [Scilit]
- Amankwah, R.; Chen, J.; Song, H.; Kudjo, P.K. Bug detection in Java code: An extensive evaluation of static analysis tools using Juliet Test Suites. Softw. Pract. Exp. 2023, 53, 1125–1143. [Google Scholar] [CrossRef] [Scilit]
- Zhang, H.; Pei, Y.; Chen, J.; Tan, S.H. Statfier: Automated testing of static analyzers via semantic-preserving program transformations. In Proceedings of the 31st ACM Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering, San Francisco, CA, USA, 3–9 December 2023; pp. 237–249. [Google Scholar]
- Van Emden, E.; Moonen, L. Java quality assurance by detecting code smells. In Proceedings of the Ninth Working Conference on Reverse Engineering; IEEE: New York, NY, USA, 2002; pp. 97–106. [Google Scholar]
- Malhotra, R.; Singh, P. Deep AST-based approach for software defect prediction: A comparative analysis. In Proceedings of the International Conference on Data Science and Applications, Jaipur, India, 17–19 July 2024; pp. 447–460. [Google Scholar]
- Rutar, N.; Almazan, C.B.; Foster, J.S. A comparison of bug finding tools for java. In Proceedings of the 15th International Symposium on Software Reliability Engineering, Saint-Malo, Bretagne, France, 2–5 November 2004; pp. 245–256. [Google Scholar]
- Saliba, L.; Shioji, E.; Oliveira, E.; Cohney, S.; Qi, J. Learning with style: Improving student code-style through better automated feedback. In Proceedings of the 55th ACM Technical Symposium on Computer Science Education V. 1, Portland, OR, USA, 20–23 March 2024; pp. 1175–1181. [Google Scholar]
- Hovemeyer, D.; Pugh, W. Finding bugs is easy. ACM Sigplan Not. 2004, 39, 92–106. [Google Scholar] [CrossRef] [Scilit]
- Lavazza, L.; Tosi, D.; Morasca, S. An empirical study on the persistence of spotbugs issues in open-source software evolution. In Proceedings of the International Conference on the Quality of Information and Communications Technology, Virtual, 8–11 September 2020; pp. 144–151. [Google Scholar]
- Yu, P.; Wu, Y.; Peng, J.; Zhang, J.; Xie, P. Towards understanding fixes of sonarqube static analysis violations: A large-scale empirical study. In Proceedings of the 2023 IEEE International Conference on Software Analysis, Evolution and Reengineering (SANER); IEEE: New York, NY, USA, 2023; pp. 569–580. [Google Scholar]
- Marcilio, D.; Bonifácio, R.; Monteiro, E.; Canedo, E.; Luz, W.; Pinto, G. Are static analysis violations really fixed? a closer look at realistic usage of sonarqube. In Proceedings of the 2019 IEEE/ACM 27th International Conference on Program Comprehension (ICPC); IEEE: New York, NY, USA, 2019; pp. 209–219. [Google Scholar]
- Imtiaz, N.; Murphy, B.; Williams, L. How do developers act on static analysis alerts? an empirical study of coverity usage. In Proceedings of the 2019 IEEE 30th International Symposium on Software Reliability Engineering (ISSRE); IEEE: New York, NY, USA, 2019; pp. 323–333. [Google Scholar]
- Bacchiani, L.; Bravetti, M.; Giunti, M.; Mota, J.; Ravara, A. A Java typestate checker supporting inheritance. Sci. Comput. Program. 2022, 221, 102844. [Google Scholar] [CrossRef] [Scilit]
- Lenarduzzi, V.; Lomio, F.; Huttunen, H.; Taibi, D. Are sonarqube rules inducing bugs? In Proceedings of the 27th International Conference on Software Analysis, Evolution and Reengineering (SANER), London, ON, Canada, 18–21 February 2020; pp. 501–511. [Google Scholar]
- Bi, Z.; Chen, J.; Jiang, Y.; Xiong, F.; Guo, W.; Chen, H.; Zhang, N. Codekgc: Code language model for generative knowledge graph construction. ACM Trans. Asian Low.-Resour. Lang. Inf. Process. 2024, 23, 1–16. [Google Scholar] [CrossRef] [Scilit]
- Hu, T.; Li, B.; Pan, Z.; Qian, C. Detect defects of solidity smart contract based on the knowledge graph. IEEE Trans. Reliab. 2023, 73, 186–202. [Google Scholar] [CrossRef] [Scilit]
- Li, B.; Hu, T.; Xu, X.; Wang, L. VulFinder: Exploring Chaincode Vulnerabilities More Effectively and Efficiently Using Knowledge Graph Based Defect Pattern Matching. IEEE Trans. Softw. Eng. 2025, 51, 3247–3266. [Google Scholar] [CrossRef] [Scilit]
- Shen, M.; Pillai, A.A.; Yuan, B.A.; Davis, J.C.; Machiry, A. Finding 709 Defects in 258 Projects: An Experience Report on Applying CodeQL to Open-Source Embedded Software (Experience Paper). Proc. ACM Softw. Eng. 2025, 2, 1077–1100. [Google Scholar] [CrossRef] [Scilit]
- Gruber, T.R. Toward principles for the design of ontologies used for knowledge sharing. Int. J. Hum.-Comput. Stud. 1995, 43, 907–928. [Google Scholar] [CrossRef] [Scilit]
- Noy, N.F.; McGuinness, D.L. Ontology Development 101: A Guide to Creating Your First Ontology; Stanford Knowledge Systems Laboratory: Palo Alto, CA, USA, 2001. [Google Scholar]
- Bhat, S.; Bhirud, R.; Bhokare, V. Survey on Various Syntax Analyzer Tools. Int. J. Res. Appl. Sci. Eng. Technol. (IJRA) 2022, 10. [Google Scholar] [CrossRef] [Scilit]
- Gu, B.-B.; Li, Y.; Jiang, T.-T. Parametric Software Defect Patterns Based on Test Perspective. In Proceedings of the 7th International Conference on Dependable Systems and Their Applications (DSA); IEEE: New York, NY, USA, 2020; pp. 364–370. [Google Scholar]
- Saberironaghi, A.; Ren, J.; El-Gindy, M. Defect detection methods for industrial products using deep learning techniques: A review. Algorithms 2023, 16, 95. [Google Scholar] [CrossRef] [Scilit]



| Abbr. | Defect Type | Related Class Types | Data Properties or Object Properties |
|---|---|---|---|
| ACP | Public constructor in an abstract class | ClassOrInterface, Constructor, Modifiers | hasConstructor |
| ACC | Comparison of objects of atomic types | BinaryExpr, Variable | variableName |
| BCS | Non-short-circuit logic in Boolean contexts | BinaryExpr | binaryExprOP |
| CIE | Catching IllegalMonitorStateException | CatchClause, Parameter | hasParameter |
| CIC | Class or interface name not capitalized | ClassOrInterface | className |
| CII | Constant defined in an interface | ClassOrInterface, Field | hasField |
| CNC | Constant name not capitalized | Field, Variable, Modifiers | variableName |
| DRT | Use of raw types without type parameters | ClassOrInterfaceType | hasValue |
| DIV | Division by zero | BinaryExpr | binaryExprRight |
| DIP | Duplicate imports | ImportDeclaration | fileName, hasValue |
| EFB | Empty finally block | TryStmt, BlockStmt | hasFinallyBlock |
| EM | Empty method | ClassOrInterface, Method, BlockStmt | hasBlockStmt |
| ICC | Constant if condition | IfStmt | conditionIs |
| LCF | Field name not following lower camel case | Field, Modifiers | variableName |
| LCV | Variable name not following lower camel case | Variable, Modifiers | variableName |
| MN | Method name not following lower camel case | Method | methodName |
| ML | Calling wait inside multiple synchronized blocks | SynchronizedStmt, MethodCallExpr | methodName |
| NTO | Nested ternary operators | ConditionalExpr | – |
| SA | Self-assignment | AssignExpr | assignValue, assignTarget |
| SBO | Self binary operations | BinaryExpr | binaryExprRight, binaryExprLeft |
| SC | Self-comparison | BinaryExpr | binaryExprRight, binaryExprLeft |
| SMB | Self modulo or division | BinaryExpr | binaryExprOP |
| SEB | Switch label without break | SwitchEntry, BreakStmt, ReturnStmt, ThrowStmt | labels |
| SND | Switch without default statement | SwitchStmt, SwitchEntry | hasSwitchEntry |
| SL | Lock object used as a synchronized lock | SynchronizedStmt, Variable | variableName, elementType |
| TR | Calling run() on a Thread object | MethodCallExpr, Variable | variableName, elementType, methodScope |
| TW | Calling wait() on a Thread object | MethodCallExpr, Variable | variableName, elementType, methodScope |
| TEF | Throwing an exception in a finally block | TryStmt, BlockStmt, ThrowStmt | hasFinallyBlock, hasThrowStmt |
| TLJ | Java file exceeding 200 lines | JavaFile | beginLine, endLine |
| TLM | Method exceeding 80 lines | Method | beginLine, endLine |
| USC | Updating a member variable in a constructor | Constructor, AssignExpr, Field | isStatic, assignTarget |
| VNI | Variable not initialized | Variable, ForEachStmt | hasVariable, variableInitializer, variableDeclarator |
| VC | Operations on a volatile variable | FieldDeclaration, UnaryExpr | unaryExprExpression, variableName |
| WII | Calling wait() inside an if block | IfStmt, MethodCallExpr | methodName |
| WCC | Constant while condition | WhileStmt | conditionIs |
| Java Project Name | Owner | Version | Repository URL |
|---|---|---|---|
| commons-cli | Apache | 1.5.0 | https://github.com/apache/commons-cli (accessed on 15 May 2026) |
| gson | 2.10.0 | https://github.com/google/gson (accessed on 15 May 2026) | |
| commons-net | Apache | 3.9.0 | https://github.com/apache/commons-net (accessed on 15 May 2026) |
| commons-io | Apache | 2.11.0 | https://github.com/apache/commons-io (accessed on 15 May 2026) |
| commons-collections | Apache | 4.4 | https://github.com/apache/commons-collections (accessed on 15 May 2026) |
| zookeeper | Apache | 3.8.1 | https://github.com/apache/zookeeper (accessed on 15 May 2026) |
| commons-math | Apache | 4.0 | https://github.com/apache/commons-math (accessed on 15 May 2026) |
| kafka | Apache | 3.4.0 | https://github.com/apache/kafka (accessed on 15 May 2026) |
| Project | Defects | Reported | TP | TN | FP | FN | Precision (%) | Recall (%) | F-Score (%) |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 454 | 429 | 417 | 41,016 | 12 | 37 | 97.20 | 91.85 | 94.45 |
| 2 | 1404 | 1290 | 1198 | 330,360 | 92 | 206 | 92.87 | 85.33 | 89.00 |
| Abbr. | JDQuery Rule | PMD Rule |
|---|---|---|
| CIC | Class or interface name not capitalized | ClassNamingConventions |
| CNC | Constant name not capitalized | FieldNamingConventions |
| LCF | Field name not following lower camel case | FieldNamingConventions |
| LCV | Variable name not following lower camel case | LocalVariableNamingConventions |
| MN | Method name not following lower camel case | MethodNamingConventions |
| EFB | Empty finally block | EmptyFinallyBlock |
| SEB | Switch label without break | ImplicitSwitchFallThrough |
| SND | Switch without default statement | SwitchStmtsShouldHaveDefault |
| TLJ | Java file exceeding 200 lines | ExcessiveFileLength |
| TLM | Method exceeding 80 lines | ExcessiveMethodLength |
| Defect Group | Number | Tool | Reported | TP | FP | FN | Precision (%) | Recall (%) |
|---|---|---|---|---|---|---|---|---|
| Injected Defects | 101 | JDQuery | 99 | 96 | 3 | 5 | 96.97 | 95.05 |
| PMD | 73 | 69 | 4 | 32 | 94.52 | 68.32 | ||
| Native Real Defects | 86 | JDQuery | 88 | 86 | 2 | 0 | 97.73 | 100.00 |
| PMD | 1 | 1 | 0 | 85 | 100 | 1.16 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2026 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license.
Share and Cite
Hu, T.; Wang, T. JDQuery: Query-Driven Defect Localization for Java Source Code Based on Code Knowledge Graphs. Electronics 2026, 15, 3827. https://doi.org/10.3390/electronics15173827
Hu T, Wang T. JDQuery: Query-Driven Defect Localization for Java Source Code Based on Code Knowledge Graphs. Electronics. 2026; 15(17):3827. https://doi.org/10.3390/electronics15173827
Chicago/Turabian StyleHu, Tianyuan, and Tong Wang. 2026. "JDQuery: Query-Driven Defect Localization for Java Source Code Based on Code Knowledge Graphs" Electronics 15, no. 17: 3827. https://doi.org/10.3390/electronics15173827
APA StyleHu, T., & Wang, T. (2026). JDQuery: Query-Driven Defect Localization for Java Source Code Based on Code Knowledge Graphs. Electronics, 15(17), 3827. https://doi.org/10.3390/electronics15173827


