JaroslavTulach: /* Get the sources */ - 2020-07-22 04:15:28

Get the sources

←Older revision Revision as of 04:15, 22 July 2020
Line 30: Line 30:
<source lang="bash">
<source lang="bash">
$ cd samples/livedb
$ cd samples/livedb
-
$ ant test
+
samples/livedb$ JAVA_HOME=/jdk-11 mvn clean test
</source>
</source>

JaroslavTulach: /* Screen Cast */ - 2014-05-18 10:04:50

Screen Cast

←Older revision Revision as of 10:04, 18 May 2014
Line 5: Line 5:
This page documents my experiment showing how to do something similar in good old [[Java]]. Well, it is not old good [[Java]], but [[Java]]6. [[Java]]6 has been around for few years already, but it is common that good ideas take time. As a result we are only slowly recognizing how useful [[AnnotationProcessor]]s can be.
This page documents my experiment showing how to do something similar in good old [[Java]]. Well, it is not old good [[Java]], but [[Java]]6. [[Java]]6 has been around for few years already, but it is common that good ideas take time. As a result we are only slowly recognizing how useful [[AnnotationProcessor]]s can be.
 +
<!--
== Screen Cast ==
== Screen Cast ==
Line 12: Line 13:
Thanks [[Geertjan]] for recording this screen cast with me!
Thanks [[Geertjan]] for recording this screen cast with me!
 +
-->
== Get the sources ==
== Get the sources ==

JaroslavTulach: /* Extending IDEs */ - 2010-08-30 05:54:07

Extending IDEs

←Older revision Revision as of 05:54, 30 August 2010
Line 61: Line 61:
== Extending IDEs ==
== Extending IDEs ==
-
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of any IDE (that supports [[AnnotationProcessor]]s). As [[AnnotationProcessor]] [[API]] is part of [[JDK]]6, it is the most standard way to extend any [[Java]] IDE. For example there can be [[AnnotationProcessor]] that supports [[SQL]] syntax inside of the annotation strings.
+
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of any IDE (that supports [[AnnotationProcessor]]s). As [[AnnotationProcessor]] [[API]] is part of [[JDK]]6, it is the most standard way to extend any [[Java]] IDE. For example there can be [[AnnotationProcessor]] that supports [[SQL]] syntax inside of the [[annotation]]'s strings.
[[Image:CompletionStatic.png]]
[[Image:CompletionStatic.png]]

JaroslavTulach: ui/* Extending IDEs */ - 2010-08-29 18:48:42

ui- Extending IDEs

←Older revision Revision as of 18:48, 29 August 2010
Line 61: Line 61:
== Extending IDEs ==
== Extending IDEs ==
-
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of an IDE. In some sense [[AnnotationProcessor]] is a perfect [[API]] to extend any [[Java]] IDE. [[AnnotationProcessor]] can generate classes, resource files and can also add code completation items into your favorite IDE. As such there could even be code complation on inside of the string representing [[SQL]] statement to show you list of all available tables:
+
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of any IDE (that supports [[AnnotationProcessor]]s). As [[AnnotationProcessor]] [[API]] is part of [[JDK]]6, it is the most standard way to extend any [[Java]] IDE. For example there can be [[AnnotationProcessor]] that supports [[SQL]] syntax inside of the annotation strings.
-
<source lang="sql">
+
[[Image:CompletionStatic.png]]
-
select * from <ctrl+space>
+
-
</source>
+
-
Still you are ''just'' writing a library. You don't need to understand any IDE's [[API]]s. This is all standard [[Java]]6 and there is no need for any hacks. [[AnnotationProcessor]]s turn [[Java]] into suitable meta-language for writing [[DSL]]s.
+
However providing static syntax is not all [[AnnotationProcessor]] can do. It can generate classes, resource files and can also add dynamic code completion items. It is possible to allow the processor to connect to live [[SQL]] database and show list of existing tables as suggestions for completion inside of the string representing [[SQL]] statement:
-
Indeed, writing the [[AnnotationProcessor]] needs a bit of skills, but it is not that much complex:
+
[[Image:CompletionDynamic.png]]
 +
 
 +
Still you are ''just'' writing a library. You don't need to understand any IDE's [[API]]s. This is all standard [[Java]]6 and there is no need for any hacks. [[AnnotationProcessor]]s turn [[Java]] into suitable meta-language for writing [[DSL]]s. Here is the code that provides static and dynamic code completion:
 +
 
 +
<source lang="java" snippet="livedb.completions"/>
 +
 
 +
 
 +
The code that generates the data access class follows. Indeed, writing such code requires a bit of understanding of the [[Java]] source model, but overall it is not that complex:
<source lang="java" snippet="livedb.processor"/>
<source lang="java" snippet="livedb.processor"/>
-
Just locate all packages annotated by ''@LiveDB'', reads the annotation data and use them to generate new [[Java]] source file. The file which later be compiled by the [[JavaC]] compiler.
+
Just locate all packages annotated by ''@LiveDB'', read the annotation data and use them to generate new [[Java]] source file. The generated file willl later be compiled by the [[JavaC]] compiler.
== Philosohical Aspects ==
== Philosohical Aspects ==

JaroslavTulach: /* Philosohical Aspects */ - 2010-08-02 21:21:53

Philosohical Aspects

←Older revision Revision as of 21:21, 2 August 2010
Line 80: Line 80:
<comments/>
<comments/>
 +
 +
 +
[[Category:Video]] [[Category:APIDesignPatterns:Encapsulation]] [[Category:APIDesignPatterns]]

JaroslavTulach: /* Get the sources */ - 2010-08-02 20:51:16

Get the sources

←Older revision Revision as of 20:51, 2 August 2010
Line 18: Line 18:
<source lang="bash">
<source lang="bash">
-
$ hg clone -r livedb http://source.apidesign.org/hg/apidesign/
+
$ hg clone http://source.apidesign.org/hg/apidesign/
$ cd apidesign
$ cd apidesign
$ hg pull
$ hg pull

JaroslavTulach: /* Extending IDEs */ - 2010-07-30 17:09:07

Extending IDEs

←Older revision Revision as of 17:09, 30 July 2010
Line 61: Line 61:
== Extending IDEs ==
== Extending IDEs ==
-
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of an IDE. In some sense [[AnnotationProcessor]] is a perfect [[API]] to extend any [[Java]] IDE.
+
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of an IDE. In some sense [[AnnotationProcessor]] is a perfect [[API]] to extend any [[Java]] IDE. [[AnnotationProcessor]] can generate classes, resource files and can also add code completation items into your favorite IDE. As such there could even be code complation on inside of the string representing [[SQL]] statement to show you list of all available tables:
 +
 
 +
<source lang="sql">
 +
select * from <ctrl+space>
 +
</source>
Still you are ''just'' writing a library. You don't need to understand any IDE's [[API]]s. This is all standard [[Java]]6 and there is no need for any hacks. [[AnnotationProcessor]]s turn [[Java]] into suitable meta-language for writing [[DSL]]s.
Still you are ''just'' writing a library. You don't need to understand any IDE's [[API]]s. This is all standard [[Java]]6 and there is no need for any hacks. [[AnnotationProcessor]]s turn [[Java]] into suitable meta-language for writing [[DSL]]s.
Line 69: Line 73:
<source lang="java" snippet="livedb.processor"/>
<source lang="java" snippet="livedb.processor"/>
-
Most of the code is [[JDBC]] specific, the rest just locates all packages annotated by ''@LiveDB'', reads their data and uses them to generate new [[Java]] source file, which will be compiled later by the [[JavaC]] compiler.
+
Just locate all packages annotated by ''@LiveDB'', reads the annotation data and use them to generate new [[Java]] source file. The file which later be compiled by the [[JavaC]] compiler.
 +
 
 +
== Philosohical Aspects ==
 +
 
 +
In some sense with an annotation like ''@LiveDB'' the structure of your database becomes part of your sources. But that is good, is not it? I really hate when a truth is spelled out at two different places (e.g. in [[Hibernate]] entity classes and also in the database schema). With [[LiveDB]] you just need a lightweight compile time database (like [[Derby]]) with the same structure as your deployment database being ready. The schema of such database is your definition of truth which remains at a single place.
<comments/>
<comments/>

JaroslavTulach: /* Screen Cast */ - 2010-07-30 12:42:47

Screen Cast

←Older revision Revision as of 12:42, 30 July 2010
Line 7: Line 7:
== Screen Cast ==
== Screen Cast ==
-
As pictures speak thousands of words, you can now watch a screencast recorded to demonstrate the functionality of this project. Simply press play!
+
As picture easily expresses more than thousands of words ever could, you can now watch a screencast recorded to demonstrate the functionality of this project. Simply press play!
{{#ev:bliptv|3952631|600}}
{{#ev:bliptv|3952631|600}}

77.249.170.237: /* Screen Cast */ - 2010-07-29 18:59:23

Screen Cast

←Older revision Revision as of 18:59, 29 July 2010
Line 7: Line 7:
== Screen Cast ==
== Screen Cast ==
-
As visual impression is usually much stronger than thousands of words, you can now watch a screen cast recorded to demonstrate the functionality of this project. Just press play!
+
As pictures speak thousands of words, you can now watch a screencast recorded to demonstrate the functionality of this project. Simply press play!
{{#ev:bliptv|3952631|600}}
{{#ev:bliptv|3952631|600}}

JaroslavTulach at 16:05, 29 July 2010 - 2010-07-29 16:05:32

←Older revision Revision as of 16:05, 29 July 2010
Line 37: Line 37:
The first thing you need to do to really play with the sample is to create your own database. Maybe you have one, maybe you need to create one. If you have [[NetBeans]] IDE then one is ready for you. Just start the [[Derby]] database as explained for example at [http://netbeans.org/kb/docs/ide/java-db.html JavaDB tutorial]. Create a table called AGE with two columns NAME and AGE.
The first thing you need to do to really play with the sample is to create your own database. Maybe you have one, maybe you need to create one. If you have [[NetBeans]] IDE then one is ready for you. Just start the [[Derby]] database as explained for example at [http://netbeans.org/kb/docs/ide/java-db.html JavaDB tutorial]. Create a table called AGE with two columns NAME and AGE.
-
== Use Code Completion ==
+
Now you need to define a connection to your database. Do it by using the ''@LiveDB'' annotation:
-
Now open the ''samples/livedb'' project in [[NetBeans]] IDE 6.9. Version 6.9 is the necessary minimum, as it is the first version that provides superior support for [[AnnotationProcessor]]s.
+
<source lang="java" snippet="livedb.connection.annotation"/>
-
Open for example '''LiveDBTest.java'''. It uses class '''Age'''. That class is dynamically generated during compile time to reflect structure of the AGE table created before. But try for example code completion on:
+
As you can see this is compile time annotation, as such it needs to be processed by [[AnnotationProcessor]]. Indeed there is one in this project too, but rather than dicovering the magic, let's explore the ease of use: To access a database, just use the above given annotation on some package statement:
 +
 
 +
<source lang="java" snippet="livedb.connect"/>
 +
 
 +
Of course you need to fill in proper connection credentials, and also the name of a class (''Age'' in the above case) that you wish to use to represent result of your ''query''. Done? If so, you are ready to use the class (which almost magically appears in your package). For example, in the following code snipptet, you can refer to the ''Age.query'' method and also access the fields of the class:
 +
 
 +
<source lang="java" snippet="livedb.test"/>
 +
 
 +
If you done things correctly, you have working access to live database. Everything compiles, runs and last, but not least, your IDE (if it is any good, like [[NetBeans]] IDE 6.9), will properly understood and use the on-the-fly generated ''Age'' class. Try for example code completion on:
<source lang="java">
<source lang="java">
Line 47: Line 55:
</source>
</source>
-
You will correctly see fields representing individual rows in the t able. Moreover these fields have proper types (e.g. NAME is String, AGE is BigInteger). All of this is a result of simple use of the ''@LiveDB'' annotation in the ''package-info.java'' file:
+
You will correctly see fields representing individual rows in the t able. Moreover these fields have proper types (e.g. NAME is String, AGE is BigInteger). Feels like ''Java on Rails''? All of this is brought to you by the power of [[AnnotationProcessor]]s!
-
<source lang="java">
+
You can also modify the table structure by adding new column. As soon as you do it (and tell the IDE to refresh), you can immediatelly use the new fields in your code. Everything compiles. Btw. you need to tell the [[NetBeans]] IDE to refresh. You can either use new action ''Refresh All Metadata'' for that or you can step into ''package-info.java'', add an empty line and save the file.
-
@LiveDB(
+
-
classname="Age", password="j1", user="j1",
+
-
query="select * from APP.AGE",
+
-
url="jdbc:derby://localhost:1527/livedb"
+
-
)
+
-
package org.apidesign.livedb.example;
+
-
import org.apidesign.livedb.LiveDB;
+
== Extending IDEs ==
-
</source>
+
 
 +
The beauty of using [[AnnotationProcessor]]s is in their ability to naturally extend functionality of an IDE. In some sense [[AnnotationProcessor]] is a perfect [[API]] to extend any [[Java]] IDE.
 +
 
 +
Still you are ''just'' writing a library. You don't need to understand any IDE's [[API]]s. This is all standard [[Java]]6 and there is no need for any hacks. [[AnnotationProcessor]]s turn [[Java]] into suitable meta-language for writing [[DSL]]s.
 +
 
 +
Indeed, writing the [[AnnotationProcessor]] needs a bit of skills, but it is not that much complex:
-
== Modify the Table ==
+
<source lang="java" snippet="livedb.processor"/>
-
Now add new column to the table. Use it in the code. See if code completion sees it (probably not). If everything compiles (probably yes).
+
Most of the code is [[JDBC]] specific, the rest just locates all packages annotated by ''@LiveDB'', reads their data and uses them to generate new [[Java]] source file, which will be compiled later by the [[JavaC]] compiler.
-
tbd.
+
<comments/>