Skip to main content

Use Cases

1. Code Shift for Java Code Analysis and Refactoring

See below an example of the Alphanumeric CNPJ module for analyzing and refactoring Java code. It uses an AST (Abstract Syntax Tree)-based analysis and keyword lists to identify the use of CNPJ in repositories. Based on this, it suggests improvements for CNPJ handling in Java applications with StackSpot AI.

1. Searching for variable declarations with CNPJ

The goal is to identify variables whose name or type is related to CNPJ keywords.

Example:

private long cnpj;
Long cnpj = Long.parseLong(cnpjString);

2. Searching for method declarations with CNPJ

The goal is to locate methods that handle or expose the CNPJ.

Example:

public long getCnpj() { ... }
public void setCnpj(long cnpj) { ... }

3. Searching for assignments to CNPJ values

The goal is to detect direct assignments of values to CNPJ fields or variables.

Example:

cnpjLong = 34110468000150L;
anotherClient.setCnpj(45997418000153L);

4. Searching for class instantiations with CNPJ

The goal is to identify constructors or methods that receive CNPJ as a parameter.

Example:

public EmpresaPública(String razaoSocial, String endereco, long cnpj) { ... }
Optional<ClientePJ> findByCnpj(Long cnpj);

5. Searching for chained calls (Builders) with CNPJ

The goal is to detect the use of CNPJ in builder patterns, for example, Lombok, especially when using inappropriate types.

Example:


// Before:
NovaEmpresaRequest empresaAntes = NovaEmpresaRequest.builder()
.nomeFantasia("Tech Solutions")
.razaoSocial("Tech Solutions LTDA")
.cnpj(12345678000190L) // ⚠️ uso de long em vez de String
.inscricaoEstadual("1234567890")
.emailContato("contato@techsolutions.com")
.telefoneContato("(11) 98765-4321")
.endereco("Av. Paulista, 1000 - São Paulo/SP")
.build();
// After:
NovaEmpresaRequest empresaDepois = NovaEmpresaRequest.builder()

6. Searching for Comparisons with CNPJ

The goal is to identify direct comparisons of CNPJ, especially when using numeric types.

Example:

if (cnpj == 34110468000150L) { ... } // ⚠️ using long instead of String

7. Filtering by Inadequate Typing

The goal is to list all occurrences of CNPJ with types other than String.

Analyzed types:

  • byte;
  • short;
  • int;
  • long;
  • float;
  • double;
  • Integer.

8. Automated Refactoring via StackSpot AI

The main objectives are:

  • Find the change points;
  • Refactor the code to ensure that CNPJ is handled as a String;
  • Maintain compatibility with numeric types;
  • Adjust automated tests.

Example:

// Before:
public class Empresa {
private long cnpj; // ⚠️ CNPJ como long
public void setCnpj(long cnpj) {
this.cnpj = cnpj;
}
public long getCnpj() {
return cnpj;
}
}
// After:
public class Empresa {
private String cnpj; // ✅ CNPJ como String
public void setCnpj(long cnpj) {
this.cnpj = String.valueOf(cnpj);
}
public void setCnpj(String cnpj) {
this.cnpj = cnpj;
}
public String getCnpj() {
return cnpj;
}
}

9. Searching for CNPJ Annotations and Validations

The goal is to identify the use of validation annotations and suggest standardization.

Example:

// Before:
public class Empresa {
@ValidCnpj
private String cnpj;
}

// After:
public class Empresa {
@CNPJ // Standardized annotation for CNPJ validation
private String cnpj;
}

10. Searching for CNPJ Masks

The goal is to detect manual implementations of CNPJ masking/validation.

Example:

String formattedCnpj = cnpj.substring(0,2) + "." + ...;

11. Searching for CNPJ Serialization and Deserialization

The goal is to identify issues in CNPJ serialization/deserialization.

Example:

@JsonProperty("cnpj")
private String cnpj;

12. Comments About CNPJ

The goal is to identify comments related to the CNPJ field in the code and suggest changes to ensure the description is correct.

Example:

// Before:
/**
* Company CNPJ must contain only numbers.
*/
private String cnpj;
// After:
/**
* Company CNPJ may contain alphanumeric characters.
*/
private String cnpj;

2. Code Shift in a Cobol Code Analysis and Refactoring

See an example using the Alphanumeric CNPJ module for Cobol code analysis and refactoring. It uses keyword lists to identify CNPJ usage in repositories and suggests improvements for handling CNPJ in Cobol applications with StackSpot AI.

Detailed use cases

1. Declaration of CNPJ PIC Variables

The goal is to standardize the declaration of CNPJ PIC variables to the appropriate alphanumeric type.

Example:

# Before:
01 CNPJ-EMPRESA PIC 9(14).
01 CNPJ-CLIENTE PIC S9(14).
01 CNPJ-FORNECEDOR PIC S9(15).

# After:
01 CNPJ-EMPRESA PIC X(14).
01 CNPJ-CLIENTE PIC X(14).
01 CNPJ-FORNECEDOR PIC X(15).

2. Identification of COMP-3 CNPJ variables

The goal is to standardize the declaration of COMP-3 CNPJ variables to the appropriate alphanumeric type.

  • Example 1: COMP / COMP-3 / USAGE COMP with FILLER
# Before:
03 NUM-CNPJ PIC 9(18) COMP. *> COMP scenario
...
03 XPTO-FILLER PIC X(60). *> ORIGINAL FILLER AT THE END OF THE BLOCK
* RESERVED SPACE

# After:
03 FILLER PIC 9(18) COMP. *> Kept for compatibility
...
03 NUM-CNPJ PIC X(18). *> CNPJALFA
03 XPTO-FILLER PIC X(42). *> ORIGINAL FILLER ADJUSTED (CNPJALFA)
* RESERVED SPACE
  • Example 2: COMP / COMP-3 / USAGE COMP with FILLER and full use of space
# Before:
03 NUM-CNPJ PIC 9(14) COMP-3. *> COMP-3 scenario
...
03 XPTO-FILLER PIC X(10). *> ORIGINAL FILLER AT THE END OF THE BLOCK
* RESERVED SPACE

# After:
03 FILLER PIC 9(14) COMP-3. *> Kept for compatibility (CNPJALFA)
...
03 NUM-CNPJ PIC X(14). *> CNPJALFA
03 XPTO-FILLER PIC X(10). *> ORIGINAL FILLER COMMENTED (CNPJALFA)
* RESERVED SPACE

Converting from COMP-3 to alphanumeric may impact the record layout in shared copybooks.

3. Assignment and value movement

The goal is to adjust MOVE, INITIALIZE, redefinitions, and type conversions to the new CNPJ standard.

Example:

# Before:
MOVE ZEROS TO CNPJ-EMPRESA.

# After:
MOVE SPACES TO CNPJ-EMPRESA.

4. Alphanumeric CNPJ Validation

The goal is to standardize the validation of the CNPJ field to ensure it contains exactly 14 alphanumeric characters.

Conversion Rules

Original ConditionMandatory ConversionContextHow to Document
EQUAL ZEROSEQUAL SPACESAlphanumeric fieldscondition
GREATER ZEROSNOT EQUAL LOW-VALUESAlphanumeric fieldscondition
IS NUMERICNOT EQUAL SPACESEmpty field validationcondition
NOT NUMERICEQUAL LOW-VALUESEmpty field validationcondition
NOT NUMERIC OR EQUAL ZEROSEQUAL LOW-VALUES OR EQUAL SPACESCombined validationcondition
ZEROS OR SPACESLOW-VALUES OR SPACESEmpty field validationcondition
VALUE ZEROSVALUE SPACESInitializationfield_declaration
VALUE 0VALUE SPACESInitializationfield_declaration
VALUE ZEROESVALUE SPACESInitializationfield_declaration
MOVE ZEROSMOVE SPACESAssignmentmove_operation
  • Example 1: Empty field validation:
# Before:
VARCNPJ EQUALS SPACES OR ZEROS

# After:
VARCNPJ EQUALS SPACES *> CNPJALFA
  • Example 2: Combined validation
# Before
IF VARCNPJCGC NOT NUMERIC OR VARCNPJCGC EQUAL ZEROS

# After
IF VARCNPJCGC EQUAL LOW-VALUES OR VARCNPJCGC EQUAL SPACES *> CNPJALFA

5. Handling Shared Fields (REDEFINES)

The goal is to adjust the use of REDEFINES to allow field sharing between CNPJ.

Example:

# Before:
01 VAR-CGC PIC 9(14). *> REDEFINES scenario
01 WCGC-VAR-CGC REDEFINES VAR-CGC
03 CGC1-VAR-CGC PIC 9(08).
03 CGC2-VAR-CGC PIC 9(04).
03 DACCGC-VAR-CGC PIC 9(02).

# After:
01 VAR-CGC PIC X(14). *> CNPJALFA - corrected REDEFINES scenario
01 WCGC-VAR-CGC REDEFINES VAR-CGC
03 CGC1-VAR-CGC PIC X(08).
03 CGC2-VAR-CGC PIC X(04).
03 DACCGC-VAR-CGC PIC X(02).