Skip to content

Commit 845d36e

Browse files
committed
Initial Java rules
1 parent 1c4cda0 commit 845d36e

8 files changed

Lines changed: 235 additions & 25 deletions

.cursor/rules/01-java.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# AI Developer Profile
2+
ai_persona:
3+
role: Senior Java Developer
4+
principles:
5+
- SOLID
6+
- DRY
7+
- KISS
8+
- YAGNI
9+
- OWASP
10+
- DOP
11+
- FP
12+
- DDD
13+
14+
# Technical Stack
15+
tech_stack:
16+
framework: Spring Boot
17+
build_tool: Maven
18+
java_version: 24
19+
dependencies:
20+
- Eclipse Collections
21+
- Commons Lang3
22+
- Guava
23+
- VAVR
24+
- JUnit5
25+
- JQwik
26+
- JMH
27+
language: English
28+
code_comments: English

.cursor/rules/02-effective-java.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Development Guidelines
2+
effective_java_notes:
3+
chapter_2:
4+
title: "Creating and Destroying Objects"
5+
items:
6+
- "Consider static factory methods instead of constructors"
7+
- "Consider a builder when faced with many constructor parameters"
8+
- "Enforce the singleton property with a private constructor or an enum type"
9+
- "Enforce noninstantiability with a private constructor"
10+
- "Prefer dependency injection to hardwiring resources"
11+
- "Avoid creating unnecessary objects"
12+
- "Eliminate obsolete object references"
13+
- "Avoid finalizers and cleaners"
14+
- "Prefer try-with-resources to try-finally"
15+
16+
chapter_3:
17+
title: "Methods Common to All Objects"
18+
items:
19+
- "Obey the general contract when overriding equals"
20+
- "Always override hashCode when you override equals"
21+
- "Always override toString"
22+
- "Override clone judiciously"
23+
- "Consider implementing Comparable"
24+
25+
chapter_4:
26+
title: "Classes and Interfaces"
27+
items:
28+
- "Minimize the accessibility of classes and members"
29+
- "In public classes, use accessor methods, not public fields"
30+
- "Minimize mutability"
31+
- "Favor composition over inheritance"
32+
- "Design and document for inheritance or else prohibit it"
33+
- "Prefer interfaces to abstract classes"
34+
- "Design interfaces for posterity"
35+
- "Use interfaces only to define types"
36+
- "Prefer class hierarchies to tagged classes"
37+
- "Favor static member classes over nonstatic"
38+
- "Limit source files to a single top-level class"
39+
40+
chapter_5:
41+
title: "Generics"
42+
items:
43+
- "Don't use raw types"
44+
- "Eliminate unchecked warnings"
45+
- "Prefer lists to arrays"
46+
- "Favor generic types"
47+
- "Favor generic methods"
48+
- "Use bounded wildcards to increase API flexibility"
49+
- "Combine generics and varargs judiciously"
50+
- "Consider typesafe heterogeneous containers"
51+
52+
chapter_6:
53+
title: "Enums and Annotations"
54+
items:
55+
- "Use enums instead of int constants"
56+
- "Use instance fields instead of ordinals"
57+
- "Use EnumSet instead of bit fields"
58+
- "Use EnumMap instead of ordinal indexing"
59+
- "Emulate extensible enums with interfaces"
60+
- "Prefer annotations to naming patterns"
61+
- "Consistently use the Override annotation"
62+
- "Use marker interfaces to define types"
63+
64+
chapter_7:
65+
title: "Lambdas and Streams"
66+
items:
67+
- "Prefer lambdas to anonymous classes"
68+
- "Prefer method references to lambdas"
69+
- "Favor the use of standard functional interfaces"
70+
- "Use streams judiciously"
71+
- "Prefer side-effect-free functions in streams"
72+
- "Prefer Collection to Stream as a return type"
73+
- "Use caution when making streams parallel"
74+
75+
chapter_8:
76+
title: "Methods"
77+
items:
78+
- "Check parameters for validity"
79+
- "Make defensive copies when needed"
80+
- "Design method signatures carefully"
81+
- "Use overloading judiciously"
82+
- "Use varargs judiciously"
83+
- "Return empty collections or arrays, not nulls"
84+
- "Return optionals judiciously"
85+
- "Write doc comments for all exposed API elements"
86+
87+
chapter_9:
88+
title: "General Programming"
89+
items:
90+
- "Minimize the scope of local variables"
91+
- "Prefer for-each loops to traditional for loops"
92+
- "Know and use the libraries"
93+
- "Avoid float and double if exact answers are required"
94+
- "Prefer primitive types to boxed primitives"
95+
- "Avoid strings where other types are more appropriate"
96+
- "Beware the performance of string concatenation"
97+
- "Refer to objects by their interfaces"
98+
- "Prefer interfaces to reflection"
99+
- "Use native methods judiciously"
100+
- "Optimize judiciously"
101+
- "Adhere to generally accepted naming conventions"
102+
103+
chapter_10:
104+
title: "Exceptions"
105+
items:
106+
- "Use exceptions only for exceptional conditions"
107+
- "Use checked exceptions for recoverable conditions and runtime exceptions for programming errors"
108+
- "Avoid unnecessary use of checked exceptions"
109+
- "Favor the use of standard exceptions"
110+
- "Throw exceptions appropriate to the abstraction"
111+
- "Document all exceptions thrown by each method"
112+
- "Include failure-capture information in detail messages"
113+
- "Strive for failure atomicity"
114+
- "Don't ignore exceptions"
115+
116+
chapter_11:
117+
title: "Concurrency"
118+
items:
119+
- "Synchronize access to shared mutable data"
120+
- "Avoid excessive synchronization"
121+
- "Prefer executors, tasks, and streams to threads"
122+
- "Prefer concurrency utilities to wait and notify"
123+
- "Document thread safety"
124+
- "Use lazy initialization judiciously"
125+
- "Don't depend on the thread scheduler"
126+
127+
chapter_12:
128+
title: "Serialization"
129+
items:
130+
- "Prefer alternatives to Java serialization"
131+
- "Implement Serializable with great caution"
132+
- "Consider using a custom serialized form"
133+
- "Write readObject methods defensively"
134+
- "For instance control, prefer enum types to readResolve"
135+
- "Consider serialization proxies instead of serialized instances"

.cursor/rules/03-concurrency.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
concurrency_guidelines:
2+
- "Use thread-safe data structures"
3+
- "Use thread-safe collections"
4+
- "Use thread-safe classes"
5+
- "Use thread-safe methods"
6+
- "Use thread-safe objects"
7+
- "Use thread-safe variables"
8+
- "Use thread-safe methods"
9+
- "Use thread-safe objects"
10+
- "Use thread-safe variables"
11+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
functional_programming_guidelines:
2+
- "Try to use immutable objects"
3+
- "Try to not mutate the state of the objects"
4+
- "Try to use pure functions"
5+
- "Try to use functional interfaces"
6+
- "Try to use lambda expressions"
7+
- "Try to use streams"
8+
- "Try to use functional programming paradigms"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
data_oriented_programming_pillars:
2+
- "Separate code from data"
3+
- "Represent data with generic data structures"
4+
- "Data should be immutable"
5+
- "Use pure functions to manipulate data"
6+
- "Keep data flat and denormalized"
7+
- "Keep data generic until it needs to be specific"
8+
- "Data integrity is maintained through validation functions"
9+
- "Data access should be flexible and generic"
10+
- "Data transformation should be explicit and traceable"
11+
- "Data flow should be unidirectional"

.cursor/rules/06-spring-boot.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Technology stack: Java Spring Boot 3 with Java 23 Dependencies (ALPHA)
2+
3+
Application Logic Design:
4+
5+
1. All request and response handling must be done only in RestController.
6+
2. RestControllers cannot autowire Repositories directly unless absolutely beneficial to do so.
7+
3. ServiceImpl classes cannot query the database directly and must use Repositories methods, unless absolutely necessary.
8+
4. Entity classes must be used only to carry data out of database query executions.
9+
10+
Service:
11+
12+
1. Service classes must be of type interface.
13+
2. All service class method implementations must be in ServiceImpl classes that implement the service class,
14+
3. All ServiceImpl classes must be annotated with @Service.
15+
4. For any logic requiring checking the existence of a record, use the corresponding repository method with an appropriate .orElseThrow lambda method.
16+
5. For any multiple sequential database executions, must use @Transactional or transactionTemplate, whichever is appropriate.
17+
18+
RestController:
19+
20+
1. Must annotate controller classes with @RestController.
21+
2. Must specify class-level API routes with @RequestMapping, e.g. ("/api/user").
22+
3. Class methods must use best practice HTTP method annotations, e.g, create = @postMapping("/create"), etc.
23+
4. All dependencies in class methods must be @Autowired without a constructor, unless specified otherwise.
24+
5. All class method logic must be implemented in a try..catch block(s).
25+
6. Add always a GlobalExceptionHandler class based on ControllerAdvice to handle all exceptions.

.gitignore

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
1-
# Compiled class file
2-
*.class
3-
4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
13-
# Package Files #
14-
*.jar
15-
*.war
16-
*.nar
17-
*.ear
18-
*.zip
19-
*.tar.gz
20-
*.rar
21-
22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-
replay_pid*
1+
.DS_Store

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
# java-cursor-rules
1+
# Java Cursor AI rules
2+
23
A collection of cursor rules for Java space
4+
5+
## Rules
6+
7+
- [Java](.cursor/rules/01-java.md)
8+
- [Effective Java](.cursor/rules/02-effective-java.md)
9+
- [Concurrency](.cursor/rules/03-concurrency.md)
10+
- [Functional Programming](.cursor/rules/04-functional-programming.md)
11+
- [Data oriented programming](.cursor/rules/05-data-oriented-programming.md)
12+
- [Spring boot](.cursor/rules/06-spring-boot.md)
13+
14+
## References
15+
16+
- https://www.cursor.com/
17+
- https://docs.cursor.com/context/rules-for-ai

0 commit comments

Comments
 (0)