Skip to content

Commit

Permalink
Merge pull request jhipster#23119 from mshima/skip_ci-java-unused
Browse files Browse the repository at this point in the history
add transform for java unused variables
  • Loading branch information
DanielFran committed Aug 10, 2023
2 parents 94c06fe + d614db6 commit c35049c
Show file tree
Hide file tree
Showing 38 changed files with 97 additions and 45 deletions.
2 changes: 2 additions & 0 deletions generators/bootstrap/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { GENERATOR_UPGRADE } from '../generator-list.mjs';
import { PRIORITY_NAMES, QUEUES } from '../base-application/priorities.mjs';
import type { BaseGeneratorDefinition, GenericTaskGroup } from '../base/tasks.mjs';
import command from './command.mjs';
import { createRemoveUnusedImportsTransform } from './support/java-unused-imports-transform.mjs';

const { MULTISTEP_TRANSFORM, PRE_CONFLICTS } = PRIORITY_NAMES;
const { MULTISTEP_TRANSFORM_QUEUE } = QUEUES;
Expand Down Expand Up @@ -155,6 +156,7 @@ export default class BootstrapGenerator extends BaseGenerator {
? []
: [
createESLintTransform.call(this, { ignoreErrors, extensions: 'ts,js' }),
createRemoveUnusedImportsTransform.call(this, { ignoreErrors }),
createPrettierTransform.call(this, {
ignoreErrors,
prettierPackageJson: true,
Expand Down
33 changes: 33 additions & 0 deletions generators/bootstrap/support/java-unused-imports-transform.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { extname } from 'path';
import { passthrough } from '@yeoman/transform';
import { isFileStateDeleted } from 'mem-fs-editor/state';
import { removeUnusedImports } from 'java-lint';
import { VinylMemFsEditorFile } from 'mem-fs-editor';
import CoreGenerator from '../../base-core/index.mjs';

// eslint-disable-next-line import/prefer-default-export
export const createRemoveUnusedImportsTransform = function (
this: CoreGenerator,
options: {
ignoreErrors?: boolean;
} = {},
) {
const { ignoreErrors } = options;
return passthrough((file: VinylMemFsEditorFile) => {
if (extname(file.path) === '.java' && !isFileStateDeleted(file)) {
if (file.contents) {
try {
file.contents = Buffer.from(removeUnusedImports(file.contents.toString('utf8')));
} catch (error: any) {
const errorMessage = `Error parsing file ${file.relative}: ${error} at ${file.contents?.toString()}`;
if (ignoreErrors) {
this?.log?.warn?.(errorMessage);
return;
}

throw new Error(errorMessage);
}
}
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import java.util.UUID;
<%_ } _%>
/**
* Service Interface for managing {@link <% if (dtoMapstruct) { %><%= entityAbsolutePackage %>.domain.<% } %><%= persistClass %>}.
* Service Interface for managing {@link <%= entityAbsoluteClass %>}.
*/
public interface <%= entityClass %>Service {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ filterVariables.push({ filterType : oauthAwareReferenceFilterType,
});
_%>
/**
* Criteria class for the {@link <%= entityAbsolutePackage %>.domain.<%= persistClass %>} entity. This class is used
* Criteria class for the {@link <%= entityAbsoluteClass %>} entity. This class is used
* in {@link <%= entityAbsolutePackage %>.web.rest.<%= entityClass %>Resource} to receive all the possible filtering options from
* the Http GET request parameters.
* For example the following could be a valid request:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
<%_ } _%>
/**
* Service Implementation for managing {@link <%= persistClass %>}.
* Service Implementation for managing {@link <%= entityAbsoluteClass %>}.
*/
@Service<% if (databaseTypeSql) { %>
@Transactional<% } %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
<%_ } _%>
/**
* REST controller for managing {@link <%= entityAbsolutePackage %>.domain.<%= persistClass %>}.
* REST controller for managing {@link <%= entityAbsoluteClass %>}.
*/
@RestController
@RequestMapping("/api")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package <%= packageName %>.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
Expand All @@ -41,7 +40,7 @@ public @interface AuthorizedFeignClient {
* {@link feign.codec.Encoder}, {@link feign.Contract}.
*
* @return the custom {@code @Configuration} for the feign client.
* @see FeignClientsConfiguration for the defaults.
* @see org.springframework.cloud.openfeign.FeignClientsConfiguration for the defaults.
*/
@AliasFor(annotation = FeignClient.class, attribute = "configuration")
Class<?>[] configuration() default OAuth2InterceptedFeignConfiguration.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.datastax.oss.driver.api.mapper.annotations.Mapper;
import com.datastax.oss.driver.api.mapper.annotations.Select;
<%_ } _%>
import <%= packageName %>.domain.PersistentToken;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (!databaseTypeCassandra) { _%>
import java.time.LocalDate;
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package <%= packageName %>.repository;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeCassandra) { _%>
import com.datastax.oss.driver.api.core.CqlIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-%>
package <%= packageName %>.security;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-%>
package <%= packageName %>.service;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import tech.jhipster.config.JHipsterProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import <%= packageName %>.config.Constants;
import <%= packageName %>.domain.Authority;
<%_ } _%>
<%_ if (!databaseTypeNo) { _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { _%>
import <%= packageName %>.repository.AuthorityRepository;
<%_ if (authenticationTypeSession && !reactive) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import <%= packageName %>.config.Constants;
<% if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { %>
import <%= packageName %>.domain.Authority;<% } %>
<%_ if (!databaseTypeNo) { _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package <%= packageName %>.service.dto;
<%_ if (!databaseTypeNo) { _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package <%= packageName %>.service.mapper;
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.service.dto.<%= user.dtoClass %>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import <%= packageName %>.domain.PersistentToken;
import <%= packageName %>.repository.PersistentTokenRepository;
<%_ } _%>
<%_ if (!reactive) { _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
import <%= packageName %>.repository.UserRepository;
import <%= packageName %>.security.SecurityUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
-%>
package <%= packageName %>.web.rest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
Expand Down Expand Up @@ -47,7 +46,7 @@ public class LogoutResource {
* @param idToken the ID token.
* @param request a {@link ServerHttpRequest} request.
* @param session the current {@link WebSession}.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and a body with a global logout URL.
* @return status {@code 200 (OK)} and a body with a global logout URL.
*/
@PostMapping("/api/logout")
public Mono<Map<String, String>> logout(@AuthenticationPrincipal(expression = "idToken") OidcIdToken idToken, ServerHttpRequest request, WebSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ public class PublicUserResource {
/**
* {@code GET /users} : get all users with only the public informations - calling this are allowed for anyone.
*<% if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { %>
<%_ if (reactive) { _%>
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { _%>
*
<%_ if (reactive) { _%>
* @param request a {@link ServerHttpRequest} request.
<%_ } _%>
* @param pageable the pagination information.<% } %>
<%_ } _%>
* @param pageable the pagination information.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users.
<%_ } _%>
*/
@GetMapping("/users")
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package <%= packageName %>.web.rest;
import <%= packageName %>.config.Constants;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.service.MailService;
Expand Down Expand Up @@ -90,7 +90,7 @@ import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
/**
* REST controller for managing users.
* <p>
* This class accesses the {@link <%= packageName %>.domain.<%= user.persistClass %>} entity, and needs to fetch its collection of authorities.
* This class accesses the {@link <%= user.entityAbsoluteClass %>} entity, and needs to fetch its collection of authorities.
* <p>
* For a normal use-case, it would be better to have an eager relationship between User and Authority,
* and send everything to the client side: there would be no View Model and DTO, a lot less code, and an outer-join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
import org.apache.commons.lang3.RandomStringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package <%= packageName %>.service;
import <%= packageName %>.config.Constants;
import <%= packageName %>.IntegrationTest;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import tech.jhipster.config.JHipsterProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import <%= packageName %>.config.Constants;
import <%= packageName %>.domain.PersistentToken;
<%_ } _%>
<%_ if (!databaseTypeNo) { _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
<%_ if ((databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) && authenticationTypeSession && !reactive) { _%>
import <%= packageName %>.repository.PersistentTokenRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-%>
package <%= packageName %>.service.mapper;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.service.dto.<%= user.dtoClass %>;
<%_ if (!authenticationTypeOauth2) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import <%= packageName %>.config.Constants;
<%_ if (authenticationTypeSession && !reactive) { _%>
import <%= packageName %>.domain.PersistentToken;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { _%>
import <%= packageName %>.repository.AuthorityRepository;
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
<%_ } _%>
import <%= packageName %>.web.rest.vm.LoginVM;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import <%= packageName %>.config.Constants;
<%_ if (authenticationTypeOauth2) { _%>
import <%= packageName %>.config.TestSecurityConfiguration;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
<%_ if (searchEngineElasticsearch) { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import <%= packageName %>.config.Constants;
<%_ if (!databaseTypeCassandra && !databaseTypeCouchbase) { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.repository.AuthorityRepository;
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ public class CacheConfiguration {
createCache(cm, <%= packageName %>.repository.UserRepository.USERS_BY_LOGIN_CACHE);
createCache(cm, <%= packageName %>.repository.UserRepository.USERS_BY_EMAIL_CACHE);
<%_ if (enableHibernateCache) { _%>
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName());
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName());
createCache(cm, <%= packageName %>.domain.Authority.class.getName());
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName() + ".authorities");
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName() + ".authorities");
<%_ if (authenticationTypeSession) { _%>
createCache(cm, <%= packageName %>.domain.PersistentToken.class.getName());
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName() + ".persistentTokens");
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName() + ".persistentTokens");
<%_ } _%>
<%_ } _%>
<%_ } _%>
Expand Down Expand Up @@ -778,12 +778,12 @@ public class CacheConfiguration {
createCache(cm, <%= packageName %>.repository.UserRepository.USERS_BY_LOGIN_CACHE, jcacheConfiguration);
createCache(cm, <%= packageName %>.repository.UserRepository.USERS_BY_EMAIL_CACHE, jcacheConfiguration);
<%_ if (enableHibernateCache) { _%>
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName(), jcacheConfiguration);
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName(), jcacheConfiguration);
createCache(cm, <%= packageName %>.domain.Authority.class.getName(), jcacheConfiguration);
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName() + ".authorities", jcacheConfiguration);
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName() + ".authorities", jcacheConfiguration);
<%_ if (authenticationTypeSession) { _%>
createCache(cm, <%= packageName %>.domain.PersistentToken.class.getName(), jcacheConfiguration);
createCache(cm, <%= packageName %>.domain.<%= user.persistClass %>.class.getName() + ".persistentTokens", jcacheConfiguration);
createCache(cm, <%= user.entityAbsoluteClass %>.class.getName() + ".persistentTokens", jcacheConfiguration);
<%_ } _%>
<%_ } _%>
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<&_ if (fragment.staticImportSection) { -&>
<%_ if (!embedded) { _%>
import static <%= packageName %>.config.Constants.ID_DELIMITER;
import static <%= packageName %>.domain.<%= persistClass %>.TYPE_NAME;
import static <%= entityAbsoluteClass %>.TYPE_NAME;
import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.UNIQUE;
<%_ } _%>
<&_ } -&>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package <%= packageName %>.repository;

import <%= packageName %>.domain.PersistentToken;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import java.util.List;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.springframework.stereotype.Repository;
import java.util.UUID;
<%_ } _%>
import static <%= packageName %>.domain.<%= persistClass %>.TYPE_NAME;
import static <%= entityAbsoluteClass %>.TYPE_NAME;
/**
* Spring Data Couchbase repository for the <%= persistClass %> entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-%>
package <%= packageName %>.repository.search;

import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.<%= user.persistClass %>Repository;

<%_ if (reactive) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package <%= packageName %>.config.dbmigrations;
import <%= packageName %>.domain.Authority;
import <%= packageName %>.domain.<%= user.persistClass %>;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.config.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"ROLE_USER",
"ROLE_ADMIN"
],
"_class": "<%= packageName %>.domain.<%= user.persistClass %>"
"_class": "<%= user.entityAbsoluteClass %>"
}
Loading

0 comments on commit c35049c

Please sign in to comment.