CHJani / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Java import manager for code generator #303

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

The way imports are managed in the generator right now is more complicated, 
inconsistent, and error-prone than it needs to be.

Here is one example in java_generator.py copied below:

   type_for_format = self._SCHEMA_TYPE_TO_JAVA_TYPE.get(json_format)
   if type_for_format:
     native_format = type_for_format
   else:
     native_format = self._SCHEMA_TYPE_TO_JAVA_TYPE.get(json_type, json_type)

...

   # Some types are not imported by default. We must explicitly import them
   if native_format == 'BigInteger':
     element.requires_imports.append('java.math.BigInteger')
   elif native_format == 'DateTime':
     element.requires_imports.append('com.google.api.client.util.DateTime')

Here's another example:

http://code.google.com/p/google-apis-client-generator/source/browse/src/googleap
is/codegen/java/default/templates/___package___/ApiClient.java.tmpl

copied below:

{% if api.dataWrapper %}
import com.google.api.client.googleapis.json.JsonCContent;
import com.google.api.client.googleapis.json.JsonCParser;
{% endif %}

Here's what we did in the GData code generator that I found very effective: 
every time you use a Java class you call 
use("com.google.api.client.googleapis.json.JsonCContent").  It automatically 
tracks all of the "extraImports" this way, and so you never have write any 
explicit import statements in your templates.  If that section of generation 
isn't called, use() is never called.  So you don't need to duplicate your 
if-logic between the code section and the import section.  It also takes care 
of other logic like determining if there is a collision between two classes and 
therefore decide when to use just the class name or a fully-qualified class 
name.

Original issue reported on code.google.com by rmis...@google.com on 27 Sep 2011 at 3:31

GoogleCodeExporter commented 9 years ago

Original comment by rmis...@google.com on 27 Sep 2011 at 3:41

GoogleCodeExporter commented 9 years ago
This issue was reported internally by yanivi@google.com

Original comment by rmis...@google.com on 27 Sep 2011 at 5:42

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 6 Oct 2011 at 4:17

GoogleCodeExporter commented 9 years ago
This has been implemented in the code generator.

Original comment by rmis...@google.com on 6 Oct 2011 at 6:01

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 7 Nov 2011 at 5:37