Skip to content

Commit

Permalink
Merge pull request #26344 from mshima/edit-java-file
Browse files Browse the repository at this point in the history
add editJavaFile
  • Loading branch information
DanielFran authored Jun 3, 2024
2 parents da574ac + 77e8491 commit 882eca1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
12 changes: 12 additions & 0 deletions generators/java/generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
isReservedJavaKeyword,
matchMainJavaFiles,
javaMainPackageTemplatesBlock,
addJavaImport,
addJavaAnnotation,
} from '../../support/index.js';

export default class BootstrapGenerator extends BaseApplicationGenerator {
Expand Down Expand Up @@ -105,6 +107,16 @@ export default class BootstrapGenerator extends BaseApplicationGenerator {
source.hasJavaProperty = (property: string) => application.javaProperties![property] !== undefined;
source.hasJavaManagedProperty = (property: string) => application.javaManagedProperties![property] !== undefined;
},
needles({ source }) {
source.editJavaFile = (file, { staticImports = [], imports = [], annotations = [] }, ...editFileCallback) =>
this.editFile(
file,
...staticImports.map(classPath => addJavaImport(classPath, { staticImport: true })),
...imports.map(classPath => addJavaImport(classPath)),
...annotations.map(annotation => addJavaAnnotation(annotation)),
...editFileCallback,
);
},
});
}

Expand Down
12 changes: 12 additions & 0 deletions generators/java/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RequireOneOrNone } from 'type-fest';
import { BaseApplication } from '../base-application/types.js';
import { GradleApplication, GradleNeedleOptions } from '../gradle/types.js';
import { JavaAnnotation } from './support/add-java-annotation.ts';
import { EditFileCallback } from '../base/api.js';

export type JavaDependencyVersion = {
name: string;
Expand Down Expand Up @@ -73,4 +75,14 @@ export type JavaSourceType = {
addJavaDependencies?(dependency: JavaDependency[], options?: JavaNeedleOptions): void;
hasJavaProperty?(propertyName: string): boolean;
hasJavaManagedProperty?(propertyName: string): boolean;

/**
* Edit a Java file by adding static imports, imports and annotations.
* Callbacks are passed to the editFile method.
*/
editJavaFile?: (
file: string,
{ staticImports, imports, annotations }: { staticImports?: string[]; imports?: string[]; annotations?: JavaAnnotation[] },
...editFileCallback: EditFileCallback[]
) => void;
};
3 changes: 2 additions & 1 deletion generators/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MessageBrokerApplicationType } from './options/message-broker.js';
import type { DeterministicOptionWithDerivedProperties, OptionWithDerivedProperties } from '../base-application/application-options.js';
import { ApplicationPropertiesNeedles } from './support/needles.ts';
import { GatewayApplication } from '../spring-cloud/generators/gateway/types.ts';
import { JavaAnnotation } from '../java/support/add-java-annotation.ts';

export type SpringEntity = {
/* Generate entity's Entity */
Expand All @@ -29,7 +30,7 @@ export type SpringBootSourceType = JavaSourceType &
addLogbackLogEntry?({ file, name, level }: { file: string; name: string; level: string }): void;
addLogbackMainLog?({ name, level }: { name: string; level: string }): void;
addLogbackTestLog?({ name, level }: { name: string; level: string }): void;
addIntegrationTestAnnotation?({ package, annotation }: { package?: string; annotation: string }): void;
addIntegrationTestAnnotation?(annotation: JavaAnnotation): void;
addAllowBlockingCallsInside?({ classPath, method }: { classPath: string; method: string }): void;
addApplicationPropertiesContent?(content: ApplicationPropertiesNeedles): void;
addApplicationPropertiesProperty?({ propertyName, propertyType }: { propertyName: string; propertyType: string }): void;
Expand Down
10 changes: 5 additions & 5 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
insertContentIntoApplicationProperties,
javaBeanCase,
} from '../server/support/index.js';
import { addJavaAnnotation, generateKeyStore } from '../java/support/index.js';
import { generateKeyStore } from '../java/support/index.js';
import { createNeedleCallback, mutateData } from '../base/support/index.js';
import {
APPLICATION_TYPE_MICROSERVICE,
Expand Down Expand Up @@ -304,10 +304,10 @@ export default class SpringBootGenerator extends BaseApplicationGenerator {
};
},
addSpringIntegrationTest({ application, source }) {
source.addIntegrationTestAnnotation = ({ package: packageName, annotation }) =>
this.editFile(this.destinationPath(`${application.javaPackageTestDir}IntegrationTest.java`), content =>
addJavaAnnotation(content, { package: packageName, annotation }),
);
source.addIntegrationTestAnnotation = annotation =>
source.editJavaFile!(this.destinationPath(`${application.javaPackageTestDir}IntegrationTest.java`), {
annotations: [annotation],
});
},
addLogNeedles({ source }) {
source.addLogbackLogEntry = ({ file, name, level }) =>
Expand Down

0 comments on commit 882eca1

Please sign in to comment.