Bug 71460 - [model] Non *.java file association with Java contents.
Summary: [model] Non *.java file association with Java contents.
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 enhancement with 4 votes (vote)
Target Milestone: 3.2 M4   Edit
Assignee: Jerome Lanneluc CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 89266 (view as bug list)
Depends on: 89977
Blocks:
  Show dependency tree
 
Reported: 2004-08-05 09:26 EDT by Alex Smirnoff CLA
Modified: 2005-12-13 07:05 EST (History)
20 users (show)

See Also:


Attachments
Patch fixes some hardcoded dependency on *.java extension (738 bytes, patch)
2005-02-03 11:51 EST, Alex Smirnoff CLA
no flags Details | Diff
Another patch to fix hardcoded dependecy on java extension (1.83 KB, patch)
2005-02-04 10:49 EST, Alex Smirnoff CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Smirnoff CLA 2004-08-05 09:26:26 EDT
Enhancement request for 3.1 stream. 

Files with non *.java extension, but with normal Java contents should also be 
treated as normal Java code, when associated with Java type editor. All core 
functionality (like code outline, automated builds, code completion, etc.) 
should also be available, not only source syntax highlight.

I'm trying to make Eclipse based project with Apache Beehive. All JPF, JCS, 
JCX, etc. files are normal Java files with JSR 175 annotations. I would like to 
preserve the compatability with Beehive defined extensions for the future, 
while working normally on Java code.

Thanks,
Alex.
Comment 1 ilias CLA 2004-09-21 13:00:49 EDT
see newsgroup:

Re: Any idea about feature request id=71460
http://www.eclipse.org/newsportal/article.php?id=11919&group=eclipse.tools.jdt
Comment 2 Philipe Mulet CLA 2004-09-22 09:42:38 EDT
Will conduct preliminary investigations before committing.
Comment 3 Edoardo Comar CLA 2004-09-30 11:18:16 EDT
I can't wait for this enhancement ... 
Comment 4 Alex Smirnoff CLA 2005-01-28 15:24:00 EST
I'm trying to reuse org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor
using editor extension like but to set different file extension:

<extension point="org.eclipse.ui.editors">
    <editor
         name="Java Editor"
         default="true"
         icon="icons/full/etool16/beehive.gif"
         extensions="jcs,jcx,jws"
        
contributorClass="org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditorActionContributor"
         class="org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor"
         symbolicFontName="org.eclipse.jdt.ui.editors.textfont"
         id="org.eclipse.pollinate.editors.CompilationUnitEditor" /> 
</extension>

But with no much luck: only coloring scheme is working fine. Is there any hack I
 may use to make this extension  work more like a Java Editor? What is the
status of this bug?

Any tip will be appreciated.
Thanks,
Alex.
Comment 5 Alex Smirnoff CLA 2005-01-30 01:36:05 EST
I tried to make very obvious/naive hack to allow non .java extensions files with
Java content to be treated as normal Java files and I was partially succeeded.
The idea was to set public static
org.eclipse.jdt.internal.core.util.Util.JAVA_LIKE_EXTENSIONS array to desired
extensions. I've created a startup extension to do the work:

public class ChangeExtensions implements IStartup {

    public final static char[] SUFFIX_jcs = ".jcs".toCharArray();
    public final static char[] SUFFIX_jcx = ".jcx".toCharArray();
    public final static char[] SUFFIX_jws = ".jws".toCharArray();
   
    /* (non-Javadoc)
     * @see org.eclipse.ui.IStartup#earlyStartup()
     */
    public void earlyStartup() {
        Util.JAVA_LIKE_EXTENSIONS = new char[][] {
                SuffixConstants.SUFFIX_java,
                SUFFIX_jcs,
                SUFFIX_jcx,
                SUFFIX_jws };
    }
}

And I've used simple file to test (Test1.jcs):

package pkg;
public class Test1 {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

The hack was working with this code: I was able to get most of actions that were
missing before and even the builder was able to compile it into class file.
However, there was a problem report in the editor (if I've tried to change
something) saying that Test1 class must be defined in it's own file (there was
no such problem report in the problems view and no problem marker in package
explorer, only editor). Also the editor lost most of the syntax highlight and
package explorer changed the icon to Java default icon.

I've looked back into the Util class and found the "defaultJavaExtension"
method. This method obviously a source for trouble in this case. I don't know if
there are any hardcoded dependencies on java file extension...

I hope this helps.
-alex
Comment 6 Alex Smirnoff CLA 2005-02-03 11:51:50 EST
Created attachment 17658 [details]
Patch fixes some hardcoded dependency on *.java extension

Continuing my hacking effort to support non *.java extensions with JavaEditors
this patch provide some tiny, but healthy fix in CompilationUnit class.
Comment 7 Alex Smirnoff CLA 2005-02-04 10:49:36 EST
Created attachment 17686 [details]
Another patch to fix hardcoded dependecy on java extension

This tiny patch supplement the fix of hardcoded dependency on ".java" extension
in JDT Core.

In fact I've made brutal text search on ".java" and find out *a lot* of
hardcoded ".java" constants in UI, debug, and other non-core plugins. It looks
like it will be not that simple to make this enhancement...
Comment 8 Jerome Lanneluc CLA 2005-03-15 12:09:49 EST
Thanks Alex for your effort. I just committed changes that remove all hardcoded
dependency on .java extension in org.eclipse.jdt.core. You should enter separate
bugs against the other plugins that hardcode this dependency.
Comment 9 Alex Smirnoff CLA 2005-03-17 15:22:03 EST
Jerome,

Is Util.JAVA_LIKE_EXTENSIONS a correct way to contribute new extesions to the model?

There would be a need to define the contribution pattern somewhere.

Thanks,
Alex.
Comment 10 Jerome Lanneluc CLA 2005-03-23 09:05:27 EST
No, Util.JAVA_LIKE_EXTENSIONS is not the correct way to contribute new file
extensions. 

I changed it to private and added an extension point
(extraJavaLikeFileExtensions) to add other Java-like file extensions to JDT Core.

The schema of this extension point is defined in
schema/extraJavaLikeFileExtensions.exsd. An example is as follows:
<extension point="org.eclipse.jdt.core.extraJavaLikeFileExtensions">            
   <extraJavaLikeFile extension="foo"/>
   <extraJavaLikeFile extension="bar"/>
</extension>
Comment 11 David Williams CLA 2005-03-23 16:18:00 EST
I've re-opened, just to request reasoning be documented, if not changed. 
Why provide a new extension point for this, instead of using platform's 
contentType extension point? Whouldn't the contentType allow more consistent 
treatment (consistent accross platform, that is, for example, if/when there was 
a mechanism for users to influence the defintion of a contentType, associate an 
editor with it, etc).?
Comment 12 David Williams CLA 2005-03-23 16:18:12 EST
I've re-opened, just to request reasoning be documented, if not changed. 
Why provide a new extension point for this, instead of using platform's 
contentType extension point? Whouldn't the contentType allow more consistent 
treatment (consistent accross platform, that is, for example, if/when there was 
a mechanism for users to influence the defintion of a contentType, associate an 
editor with it, etc).?
Comment 13 Philipe Mulet CLA 2005-03-23 16:38:02 EST
Interesting idea, how would you imagine clients to contribute their custom
Java-like extensions ?

for instance, this is our contribution for "java" files.

	<content-type id="javaSource" name="%javaSourceName" 
		base-type="org.eclipse.core.runtime.text"
		priority="high"				
		file-extensions="java"/>

Do you imagine some extra attribute, where clients would specify they want it
contributed as a java-like contents as well ? 
Comment 14 David Williams CLA 2005-03-25 17:55:17 EST
Part One: the easiest way

If clients just wanted to add a file extension to what's considered 'java', the
following should work:
<extension point="org.eclipse.core.runtime.contentTypes">
    <file-association 
        content-type="org.eclipse.jdt.core.javaSource"
        file-extensions="jav,javaAlso"/>
</extension>

The implementations that currently look for java file extension would have to be
changed to look at the resource's contentType
(id.equals("org.eclipse.jdt.core.javaSource")

the contentType framework accumulates contributed file extensions (doesn't
replace them) 
Comment 15 David Williams CLA 2005-03-25 18:04:12 EST
Part Two: slightly more sophisticated

If the implementation was changed to instead of looking for literal java content
type id, it got the java contentType object (from content type manager) and then
got the resource's content type, it could use 'isKindOf' to see if the resource
was extending java -- the assumptioning being that all the base java stuff
should still work, but client's code might do something special (like aspect J
looks into comments to get special info). 

Clients would then indicate this extended java file type using following type of 
extension, indicating that files with "javaPlus" extension should be treated
as java (but their own add-on function could detect 'javaPlus' specifically to
do its special thing). 

 <extension
          point="org.eclipse.core.runtime.contentTypes">
          <content-type
                priority="medium"
                name="Java Plus Content"
                id="javaPlusSource"
                base-type="org.eclipse.jdt.core.javaSource"
                file-extensions="javaPlus"/>
 </extension>
 
Comment 16 David Williams CLA 2005-03-25 18:21:44 EST
I could add ... at one time it was suggested the Eclipse 3.1 theme of "Design
for extensibility: Be a Better Platform" explicitly mentioned that "content
types should be implemented consistently through out the base" but seems it was
omitted in final version -- probably because writers didn't know know what it
meant :)
Comment 17 Alex Smirnoff CLA 2005-03-28 14:49:54 EST
I've added bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=89266 for UI plugin.
Comment 18 Rafael Chaves CLA 2005-03-28 16:55:14 EST
Re: comment 14 - what David suggests would be the recommended approach for
supporting *regular* Java source files with file names different than *.java.
Clients interested in Java source files could then obtain the corresponding
content type (org.eclipse.jdt.core.javaSource) and for every file of interest do:

if (!javaSourceFile.isAssociatedWith(fileName))
  //ignore, name does not match Java source file
  return;
// do Java-only related work

That would allow Java source files to have any name, and does very little work
(file name matching only).
Comment 19 Rafael Chaves CLA 2005-03-28 17:28:07 EST
Re: comment 15

Making a content type B derived from an existing content type A only makes sense
if: 
i) all occurrences of the derived content type (B) are valid occurrences of the
base content type (A), and
ii) not all occurrences of the base type (A) are valid occurrences of the
derived type (b).

If only i) is true, B is actually A, so no need for another content type, just
add new file patterns to A.
If only ii) is true, then B should not be derived from A. It may be similar but
is actually an unrelated content type (for example - AspectJ is not an eligible
derived type of Java source - it is actually the other way around).

If both are true, Dave's suggestion is correct. Clients looking for Java source
files could do:

IContentType javaSource = Platform.getContentTypeManager()
  .getContentType("org.eclipse.jdt.core.javaSource");
IFile file = ...;
IContentDescription description = file.getContentDescription();
if (description == null || !description.getContentType().isKindOf(javaSource)) 
  // not a valid Java file
  return;
// process Java and special (but backward compatible) Java files

That being said, if all that is wanted here is a common way of handling the Java
family (AspectJ, SQLJ, Java, JSP), then I would say you do need an extension
point for that, but would strongly recommend basing such extension point on
content type ids, instead of file names. This exactly what we are trying to
avoid with the content type framework. All file associations should occur
through content type assocations.

Sorry for talking about so many options, but I do not know what the exact
requirements are here, so I thought I should at least be as informative as possible.
Comment 20 Dani Megert CLA 2005-03-29 07:17:08 EST
*** Bug 89266 has been marked as a duplicate of this bug. ***
Comment 21 Jerome Lanneluc CLA 2005-03-30 10:10:50 EST
Thanks David and Rafael for the explanation.

I confirm that we want to solve the problem described in comment #14 (i.e.
supporting *regular* Java source files with file names different than *.java).

The other problem described in comment #15 is not part of the original request.
Moreoever JDT Core is not ready to support files with contents other than pure
Java. So David please feel free to add a bug report requesting that, since it is
a much harder problem, we won't be able to do it soon.
Comment 22 David Williams CLA 2005-03-30 10:50:14 EST
Thanks for the comments. Actually, I don't have a specific need at the moment, 
I was just trying to highlight the advantages of moving to 'contentType' based 
approach. You could evolve in future. 

Do your remarks mean you'll provide the solution suggested in #14, instead of 
introducing a new file extension based extension point. (If so, great, and thanks). 
Comment 23 Jerome Lanneluc CLA 2005-03-30 11:34:16 EST
You guessed right :-)

Removed the extraJavaLikeFileExtensions extension point. 
Changed Util.getJavaLikeExtensions() to use the content type manager and
retrieve the file extensions for the "org.eclipse.jdt.core.javaSource" content type.
Updated the tests to use the "org.eclipse.core.runtime.contentTypes" extension.
Comment 24 Olivier Thomann CLA 2005-03-30 17:05:39 EST
Verified in I20050330-0500
Comment 25 Rafael Chaves CLA 2005-03-30 17:44:17 EST
I20050330

I was expecting the Java builder to honor the Java source file content type
settings, but that does not seem to work, no class files are generated for a
Java source file having a non .java extension. Am I missing something?
Comment 26 Jerome Lanneluc CLA 2005-03-31 04:14:41 EST
Rafael, I just noticed that you could dynamicaly update the content-type through
the preferences page (hidden in the editor preferences (bug 89787)). Is it the
way you added a Java file extension ? 

My understanding was that content-types were statically set. It looks like I was
wrong. The current implementation caches the Java file extensions on start up.
(I'm not sure we can afford to dynamically look them up).
Comment 27 Jerome Lanneluc CLA 2005-03-31 05:54:45 EST
JDT UI is not ready for this feature yet: they hardcode the .java file extension
in many places, they need more time to remove this dependency and test it. So
this feature cannot go into 3.1 M6.

Reopening and disabling the support in JDT Core for now. Changed
org.eclipse.jdt.internal.core.Util#getJavaLikeExtensions() to just return the
".java" extension and disabled JavaProjectTests#testExtraJavaLikeExtension1()
and testExtraJavaLikeExtension2().
Comment 28 Jerome Lanneluc CLA 2005-03-31 06:18:38 EST
Rafael, looking at IContentTypeChangeListener, I noticed the following note:
* Clients who reference the <code>org.eclipse.core.resources</code>
* bundle are encouraged <em>not</em> to use this listener mechanism to
* listen to content type changes. The Core Resources bundle will 
* propagate changes to content types and notify clients appropriately
* via the resource change mechanism.
However, looking at IResourceDelta, I don't see any flag indicating that the
content-type has changed. How am I supposed to know that an .abc file is now a
Java source file ?
Comment 29 Rafael Chaves CLA 2005-03-31 10:51:11 EST
Re: comment 26 - yes, they can be changed by the user. 

>The current implementation caches the Java file extensions on start up.
>(I'm not sure we can afford to dynamically look them up).

I am not aware of all your requirements, but couldn't you cache for a file scan
operation only? In what scenarios are you concerned about the performance of
matching a file name against the Java content type?
Comment 30 Rafael Chaves CLA 2005-03-31 11:34:04 EST
Re: comment 28 - please disregard that blurb, it is bogus. I am going to fix
that. Anyway, what it should be saying is "Clients who reference the
org.eclipse.core.resources bundle are encouraged not to use this listener
mechanism to detect encoding changes. The Core Resources bundle will detect
changes to content types and notify clients appropriately of potential changes
to the encoding of files in the workspace via the resource change mechanism."
Comment 31 David Williams CLA 2005-03-31 11:42:12 EST
So ... (If I may request elaboration), IContentTypeChangeListener is the way to
detect changes in content type itself? (Just not encoding). 
Comment 32 Rafael Chaves CLA 2005-03-31 11:54:10 EST
Yes. That API was added late during the 3.0 cycle in the context of bug 59899.
The event itself does not tell you about what exactly changed, only that user
settings for that content type changed. Dynamic addition/removal of content
types (by adding/removing plug-ins dinamically) are *not* reported.
Comment 33 Alex Smirnoff CLA 2005-04-24 11:39:55 EDT
Jerome,

Can you enable the fixes for JDT Core before 3.1 ships. As of my understanding
this should not brake other functionality (that based on *.java extensions). But
this will allows us start testing and reporting problems for non-java part sooner.

Thanks,
Alex.
Comment 34 Philipe Mulet CLA 2005-04-25 06:26:19 EDT
We have been debating whether we should include into M7 timeframe some support
for non-"java" file extensions. In brief, given this item is not committed and
in agreement with PMC, we are proposing to defer it post 3.1.

We believe it could jeopardize our 3.1 schedule, since it is quite a big piece
of work to complete. As of today, JDT/Core has (disabled) support for non-"java"
file extensions, in a static way (i.e. relying on content type extensions in
plugin.xml). We don't support dynamically changing them (this is quite a big
amount of work to get there, since we need to broadcast changes to clients when
tons of resources are suddenly changing their Java nature. And we get no support
from platform there). On JDT/UI & Debug, there is no support at all implemented
today; and there are significant algorithms to rework in order to get there.
Also testing will be involving, since we have no client for it either.

We want to keep room for addressing issues near 1.5 support, access restrictions
and performance (plus normal bug fixing activity).

Comment 35 Dirk Baeumer CLA 2005-04-25 08:45:24 EDT
Just to clarify: the code can't only be enabled in JDT/Core. This would face the
UI with elements it currently can't deal with asking for lot of trouble.
Comment 36 Matt Chapman CLA 2005-09-26 06:11:34 EDT
As this was deferred to post-3.1, can it now be reopened? I think it would be a
good idea to re-enable the extension point, to help flush out the remaining
problems during 3.2 development.
Comment 37 Jerome Lanneluc CLA 2005-09-26 08:59:50 EDT
Sorry we must ensure that UI and Debug behave correctly (see bug 89977) before
we can enable this extension point.
Comment 38 Jerome Lanneluc CLA 2005-11-30 05:56:11 EST
Reopening since UI and Debug are now ready
Comment 39 Jerome Lanneluc CLA 2005-11-30 06:56:22 EST
Changed org.eclipse.jdt.internal.core.util.Util#ENABLE_JAVA_LIKE_EXTENSIONS to true.
Also added constant JavaCore#JAVA_SOURCE_CONTENT_TYPE to retrieve the Java source content type from the content type manager (see org.eclipse.core.runtime.content.IContentTypeManager#getContentType(String))
Comment 40 Jerome Lanneluc CLA 2005-11-30 07:11:08 EST
For those trying this support, be aware of the following limitation:
Due to bug 118619, it is not possible to update the Java model when a file extension is added to the Java source content-type through the UI. One has to either exit/restart Eclipse, or close/reopen the affected projects.
Comment 41 David Audel CLA 2005-12-13 07:05:49 EST
Verified for 3.2 M4 using build I20051212-0010