Compile-time Access to local context: ver 1.1
rssh at gradsoft.com.ua
rssh at gradsoft.com.ua
Tue Mar 24 16:38:11 PDT 2009
Already rejected, but to "finish gelshtat." Sorry,
OVERVIEW:
FEATURE SUMMARY:
Add API to access compile-time local context of currenct compiled element
from programs.
MAJOR ADVANTAGE:
Now compile time properties of local context are unaccessible for
programmer. This meaning that some functionality (such as access to
location of file and line) is accessible only from debug version of program
with technique, which is not officially supported, some (such as binding
context in current location) is not available at all. some (such as time of
compilation) can be accessibla via annotation processor, but direct access
is more natural.
MAJOR DISADVANTAGE
Inaccurate usage of this feature can cause producing of unmaintable code.
ALTERNATIVES:
External preprocessing of java sources.
EXAMPLES
SIMPLE EXAMPLE:
Example 1
if (traceCondigion) {
log.trace(Compiletime.getFileName()+":"+
Compiletime.getLineNumber());
}
Example 2
if (system.currentTimeMillis() - Compiletime.getTimeMillis() > 86400) {
System.out.println("This file was compiled more than day ago");
}
ADVANCED EXAMPLE:
Example 1
// assuming that multiline strings are implemented:
int a=0;
int b=1;
Binding bindings = Compiletime.getInstance().getCurrentBindings();
ScriptEngine velocity = ScriptEngineManager.get("velocity");
try {
String fname = Compiletime.getFileName();
int line = Compiletime.getLineNumber();
String s = velocity.eval("""
#if ($a==$b)
Something interesting here may be,
Are you know that value of b is $b ?
#else
no mistics here.
#end
""",
bindings);
} catch (ScriptException ex){
Sytem.err.println("error in inline velocity in "+fname+", "
"line "+line+1+ex.getLineNumber());
}
DETAILS:
Add to Java Library pseudo-objects java.lang.Compiletime with access to
compile-time properties and next signature:
public class Compiletime
{
/**
* get filename of compiled call.
* in case of generated source and avaible JSR-45 SMAP file
* return file name of translated-source.
**/
public static String getFileName();
/**
* get line number of compiled call.
* in case of generated source and avaible JSR-45 SMAP file
* return line number in translated-source.
**/
public static int getLineNumber();
/**
* get class name where this call is placed.
* in case of generated source and avaible JSR-45 SMAP file
* return class name in translated-source.
**/
public static int getClassName();
/**
* get method name where this call is placed.
* in case of generated source and avaible JSR-45 SMAP file
* return method name in translated-source.
**/
public static int getMethodName();
/**
* generate JSR223 bindings for given names in current compilation
*context.
*Names array must be empty or consists from string literals.
**/
public static Bindings getBindings(String ... names)
/**
* get time of compilation in miliseconds.
**/
public static long getTimeMillis();
}
COMPILATION:
During compilation calls to compile time are changed to generation of
appriative constant expressions.
String x = Compiletime.getFileName();
changed to
String x = "com/mycompany/package/MyClass.java";
int x = Compiletime.getLinNumber();
changed to
int x = 33;
String x = getClassName()
changed to
String x = "com.mycompany.package.MyClass";
class X
{
int p1;
String p2;
...
public void doSomething(int x, int y)
{
int local = x+y;
Bindings bindings = Compiletime.getBindings();
evalSomeScript(bindings);
}
}
will translated to
will translated to
class X
{
int p1;
String p2;
...
public void doSomething(int x, int y)
{
int local = x+y;
Binding binding=(new CompileTime.Binding().build(
"this",this,
"p1",p1,
"p2",p2,
"x",x,
"y",y
"local",local));
evalSomeScript(bindings);
}
}
JLS changes: current scope of JLS is not touched.
Btw, may be add to JLS chapter with name 'Compilation process' where
describe:
- high level description of transforming source code to JVM classes.
- process of automatic loading of annotation processors and when ones are
used.
- this API.
TESTING:
Special cases are:
* compability with JSR45
LIBRARY SUPPORT:
Adding one classes for Compiletime.Bindings implementation to standart
library.
REFLECTIVE APIS: None
OTHER CHANGES: None
MIGRATION: None
COMPABILITY
None
REFERENCES
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4411102
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6439965
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4649007
IMPLEMENTATION URL (optional)
None.
More information about the coin-dev
mailing list