# (DD09) Business Logic — PatternBExtractor.extract() [24 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `com.optage.parser.PatternBExtractor` |
| Layer | Utility |
| Module | `parser` (Package: `com.optage.parser`) |

## 1. Role

### PatternBExtractor.extract()

This method scans an input source snippet line by line and extracts business labels from field declarations that carry inline comments. In practice, it acts as a lightweight parser for a specific code annotation pattern: a field declaration such as `private` or `public` followed by `//` comment text that can be turned into a `BusinessLabel` record. The method is not performing domain CRUD against persistent data; instead, it converts source-code-like text into structured metadata for downstream analysis or documentation generation.

The method implements a simple routing/dispatch pattern based on the presence of a declaration keyword and an inline comment. Only lines that look like field declarations are examined further, and only lines with a valid comment are transformed into label objects. This makes the method a shared parsing utility that can be reused wherever the system needs to infer business-friendly labels from code comments.

Within the larger system, this method serves as a text-to-metadata transformer for pattern-based extraction. It separates the declaration part from the comment part, derives a field name and a label, validates both values, and then appends a `BusinessLabel` with the source tag `inline_comment`. The overall business role is to normalize developer-authored inline comments into a structured list that other components can consume for reporting, indexing, or documentation workflows.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["extract(code)"])
INIT["Create empty BusinessLabel list"]
SPLIT["Split code into lines by newline"]
LOOP{"For each line"}
CHECK{"Line contains private or public"}
COMMENT{"Inline comment exists with //"}
FIELD["Extract field name from declaration"]
LABEL["Extract label from comment"]
VALID{"fieldName and label are both valid and label is not empty"}
ADD["Add new BusinessLabel(fieldName, label, inline_comment)"]
RETURN["Return labels"]
START --> INIT
INIT --> SPLIT
SPLIT --> LOOP
LOOP --> CHECK
CHECK -->|Yes| COMMENT
CHECK -->|No| LOOP
COMMENT -->|Yes| FIELD
COMMENT -->|No| LOOP
FIELD --> LABEL
LABEL --> VALID
VALID -->|Yes| ADD
VALID -->|No| LOOP
ADD --> LOOP
LOOP --> RETURN
```

**CRITICAL — Constant Resolution:**
No external business constants are referenced in this method. The branch conditions rely on literal tokens (`private`, `public`, `//`) embedded directly in the source code.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `code` | `String` | Source text to be analyzed for business-label extraction. It is expected to contain one or more lines of code-like content; each line may include a field declaration and an inline `//` comment. Only lines that match this pattern influence processing, and the text content directly determines which `BusinessLabel` objects are created. |

**External state read by this method:** none. The method uses only the incoming `code` parameter and local variables.

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `PatternBExtractor.extractFieldName` | PatternBExtractor | - | Calls `extractFieldName` in `PatternBExtractor` |
| - | `PatternBExtractor.extractLabel` | PatternBExtractor | - | Calls `extractLabel` in `PatternBExtractor` |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `String.split` | JDK String | - | Splits the input source text into individual lines for parsing |
| R | `String.contains` | JDK String | - | Checks whether a line appears to be a field declaration |
| R | `String.indexOf` | JDK String | - | Locates the inline comment marker `//` |
| R | `String.substring` | JDK String | - | Separates the declaration portion from the comment portion |
| R | `PatternBExtractor.extractFieldName` | PatternBExtractor | - | Derives a field name from the declaration text |
| R | `PatternBExtractor.extractLabel` | PatternBExtractor | - | Derives a business label from the inline comment text |
| C | `labels.add` | `java.util.List` | BusinessLabel collection | Appends a newly created `BusinessLabel` object to the result list |
| C | `new BusinessLabel(...)` | BusinessLabel | BusinessLabel | Creates a parsed label record for downstream use |

**Classification note:** Although this method does not touch a database, CRUD is used here in the broader data-transformation sense. The only creation activity is the instantiation and collection of `BusinessLabel` objects.

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 7 methods.
Terminal operations from this method: `extractLabel` [-], `extractFieldName` [-]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen: direct test caller | `PatternBExtractorTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 2 | Screen: direct test caller | `PatternBExtractorTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 3 | Screen: direct test caller | `PatternBExtractorTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 4 | Screen: direct test caller | `PatternBExtractorTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 5 | Screen: direct test caller | `PatternBExtractorTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 6 | Controller: direct integration caller | `IntegrationTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |
| 7 | Controller: direct integration caller | `IntegrationTest.test* -> PatternBExtractor.extract` | `extractFieldName [R] -` / `extractLabel [R] -` |

The method is called directly from test fixtures and integration-style verification code rather than from production screens or batch entry points. Its downstream terminals are helper methods inside the same class, which means the dependency chain ends in local parsing logic rather than external service or DB access.

## 6. Per-Branch Detail Blocks

**Block 1** — [SET] `(method entry and initialization)` (L14-L15)

> Creates the result container that will accumulate extracted business labels.

| # | Type | Code |
|---|------|------|
| 1 | SET | `List<BusinessLabel> labels = new ArrayList<>();` // initialize empty result collection |

**Block 2** — [SET] `(split input text into lines)` (L16)

> Converts the input code snippet into line-based units for sequential inspection.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String[] lines = code.split("
");` // split source text into lines |

**Block 3** — [FOR] `(for each line in lines)` (L18-L30)

> Iterates through every source line and applies the field/comment extraction rule.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `for (String line : lines) { ... }` // process each line |

**Block 3.1** — [IF] `(line contains "private" or "public")` (L20)

> Filters candidate lines to only those that look like field declarations.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `line.contains("private")` // check for private field declaration |
| 2 | EXEC | `line.contains("public")` // check for public field declaration |

**Block 3.2** — [IF] `(commentIndex > 0)` (L21-L22)

> Confirms the line contains an inline comment token and separates declaration text from comment text.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int commentIndex = line.indexOf("//");` // locate comment marker |
| 2 | EXEC | `line.indexOf("//")` // identify inline comment boundary |
| 3 | SET | `String fieldPart = line.substring(0, commentIndex);` // extract declaration portion |
| 4 | SET | `String commentPart = line.substring(commentIndex);` // extract comment portion |

**Block 3.3** — [CALL] `(derive field name and label)` (L24-L25)

> Translates the raw text fragments into business-friendly values.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `extractFieldName(fieldPart);` // derive field name |
| 2 | CALL | `extractLabel(commentPart);` // derive label text |

**Block 3.4** — [IF] `(fieldName != null && label != null && !label.isEmpty())` (L27)

> Validates that both parsed values are usable before creating a label object.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `fieldName != null` // ensure field name exists |
| 2 | EXEC | `label != null` // ensure label exists |
| 3 | EXEC | `!label.isEmpty()` // ensure label text is not blank |

**Block 3.4.1** — [SET/CALL] `(create and append BusinessLabel)` (L28)

> Adds a normalized business label record to the result list.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `new BusinessLabel(fieldName, label, "inline_comment")` // create parsed label record |
| 2 | SET | `labels.add(new BusinessLabel(fieldName, label, "inline_comment"));` // append to output list |

**Block 4** — [RETURN] `(return labels)` (L33)

> Returns all labels extracted from the input source.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return labels;` // final extracted label list |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `code` | Field | Input source text provided to the extractor. It contains the code-like lines that are scanned for declarations and inline comments. |
| `BusinessLabel` | Domain object | Structured business label record produced by the parser. It stores the extracted field name, label text, and origin tag. |
| `fieldName` | Field | Parsed identifier from the field declaration part of the line. It represents the source field being annotated. |
| `label` | Field | Human-readable label derived from the inline comment text. It becomes the business description associated with the field. |
| `inline_comment` | Technical tag | Source marker indicating the label was extracted from an inline `//` comment. |
| `private` | Java keyword | Access modifier used to identify candidate field declarations. |
| `public` | Java keyword | Access modifier used to identify candidate field declarations. |
| `//` | Syntax token | Inline comment delimiter used to split declaration text from descriptive comment text. |
| `ArrayList` | JDK collection | Mutable list implementation used to accumulate extracted labels. |
| `String.split` | JDK API | Splits the source text into line units for scanning. |
| `String.contains` | JDK API | Tests whether a line likely represents a field declaration. |
| `String.indexOf` | JDK API | Locates the first inline comment marker within a line. |
| `String.substring` | JDK API | Extracts the declaration and comment fragments from the line. |
| `extractFieldName` | Helper method | Local parser helper that resolves a field declaration into its field name. |
| `extractLabel` | Helper method | Local parser helper that resolves inline comment text into a business label. |
