Mapper added

This commit is contained in:
Michael Wain 2025-01-09 20:22:51 +03:00
parent 56a9a46bbe
commit 2fc08152b1
3 changed files with 48 additions and 1 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.alterdekim.game</groupId>
<artifactId>actionScriptDecompiler</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<packaging>jar</packaging>
<name>SWFDissect</name>

View File

@ -0,0 +1,12 @@
package com.alterdekim.flash.decompiler.mapper;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FlashField {
String name() default "";
}

View File

@ -0,0 +1,35 @@
package com.alterdekim.flash.decompiler.mapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;
@Slf4j
public class FlashMapper {
public static <T> T mapToObj(Map<String, Object> m, Class<T> obj) {
try {
T result = obj.getDeclaredConstructor().newInstance();
Arrays.stream(obj.getDeclaredFields())
.filter(f -> m.containsKey(f.getName()) || (f.isAnnotationPresent(FlashField.class) && m.containsKey(f.getAnnotation(FlashField.class).name())))
.map(f -> Pair.of(f, f.isAnnotationPresent(FlashField.class) ? f.getAnnotation(FlashField.class).name() : f.getName()))
.forEach(p -> {
try {
Field f = p.getLeft();
String name = p.getRight();
f.setAccessible(true);
f.set(result, m.get(name));
} catch (IllegalAccessException e) {
log.error("FlashMapper valueSet error: {}", e.getMessage());
}
});
return result;
} catch (Exception e) {
log.error("FlashMapper error: {}", e.getMessage());
}
return null;
}
}