Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active October 6, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viacheslav77/41c07d3e6f1c89c2bfb9c2f7e2ddcc7d to your computer and use it in GitHub Desktop.
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(..) {…}
'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.
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);
}
}
package TextConteiner;
public class Container1 extends Container{
public Container1(String myText) {
super(myText);
}
}
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.");
}
}
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);
}
}
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 {
}
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();
}
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