Last active
October 6, 2016 21:53
-
-
Save Viacheslav77/41c07d3e6f1c89c2bfb9c2f7e2ddcc7d to your computer and use it in GitHub Desktop.
Написать класс TextContainer, который содержит в себе строку. С помощью механизма аннотаций указать 1) в какой файл должен сохраниться текст 2) метод, который выполнит сохранение. Написать класс Saver,который сохранит объект класса TextContainer. @saveto(“c:\\file.txt”) class Container { … @saver public void 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
'My text 3' was saved in the file using annotations. | |
'My text 5' was saved in the file using annotations. | |
'My text 2' was saved in the file using annotations. |
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
package TextConteiner; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
@SaveTo(nameFile = "d:\\1\\file.txt") | |
public class Container { | |
protected String myText; | |
public Container (String myText){ | |
this.myText = myText ; | |
} | |
public String getMyText (){ | |
return myText; | |
} | |
public void save(String nameFile) throws IOException{ | |
try (FileWriter fw = new FileWriter(nameFile)) { | |
fw.write(myText); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
System.out.println("The file is saved " + nameFile); | |
} | |
} |
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
package TextConteiner; | |
public class Container1 extends Container{ | |
public Container1(String myText) { | |
super(myText); | |
} | |
} |
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
package TextConteiner; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
@SaveTo(nameFile = "d:\\1\\file.txt") | |
public class Container2 extends Container{ | |
public Container2(String myText) { | |
super(myText); | |
} | |
@Saver | |
public void saveWithAnnotation(String nameFile) throws IOException{ | |
try (FileWriter fw = new FileWriter(nameFile)) { | |
fw.write(myText); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
System.out.println( "'" + myText + "'" +" was saved in the file using annotations."); | |
} | |
} |
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
package TextConteiner; | |
/* Написать класс TextContainer, который содержит в себе строку. С помощью механизма аннотаций указать | |
1) в какой файл должен сохраниться текст | |
2) метод, который выполнит сохранение. | |
Написать класс Saver,который сохранит объект класса TextContainer. | |
@SaveTo(“c:\\file.txt”) | |
class Container { | |
… | |
@Saver | |
public void save(..) {…} | |
}*/ | |
public class Main { | |
public static void main (String[] args){ | |
Container[] list = { new Container ("My text"), | |
new Container2 ("My text 3"), | |
new Container ("My text 4"), | |
new Container2 ("My text 5"), | |
new Container1 ("My text 1"), | |
new Container2 ("My text 2") | |
}; | |
for (Container c: list) | |
(new SaveWithAnnotation()).findSave(c); | |
} | |
} |
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
package TextConteiner; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(value=ElementType.METHOD) | |
@Retention(value= RetentionPolicy.RUNTIME) | |
public @interface Saver { | |
} |
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
package TextConteiner; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(value=ElementType.TYPE) | |
@Retention(value= RetentionPolicy.RUNTIME) | |
public @interface SaveTo { | |
String nameFile(); | |
} |
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
package TextConteiner; | |
import java.lang.reflect.Method; | |
public class SaveWithAnnotation { | |
public void findSave(Container c){ | |
Class <?> cls = c.getClass(); | |
if (cls.isAnnotationPresent(SaveTo.class)){ | |
SaveTo st = (SaveTo) cls.getAnnotation(SaveTo.class); | |
try { | |
Method[] methods = cls.getDeclaredMethods(); | |
for (Method method : methods) { | |
if (method.isAnnotationPresent(Saver.class)){ | |
method.invoke(c, st.nameFile()); | |
} | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment