# HG changeset patch # User František Kučera # Date 1410267068 -7200 # Node ID 627912dacae3beb89710e8e1af4632dd15fceb73 # Parent 63b496ae04f9b664582511ecb9a52ee77097e65f RAII v Javě pomocí try+AutoCloseable diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/build.xml Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project RAII. + + + diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/manifest.mf --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/manifest.mf Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/nbproject/build-impl.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/nbproject/build-impl.xml Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/nbproject/genfiles.properties Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=376d6354 +build.xml.script.CRC32=9900a8e8 +build.xml.stylesheet.CRC32=8064a381@1.74.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=376d6354 +nbproject/build-impl.xml.script.CRC32=5fb085fb +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.2.48 diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/nbproject/project.properties Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/RAII.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=cz.frantovo.priklady.raii.RAIIDemo +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/nbproject/project.xml Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + RAII + + + + + + + + + diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/Chyba.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/Chyba.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,37 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Chyba extends Exception { + + private final Zdroj zdroj; + + public Chyba(Zdroj zdroj) { + this.zdroj = zdroj; + } + + @Override + public String toString() { + return getClass().getSimpleName() + ": " + zdroj; + } + +} diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/ChybaDělání.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/ChybaDělání.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,30 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ChybaDělání extends Chyba { + + public ChybaDělání(Zdroj zdroj) { + super(zdroj); + } + +} diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/ChybaVytváření.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/ChybaVytváření.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,29 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ChybaVytváření extends Chyba { + + public ChybaVytváření(Zdroj zdroj) { + super(zdroj); + } +} diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/ChybaZavírání.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/ChybaZavírání.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,30 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ChybaZavírání extends Chyba { + + public ChybaZavírání(Zdroj zdroj) { + super(zdroj); + } + +} diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/RAIIDemo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/RAIIDemo.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,77 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class RAIIDemo { + + public static void main(String[] args) { + + /** + * Bez výjimek: + */ + try { + try (Zdroj a = new Zdroj("a", false, false, false)) { + try (Zdroj b = new Zdroj("b", false, false, false)) { + try (Zdroj c = new Zdroj("c", false, false, false)) { + a.něcoDělej(); + b.něcoDělej(); + c.něcoDělej(); + } // končí rozsah platnosti proměnné c → volá se c.close() + } // b.close() + } // a.close() + + } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) { + System.err.println(e); + } + + System.err.println("--------------------------------"); + + /** + * S výjimkami: + */ + try { + try (Zdroj a = new Zdroj("a", false, false, true)) { + try (Zdroj b = new Zdroj("b", false, false, true)) { + try (Zdroj c = new Zdroj("c", false, true, true)) { + a.něcoDělej(); + b.něcoDělej(); + c.něcoDělej(); + } + } + } + } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) { + System.err.println(e); + } + + System.gc(); // aby se GC vůbec stihl spustit + + čekej(); // aby GC stihl zavolat finalize() + + } + + private static void čekej() { + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + } + } +} diff -r 63b496ae04f9 -r 627912dacae3 java/RAII/src/cz/frantovo/priklady/raii/Zdroj.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/RAII/src/cz/frantovo/priklady/raii/Zdroj.java Tue Sep 09 14:51:08 2014 +0200 @@ -0,0 +1,70 @@ +/** + * RAII v Javě + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.priklady.raii; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Zdroj implements AutoCloseable { + + private final String název; + private final boolean chybaVytváření; + private final boolean chybaDělání; + private final boolean chybaZavírání; + + public Zdroj(String název, boolean chybaVytváření, boolean chybaDělání, boolean chybaZavírání) throws ChybaVytváření { + this.název = název; + System.err.println("Vytváříme: " + this + " chyba=" + chybaVytváření); + this.chybaVytváření = chybaVytváření; + this.chybaDělání = chybaDělání; + this.chybaZavírání = chybaZavírání; + + if (this.chybaVytváření) { + throw new ChybaVytváření(this); + } + } + + public void něcoDělej() throws ChybaDělání { + System.err.println("Něco děláme: " + this + " chyba=" + chybaDělání); + + if (chybaDělání) { + throw new ChybaDělání(this); + } + } + + @Override + public void close() throws ChybaZavírání { + System.err.println("Zavíráme: " + this + " chyba=" + chybaZavírání); + + if (chybaZavírání) { + throw new ChybaZavírání(this); + } + } + + @Override + protected void finalize() throws Throwable { + System.err.println("Finalizujeme: " + this); + } + + @Override + public String toString() { + return getClass().getSimpleName() + " [" + název + "]"; + } + +}