-
-
Save AleksandrPikalo/bae1c6067b3a5c9079844244320d63f7 to your computer and use it in GitHub Desktop.
Serialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/saveVar.iml" filepath="$PROJECT_DIR$/saveVar.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s=Hello;i=10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
public class Run { | |
public static void main (String[] args) throws IllegalAccessException, IOException, NoSuchFieldException, InstantiationException { | |
Saver saver = new Saver(); | |
Serialize ser = new Serialize(); | |
ToFileFromFile toFileFromFile = new ToFileFromFile(); | |
saver.setS("Hello"); | |
saver.setI(10); | |
saver.setD(50.3); | |
toFileFromFile.save(ser.serialize(saver)); | |
String fromFile = toFileFromFile.read(); | |
ser.deserialize(fromFile, saver.getClass()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.*; | |
@Target(value = ElementType.FIELD) | |
@Retention(value = RetentionPolicy.RUNTIME) | |
public @interface Save { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Saver { | |
@Save | |
public String s; | |
@Save | |
private int i; | |
public double d; | |
public void setS(String s) { | |
this.s = s; | |
} | |
public void setI(int i) { | |
this.i = i; | |
} | |
public void setD(double d) { | |
this.d = d; | |
} | |
public String getS() { | |
return s; | |
} | |
public int getI() { | |
return i; | |
} | |
public double getD() { | |
return d; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileReader; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Modifier; | |
import java.security.InvalidParameterException; | |
public class Serialize { | |
public static String serialize (Object o) throws IllegalAccessException { | |
Class<?> cls = o.getClass(); | |
StringBuilder sb = new StringBuilder(); | |
Field[] fields = cls.getDeclaredFields(); | |
for (Field field : fields) { | |
if (!field.isAnnotationPresent(Save.class)) { | |
continue; | |
} | |
if (Modifier.isPrivate(field.getModifiers())) { | |
field.setAccessible(true); | |
} | |
sb.append(field.getName() + "="); | |
if (field.getType()==int.class) { | |
sb.append(field.getInt(o)); | |
} else { | |
if (field.getType() == String.class) { | |
sb.append((String) field.get(o)); | |
} else { | |
if (field.getType() == long.class) { | |
sb.append(field.getDouble(o)); | |
} | |
} | |
} | |
sb.append(";"); | |
} | |
return sb.toString(); | |
} | |
public static <T> T deserialize (String s, Class<T> cls) throws IllegalAccessException, InstantiationException, NoSuchFieldException { | |
T res = (T)cls.newInstance(); | |
String[] pairs = s.split(";"); | |
for (String pair : pairs) { | |
String[] nv = pair.split("="); | |
if (nv.length!=2) { | |
throw new InvalidParameterException(); | |
} | |
String name = nv[0]; | |
String value = nv[1]; | |
Field field = cls.getDeclaredField(name); | |
if (Modifier.isPrivate(field.getModifiers())) { | |
field.setAccessible(true); | |
if (field.isAnnotationPresent(Save.class)) { | |
if (field.getType() == int.class) { | |
field.setInt(res, Integer.parseInt(value)); | |
} else { | |
if (field.getType() == String.class) { | |
field.set(res, value); | |
} else { | |
if (field.getType() == Double.class) { | |
field.setDouble(res, Double.parseDouble(value)); | |
} | |
} | |
} | |
} | |
} | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
public class ToFileFromFile { | |
String path = "C:\\Users\\admin\\IdeaProjects\\saveVar\\src\\File"; | |
public void save (String s) throws IOException { | |
FileWriter fileWriter = new FileWriter(path); | |
try { | |
fileWriter.write(s); | |
} finally { | |
fileWriter.close(); | |
} | |
} | |
public String read () throws FileNotFoundException { | |
FileReader fileReader = new FileReader(path); | |
String res = fileReader.toString(); | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment