adapterClassName、mapAdapterClassName、autoMapperConfigClassName、autoMapMapperConfigClassName 默认生成规则增加自增后缀

This commit is contained in:
linpeilie 2024-04-01 20:04:21 +08:00
parent 17b94de8d7
commit bc6a6be078
3 changed files with 84 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package io.github.linpeilie.processor;
import io.github.linpeilie.processor.utils.FileUtils;
import io.github.linpeilie.processor.utils.IncrementMarkUtils;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.ReportingPolicy;
@ -16,7 +18,8 @@ public class AutoMapperProperties {
private static NullValueMappingStrategy nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL;
private static NullValuePropertyMappingStrategy nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_NULL;
private static NullValuePropertyMappingStrategy nullValuePropertyMappingStrategy =
NullValuePropertyMappingStrategy.SET_TO_NULL;
private static String buildMethod = "build";
@ -34,6 +37,15 @@ public class AutoMapperProperties {
private static String autoMapMapperConfigClassName = ContextConstants.AutoConfig.autoMapMapperConfigClassName;
static {
// load increment mark
Integer mark = IncrementMarkUtils.incrementAndGet();
adapterClassName = adapterClassName + "$" + mark;
mapAdapterClassName = mapAdapterClassName + "$" + mark;
autoMapperConfigClassName = autoMapperConfigClassName + "$" + mark;
autoMapMapperConfigClassName = autoMapMapperConfigClassName + "$" + mark;
}
/* ******************** getter/setter ******************** */
public static String getMapperPackage() {

View File

@ -1,5 +1,6 @@
package io.github.linpeilie.processor;
import java.io.File;
import org.mapstruct.MappingConstants;
/**
@ -94,6 +95,10 @@ public interface ContextConstants {
String enumMappers = "enumMappers";
}
interface AutoIncrementFile {
String file = System.getProperty("user.home") + File.separator + ".msp" + File.separator + "incrementMark";
}
interface Map {
String packageName = "java.util";
String className = "Map";

View File

@ -0,0 +1,66 @@
package io.github.linpeilie.processor.utils;
import io.github.linpeilie.processor.ContextConstants;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class IncrementMarkUtils {
public static Integer incrementAndGet() {
int mark = 0;
RandomAccessFile randomAccessFile = null;
FileChannel channel = null;
FileLock lock = null;
try {
File file = FileUtils.touch(new File(ContextConstants.AutoIncrementFile.file));
randomAccessFile = new RandomAccessFile(file, "rw");
channel = randomAccessFile.getChannel();
// 获取文件锁
lock = channel.lock();
String line = randomAccessFile.readLine();
if (line != null && !line.isEmpty()) {
mark = Integer.parseInt(line);
mark ++;
}
randomAccessFile.setLength(0);
randomAccessFile.seek(0);
randomAccessFile.writeBytes(String.valueOf(mark));
return mark;
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (lock != null && lock.isValid()) {
try {
lock.release();
} catch (IOException e) {
// ignore
}
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
// ignore
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
// ignore
}
}
}
}
}