Skip to content

Add 6 Java patterns and fix resource leak in writing-files#19

Merged
brunoborges merged 4 commits intomainfrom
copilot/add-new-java-patterns
Feb 18, 2026
Merged

Add 6 Java patterns and fix resource leak in writing-files#19
brunoborges merged 4 commits intomainfrom
copilot/add-new-java-patterns

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 17, 2026

Implementation Complete ✅

  • Understand repository structure and pattern schema
  • Fix writing-files.json oldCode bug (resource leak issue)
  • Add markdown-comments pattern (language category)
  • Add static-methods-in-interfaces pattern (language category)
  • Add static-members-in-inner-classes pattern (language category)
  • Add compact-canonical-constructor pattern (language category)
  • Add reverse-list-iteration pattern (collections category)
  • Add file-memory-mapping pattern (io category)
  • Update prev/next links for all affected patterns
  • Run generator to build HTML files
  • Test generated pages locally
  • Address code review feedback
  • Security scan with CodeQL (no issues - JSON only)
  • Fix compact-canonical-constructor oldLabel (Java 8 → Java 16)
  • Final validation complete
Original prompt

This section details on the original issue you should resolve

<issue_title>New patterns</issue_title>
<issue_description>Some missing bits:

  • markdown comments
    /// This is a javadoc comment

  • static method in interface (Java 8)

  • static member in inner classes (Java 16)

    class Main {
    class Book {
    static int globalBookCount;

     Book() {
      globalBookCount++;
     }
    

    }
    }

  • compact canonical constructor (for preconditions) (Java 15 ?)

    public record Person(String name, List pets) {
    public {
    Objects.requireNonNull(name);
    pets = List.copyOf(pets);
    }
    }

  • loop over a List in reverse order

    for(Iterator iterator = list.listIerator(list.isze()); iterator.hasPrevious();) {
    String element = iterator.previous();
    ...
    }

vs

for(String element : list.reversed()) {
...
}

  • File mapping in memory

    FileChannel channel = ...
    MappedByteBuffer buffer = channel.map( FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());
    // file size limited to 2G
    // freed by the GC, no control

vs

FileChannel channel = ...
try (Arena arena = Arena.ofShared()) {
MemorySegment segment = channel.map( FileChannel.MapMode.READ_WRITE, 0, channel.size(), arena);
// no file size limit
...
} // call close() => freed

  • The example of "writing files" old style is wrong
    try(BufferedWriter writer = new BufferedWriter(new FileReader(path))) {
    ...
    }

    here if the new BufferedWriterfails to allocate the buffer, close() is not called on the FileReader,
    it should be (using two lines)

    try(FileWriter fileWriter = new FileReader(path);
    BufferedWriter writer = new BufferedWriter(fileWriter)) {
    ...
    }</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 17, 2026 14:21
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new Java patterns and features Add 6 Java patterns and fix resource leak in writing-files Feb 17, 2026
Copilot AI requested a review from brunoborges February 17, 2026 14:26
@brunoborges
Copy link
Copy Markdown
Member

@copilot slug compact-canonical-constructor says the old way was for Java 8, but records don't exist in Java 8

Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 17, 2026

@copilot slug compact-canonical-constructor says the old way was for Java 8, but records don't exist in Java 8

Fixed in e247d91. Changed oldLabel from "Java 8" to "Java 16" since both the full canonical constructor and compact canonical constructor are record features available since Java 16.

compact-canonical-constructor-fixed

@brunoborges brunoborges marked this pull request as ready for review February 18, 2026 00:03
@brunoborges brunoborges merged commit 0c78f74 into main Feb 18, 2026
@brunoborges brunoborges deleted the copilot/add-new-java-patterns branch February 26, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New patterns

2 participants