generator-jhipster icon indicating copy to clipboard operation
generator-jhipster copied to clipboard

Please set attribute access modifiers to protected instead of private for all service classes and controller classes

Open vw98075 opened this issue 2 years ago • 3 comments

Overview of the feature request

If JHipster is used as a building block of a real-world application, almost all of those generated classes need to be extended to separate customized code from the generated code. However, such an extended class won't be able to access its parent class attributes unless it changes the attribute modifiers to protected from private in its parent class.

Motivation for or Use Case

If JHipster is meant for real-world application usage, not just for demos or small-scale prototyping, it needs to be built for customization.

For example, a new web tier controller method is needed in addition to those generated ones. Based on the Single Responsibility Principle principle, a controller class with the new method is created by extending the existing controller class instead of creating an independent controller class, because it is for the same responsibility and the same consumer. In this case, the protected access modifier would make this type of customization litter easier.

Related issues or PR
  • [x] Checking this box is mandatory (this is just to show you read everything)

vw98075 avatar Oct 01 '23 22:10 vw98075

Add this to .blueprint/app/generator.mjs file.

import BaseApplicationGenerator from 'generator-jhipster/generators/base-application';

export default class extends BaseApplicationGenerator {
  constructor(args, opts, features) {
    super(args, opts, { ...features, sbsBlueprint: true });
  }

  get [BaseApplicationGenerator.POST_WRITING]() {
    return this.asPostWritingTaskGroup({
      async postWritingTemplateTask() {
        this.queueTransformStream(async function* (generator) {
          for await (const file of generator) {
            if (file.path.endsWith('.java') && !file.path.includes('test')) {
              file.contents = Buffer.from(file.contents.toString().replaceAll('private', 'protected'));
            }
            yield file;
          }
        });
      },
    });
  }
}

mshima avatar Oct 07 '23 01:10 mshima

Hi, +1 Yes Please, please, please !!!

and also for Angular, (may be also for Vue & react)

You preconize to extends "services" classes for further customizations pattern-3---side-by-side

but these classes must be open for extention (private -> protected).

if not lot of boiler code will be duplicated.

desprez avatar Feb 03 '24 23:02 desprez

Please create a PR with this enhancement. I'd be happy to review it!

mraible avatar Feb 04 '24 00:02 mraible

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days

github-actions[bot] avatar Aug 02 '24 00:08 github-actions[bot]