Accessing a constant from a mapped Class

View: New views
6 Messages — Rating Filter:   Alert me  

Accessing a constant from a mapped Class

by Juliano Fernandes Schroeder :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Something like it:

public class DwrMappedClass {

  static final String CONSTANT_1 = "constant1";
  static final String CONSTANT_2 = "constant2";

  public void aRemoteMethod(String param) {
    ...
  }
}

Is there a way to access these constants in javascript like this:
  var ct = DwrMappedClass.CONSTANT_1;

and not through a callback?
--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot

Re: Accessing a constant from a mapped Class

by JoeWalker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Not currently. It might make a nice addition.
You could probably write some code to reflect a class and generate JS from the public static finals, and then pass it to the browser using script session.

Joe.

On 11/13/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Something like it:

public class DwrMappedClass {

  static final String CONSTANT_1 = "constant1";
  static final String CONSTANT_2 = "constant2";

  public void aRemoteMethod(String param) {
    ...
  }
}

Is there a way to access these constants in javascript like this:
  var ct = DwrMappedClass.CONSTANT_1;

and not through a callback?
--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot


Re: Accessing a constant from a mapped Class

by Juliano Fernandes Schroeder :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Joe,
i added to DefaultRemoter the following code that adds String and Integer static finals to the js interface.
(note that i'm no king of reflection)

String init = EnginePrivate.getEngineInitScript(actualPath);
        buffer.append(init);

        buffer.append("if (" + scriptName + " == null) var " + scriptName + " = {};\n");
        buffer.append(scriptName + "._path = '" + actualPath + "';\n\n");
       
        buffer.append("//Class constants\n");       
        Field[] fields = creator.getType().getFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
           
            //Is it on the list of banned names
            if (JavascriptUtil.isReservedWord (fieldName))
            {
                continue;
            }
           
            String jsField;
            try
            {
                jsField = getFieldJS(scriptName,field);
                buffer.append(jsField);
            }
            catch (Exception ex)
            {
                buffer.append("/*Something wrong defining the static field "
                              + fieldName + ": \n");
                buffer.append("  ").append(ex.getMessage()).append("*/\n");
            }           
        }
        buffer.append("\n");

        Method[] methods = creator.getType().getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            Method method = methods[i];
            String methodName = method.getName();
           
            ...
        }
        ...
     }

     /**
     * Generates Javascript for a given public static Java field
     * @param scriptName  Name of the Javascript file, sans ".js" suffix
     * @param field Target field
     * @return Javascript constant representing the field
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    private String getFieldJS(String scriptName, Field field)
        throws Exception       
    {
        StringBuffer buffer = new StringBuffer();
               
        String fieldName = field.getName();
        buffer.append(scriptName).append(".").append(fieldName).append(" = ");
       
      
        if (field.getType().equals(String.class))
        {           
            String fieldValue = (String) field.get(null);
            buffer.append("\"").append(fieldValue).append("\";");
        }
        if (field.getType().equals(Integer.class))
        {
            Integer fieldValue = (Integer) field.get(null);
            buffer.append(fieldValue).append(";");
        }       

        buffer.append("\n");
       
        return buffer.toString();
    }


Of course this is incomplete and probably not right, at least it converts the String and Integer public static finals to javascript.
I got lazy to make it work in an inteligent way for all static fields. If you could suggest me something i'd appreciate, i'm no good with reflections.

On 11/13/06, Joe Walker < joe@...> wrote:

Not currently. It might make a nice addition.
You could probably write some code to reflect a class and generate JS from the public static finals, and then pass it to the browser using script session.

Joe.


On 11/13/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Something like it:

public class DwrMappedClass {

  static final String CONSTANT_1 = "constant1";
  static final String CONSTANT_2 = "constant2";

  public void aRemoteMethod(String param) {
    ...
  }
}

Is there a way to access these constants in javascript like this:
  var ct = DwrMappedClass.CONSTANT_1;

and not through a callback?
--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot




--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot

Re: Accessing a constant from a mapped Class

by JoeWalker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
 
Thanks for this suggestion.
I'm trying to keep things stable for a 2.0 release so I'd prefer to leave this until 2.1. I've put the code into my lisst of enhancements, and maybe we can add to it the ability to export things other than strings and ints
 
You can of course include this in your own code without having to rebuild DWR using the extensions mechanism (add an init-param with a name of the class to override, and a value of the override class name)
 
Thanks,
 
Joe

 
On 11/14/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Joe,
i added to DefaultRemoter the following code that adds String and Integer static finals to the js interface.
(note that i'm no king of reflection)

String init = EnginePrivate.getEngineInitScript(actualPath);
        buffer.append(init);

        buffer.append("if (" + scriptName + " == null) var " + scriptName + " = {};\n");
        buffer.append(scriptName + "._path = '" + actualPath + "';\n\n");
       
        buffer.append("//Class constants\n");       
        Field[] fields = creator.getType().getFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
           
            //Is it on the list of banned names
            if (JavascriptUtil.isReservedWord (fieldName))
            {
                continue;
            }
           
            String jsField;
            try
            {
                jsField = getFieldJS(scriptName,field);
                buffer.append(jsField);
            }
            catch (Exception ex)
            {
                buffer.append("/*Something wrong defining the static field "
                              + fieldName + ": \n");
                buffer.append ("  ").append(ex.getMessage()).append("*/\n");
            }           
        }
        buffer.append("\n");

        Method[] methods = creator.getType().getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            Method method = methods[i];
            String methodName = method.getName();
           
            ...
        }
        ...
     }

     /**
     * Generates Javascript for a given public static Java field
     * @param scriptName  Name of the Javascript file, sans ".js" suffix
     * @param field Target field
     * @return Javascript constant representing the field
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    private String getFieldJS(String scriptName, Field field)
        throws Exception       
    {
        StringBuffer buffer = new StringBuffer();
               
        String fieldName = field.getName();
        buffer.append(scriptName).append(".").append(fieldName).append(" = ");
       
      
        if (field.getType().equals(String.class))
        {           
            String fieldValue = (String) field.get(null);
            buffer.append("\"").append(fieldValue).append("\";");
        }
        if (field.getType().equals(Integer.class))
        {
            Integer fieldValue = (Integer) field.get(null);
            buffer.append(fieldValue).append(";");
        }       

        buffer.append("\n");
       
        return buffer.toString();
    }


Of course this is incomplete and probably not right, at least it converts the String and Integer public static finals to javascript.
I got lazy to make it work in an inteligent way for all static fields. If you could suggest me something i'd appreciate, i'm no good with reflections.


On 11/13/06, Joe Walker < joe@...> wrote:

Not currently. It might make a nice addition.
You could probably write some code to reflect a class and generate JS from the public static finals, and then pass it to the browser using script session.

Joe.


On 11/13/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Something like it:

public class DwrMappedClass {

  static final String CONSTANT_1 = "constant1";
  static final String CONSTANT_2 = "constant2";

  public void aRemoteMethod(String param) {
    ...
  }
}

Is there a way to access these constants in javascript like this:
  var ct = DwrMappedClass.CONSTANT_1;

and not through a callback?
--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot




--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot


Re: Accessing a constant from a mapped Class

by Kevin Conaway :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Did this make it into the trunk at all?  Is this still planned for a future release?

On Sun, Nov 26, 2006 at 10:42 AM, Joe Walker <joe@...> wrote:
 
Thanks for this suggestion.
I'm trying to keep things stable for a 2.0 release so I'd prefer to leave this until 2.1. I've put the code into my lisst of enhancements, and maybe we can add to it the ability to export things other than strings and ints
 
You can of course include this in your own code without having to rebuild DWR using the extensions mechanism (add an init-param with a name of the class to override, and a value of the override class name)
 
Thanks,
 
Joe

 
On 11/14/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Joe,
i added to DefaultRemoter the following code that adds String and Integer static finals to the js interface.
(note that i'm no king of reflection)

String init = EnginePrivate.getEngineInitScript(actualPath);
        buffer.append(init);

        buffer.append("if (" + scriptName + " == null) var " + scriptName + " = {};\n");
        buffer.append(scriptName + "._path = '" + actualPath + "';\n\n");
       
        buffer.append("//Class constants\n");       
        Field[] fields = creator.getType().getFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
           
            //Is it on the list of banned names
            if (JavascriptUtil.isReservedWord (fieldName))
            {
                continue;
            }
           
            String jsField;
            try
            {
                jsField = getFieldJS(scriptName,field);
                buffer.append(jsField);
            }
            catch (Exception ex)
            {
                buffer.append("/*Something wrong defining the static field "
                              + fieldName + ": \n");
                buffer.append ("  ").append(ex.getMessage()).append("*/\n");
            }           
        }
        buffer.append("\n");

        Method[] methods = creator.getType().getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            Method method = methods[i];
            String methodName = method.getName();
           
            ...
        }
        ...
     }

     /**
     * Generates Javascript for a given public static Java field
     * @param scriptName  Name of the Javascript file, sans ".js" suffix
     * @param field Target field
     * @return Javascript constant representing the field
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    private String getFieldJS(String scriptName, Field field)
        throws Exception       
    {
        StringBuffer buffer = new StringBuffer();
               
        String fieldName = field.getName();
        buffer.append(scriptName).append(".").append(fieldName).append(" = ");
       
      
        if (field.getType().equals(String.class))
        {           
            String fieldValue = (String) field.get(null);
            buffer.append("\"").append(fieldValue).append("\";");
        }
        if (field.getType().equals(Integer.class))
        {
            Integer fieldValue = (Integer) field.get(null);
            buffer.append(fieldValue).append(";");
        }       

        buffer.append("\n");
       
        return buffer.toString();
    }


Of course this is incomplete and probably not right, at least it converts the String and Integer public static finals to javascript.
I got lazy to make it work in an inteligent way for all static fields. If you could suggest me something i'd appreciate, i'm no good with reflections.


On 11/13/06, Joe Walker < joe@...> wrote:

Not currently. It might make a nice addition.
You could probably write some code to reflect a class and generate JS from the public static finals, and then pass it to the browser using script session.

Joe.


On 11/13/06, Juliano Fernandes Schroeder <juliano7s@...> wrote:
Something like it:

public class DwrMappedClass {

  static final String CONSTANT_1 = "constant1";
  static final String CONSTANT_2 = "constant2";

  public void aRemoteMethod(String param) {
    ...
  }
}

Is there a way to access these constants in javascript like this:
  var ct = DwrMappedClass.CONSTANT_1;

and not through a callback?
--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot




--
Juliano F. Schroeder
--------------------------------------------------
Plástico Bolha
http://plastico-bolha.blogspot



Re: Accessing a constant from a mapped Class

by David Marginian-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kevin,
   I just looked at the 3.x source and it doesn't look like this has
made it in yet.  I remember seeing the issue so I believe it is still
planned.

Kevin Conaway wrote:

> Did this make it into the trunk at all?  Is this still planned for a
> future release?
>
> On Sun, Nov 26, 2006 at 10:42 AM, Joe Walker <joe@...
> <mailto:joe@...>> wrote:
>
>      
>     Thanks for this suggestion.
>     I'm trying to keep things stable for a 2.0 release so I'd prefer
>     to leave this until 2.1. I've put the code into my lisst of
>     enhancements, and maybe we can add to it the ability to export
>     things other than strings and ints
>      
>     You can of course include this in your own code without having to
>     rebuild DWR using the extensions mechanism (add an init-param with
>     a name of the class to override, and a value of the override class
>     name)
>      
>     Thanks,
>      
>     Joe
>
>      
>     On 11/14/06, *Juliano Fernandes Schroeder* <juliano7s@...
>     <mailto:juliano7s@...>> wrote:
>
>         Joe,
>         i added to DefaultRemoter the following code that adds String
>         and Integer static finals to the js interface.
>         (note that i'm no king of reflection)
>
>         String init = EnginePrivate.getEngineInitScript(actualPath);
>                 buffer.append(init);
>
>                 buffer.append("if (" + scriptName + " == null) var " +
>         scriptName + " = {};\n");
>                 buffer.append(scriptName + "._path = '" + actualPath +
>         "';\n\n");
>                
>                 buffer.append("//Class constants\n");      
>                 Field[] fields = creator.getType().getFields();
>                 for (int i = 0; i < fields.length; i++) {
>                     Field field = fields[i];
>                     String fieldName = field.getName();
>                    
>                     //Is it on the list of banned names
>                     if (JavascriptUtil.isReservedWord (fieldName))
>                     {
>                         continue;
>                     }
>                    
>                     String jsField;
>                     try
>                     {
>                         jsField = getFieldJS(scriptName,field);
>                         buffer.append(jsField);
>                     }
>                     catch (Exception ex)
>                     {
>                         buffer.append("/*Something wrong defining the
>         static field "
>                                       + fieldName + ": \n");
>                         buffer.append ("
>         ").append(ex.getMessage()).append("*/\n");
>                     }          
>                 }
>                 buffer.append("\n");
>
>                 Method[] methods = creator.getType().getMethods();
>                 for (int i = 0; i < methods.length; i++)
>                 {
>                     Method method = methods[i];
>                     String methodName = method.getName();
>                    
>                     ...
>                 }
>                 ...
>              }
>
>              /**
>              * Generates Javascript for a given public static Java field
>              * @param scriptName  Name of the Javascript file, sans
>         ".js" suffix
>              * @param field Target field
>              * @return Javascript constant representing the field
>              * @throws IllegalAccessException
>              * @throws IllegalArgumentException
>              * @throws IllegalAccessException
>              * @throws IllegalArgumentException
>              */
>             private String getFieldJS(String scriptName, Field field)
>                 throws Exception      
>             {
>                 StringBuffer buffer = new StringBuffer();
>                        
>                 String fieldName = field.getName();
>                
>         buffer.append(scriptName).append(".").append(fieldName).append("
>         = ");
>                
>              
>                 if (field.getType().equals(String.class))
>                 {          
>                     String fieldValue = (String) field.get(null);
>                     buffer.append("\"").append(fieldValue).append("\";");
>                 }
>                 if (field.getType().equals(Integer.class))
>                 {
>                     Integer fieldValue = (Integer) field.get(null);
>                     buffer.append(fieldValue).append(";");
>                 }      
>
>                 buffer.append("\n");
>                
>                 return buffer.toString();
>             }
>
>
>         Of course this is incomplete and probably not right, at least
>         it converts the String and Integer public static finals to
>         javascript.
>         I got lazy to make it work in an inteligent way for all static
>         fields. If you could suggest me something i'd appreciate, i'm
>         no good with reflections.
>
>
>         On 11/13/06, *Joe Walker* < joe@...
>         <mailto:joe@...>> wrote:
>
>
>             Not currently. It might make a nice addition.
>             You could probably write some code to reflect a class and
>             generate JS from the public static finals, and then pass
>             it to the browser using script session.
>
>             Joe.
>
>
>             On 11/13/06, *Juliano Fernandes Schroeder* <
>             juliano7s@... <mailto:juliano7s@...>> wrote:
>
>                 Something like it:
>
>                 public class DwrMappedClass {
>
>                   static final String CONSTANT_1 = "constant1";
>                   static final String CONSTANT_2 = "constant2";
>
>                   public void aRemoteMethod(String param) {
>                     ...
>                   }
>                 }
>
>                 Is there a way to access these constants in javascript
>                 like this:
>                   var ct = DwrMappedClass.CONSTANT_1;
>
>                 and not through a callback?
>                 --
>                 Juliano F. Schroeder
>                 --------------------------------------------------
>                 Plástico Bolha
>                 http://plastico-bolha.blogspot
>                 <http://plastico-bolha.blogspot/>
>
>
>
>
>
>         --
>         Juliano F. Schroeder
>         --------------------------------------------------
>         Plástico Bolha
>         http://plastico-bolha.blogspot <http://plastico-bolha.blogspot/>
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

LightInTheBox - Buy quality products at wholesale price