-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSnippetsExample.java
More file actions
122 lines (114 loc) · 3.14 KB
/
Copy pathCodeSnippetsExample.java
File metadata and controls
122 lines (114 loc) · 3.14 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package java18.javadoc;
/**
* Java 18 Code Snippets in Java API Documentation
* Demonstrates the @snippet tag in Javadoc
*
* NOTE: The @snippet tag is used in Javadoc comments, not in regular code
* This file shows examples of how to use @snippet in your documentation
*/
public class CodeSnippetsExample {
/**
* Calculates the sum of two numbers.
*
* <p>Example usage:</p>
* {@snippet :
* Calculator calc = new Calculator();
* int result = calc.add(5, 3);
* System.out.println(result); // Output: 8
* }
*
* @param a first number
* @param b second number
* @return sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
/**
* Checks if a string is empty or null.
*
* <p>Example:</p>
* {@snippet :
* String str = "Hello";
* if (isEmpty(str)) {
* System.out.println("String is empty");
* }
* }
*
* @param str the string to check
* @return true if string is null or empty
*/
public boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
/**
* External snippet file example.
*
* <p>You can also reference external snippet files:</p>
* {@snippet file="examples.java" region="calculator"}
*
* The external file would contain:
* <pre>
* // @start region="calculator"
* Calculator calc = new Calculator();
* int result = calc.add(5, 3);
* // @end
* </pre>
*
* @param value the value to process
* @return processed value
*/
public int process(int value) {
return value * 2;
}
/**
* Highlights specific parts of code.
*
* <p>Example with highlighting:</p>
* {@snippet :
* String name = "Java"; // @highlight substring="name"
* int version = 18; // @highlight substring="version"
* System.out.println(name + " " + version);
* }
*
* @param name the name
* @param version the version
*/
public void display(String name, int version) {
System.out.println(name + " " + version);
}
/**
* Shows how to use attributes in snippets.
*
* <p>Example with attributes:</p>
* {@snippet lang="java" :
* public class Example {
* public void method() {
* System.out.println("Hello");
* }
* }
* }
*
* @param message the message to display
*/
public void showMessage(String message) {
System.out.println(message);
}
}
/**
* Benefits of @snippet tag:
*
* 1. Syntax highlighting in generated documentation
* 2. Compile-time validation of code snippets
* 3. Better integration with IDE
* 4. Embedded examples in API documentation
* 5. External snippet files for complex examples
* 6. Highlighting specific parts of code
* 7. Language-specific formatting
*
* Usage:
* - Inline snippets: {@snippet : code here }
* - External snippets: {@snippet file="file.java" region="regionName"}
* - With attributes: {@snippet lang="java" : code here }
* - With highlighting: // @highlight substring="variableName"
*/