Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352409

Summary: Imports in the code generated by Xtext should be optimized to avoid build errors
Product: [Modeling] TMF Reporter: Ralph Mayr <ralph.mayr>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: sebastian.zarnekow
Version: unspecifiedFlags: sebastian.zarnekow: indigo+
Target Milestone: SR1   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
proposed fix removing the unused import sven.efftinge: iplog+

Description Ralph Mayr CLA 2011-07-19 04:03:13 EDT
Build Identifier: 20110615-0604

The class Internal<DSL>Parser, which gets generated in the UI-Plug-In of Xtext projects contains some strange imports, like org.eclipse.xtext.parsetree.*; When building this Plug-In via Command Line (javac –classpath…), the import of the empty package cannot be resolved:

src-gen\org\example\mydsl\ui\contentassist\antlr\internal\InternalMyDSLParser.java:7: package org.eclipse.xtext.parsetree does not exist
import org.eclipse.xtext.parsetree.*;
^
1 error

After manually optimizing Imports in InternalMyDSLParser.java, building works fine. Therefore I would consider it a good idea to automatically optimize Imports of the generated Code. As far as I know, EMF and GMF do so by default.


Reproducible: Always
Comment 1 Sebastian Zarnekow CLA 2011-07-19 04:08:39 EDT
Hi Ralph,

I'm pretty sure your installation in inconsistent. The generator does not match the other Xtext plugins. The code generated by Xtext is compile clean.

Please double check the versions of the installed plugins and make sure that you only have one version of Xtext installed.

Feel free to reopen the bug if I missed your point.

The import section of a correctly generated parser looks like this:

import org.eclipse.xtext.*;
import org.eclipse.xtext.parser.*;
import org.eclipse.xtext.parser.impl.*;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser;
import org.eclipse.xtext.parser.antlr.XtextTokenStream;
import org.eclipse.xtext.parser.antlr.XtextTokenStream.HiddenTokens;
import org.eclipse.xtext.parser.antlr.AntlrDatatypeRuleToken;
import <YourPackage>.<YourLang>GrammarAccess;



import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
Comment 2 Ralph Mayr CLA 2011-07-21 00:48:39 EDT
Hi, thanks for the quick response!

The difference in the imports seems to be caused by the generator fragment:

Using parser.antlr.XtextAntlrUiGeneratorFragment, i get the following imports in the Internal<DSL>Parser, which leads to the problem described:

import java.io.InputStream;
import org.eclipse.xtext.*;
import org.eclipse.xtext.parser.*;
import org.eclipse.xtext.parser.impl.*;
import org.eclipse.xtext.parsetree.*;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.parser.antlr.XtextTokenStream;
import org.eclipse.xtext.parser.antlr.XtextTokenStream.HiddenTokens;
import org.eclipse.xtext.ui.editor.contentassist.antlr.internal.AbstractInternalContentAssistParser;
import org.eclipse.xtext.ui.editor.contentassist.antlr.internal.DFA;
import <...>.<DSL>GrammarAccess;

import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

Using the org.eclipse.xtext.generator.parser.antlr.ex.rt.AntlrGeneratorFragment on the other hand, i get the imports you described:

import org.eclipse.xtext.*;
import org.eclipse.xtext.parser.*;
import org.eclipse.xtext.parser.impl.*;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser;
import org.eclipse.xtext.parser.antlr.XtextTokenStream;
import org.eclipse.xtext.parser.antlr.XtextTokenStream.HiddenTokens;
import org.eclipse.xtext.parser.antlr.AntlrDatatypeRuleToken;
import <...>.<DSL>GrammarAccess;

import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

Unfortunately, when using the org.eclipse.xtext.generator.parser.antlr.ex.rt.AntlrGeneratorFragment, content assist does generally not seem to work for my languge...
Comment 3 Sebastian Zarnekow CLA 2011-07-21 00:58:00 EDT
Thanks for the clarification.
Comment 4 Ralph Mayr CLA 2011-07-21 01:25:52 EDT
Created attachment 200050 [details]
proposed fix removing the unused import

The attached fix removes the unnecessary import from the generator template.
Comment 5 Sebastian Zarnekow CLA 2011-07-21 12:22:11 EDT
Thanks for the report. Pushed fix to master for SR1.
Comment 6 Karsten Thoms CLA 2017-09-19 17:44:14 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 7 Karsten Thoms CLA 2017-09-19 17:55:30 EDT
Closing all bugs that were set to RESOLVED before Neon.0