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

Bug 356339

Summary: Java calling ExternalType constructor that doesn't exist
Product: z_Archived Reporter: Kathy Carroll <carrollk>
Component: EDTAssignee: Project Inbox <edt.compiler-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P1 CC: jeffdouglas, mheitz, pharmon, svihovec, tww
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Kathy Carroll CLA 2011-08-31 10:22:33 EDT
===========  EGL code  ================

handler CreateResultsFile 
    function createFile(fileDirectory String in, fullFileName String in, fileContent String in)
    	myfile File  = new File(fileDirectory);
		createdDirs boolean = myfile.mkdirs();
		if(createdDirs)
			SysLib.writeStdOut("directories created");			
		end					
		
		outWriter FileWriter;  
		try
			outWriter = new FileWriter(fullFileName);
			outWriter.write(fileContent);
			outWriter.close_();
		onException (exp AnyException)
		end
	end
end

ExternalType File type JavaObject {packageName = "java.io"}
     constructor (pathname String In);
     function getAbsolutePath() returns (String);
     function createNewFile() returns (boolean);
     function mkdirs() returns (boolean);
     function exists() returns (boolean);
end

ExternalType FileWriter type JavaObject { packageName = "java.io"}
	constructor (file File In);
	constructor (filename String In);
	function write(str String In);
	function close_() {externalName ="close"};
end

================= Java Code =============
		FileWriter outWriter = null;
		outWriter = new FileWriter();
		try {
			outWriter = new FileWriter(fullFileName);
			outWriter.write(fileContent);
			outWriter.close();
		}
Comment 1 Matt Heitz CLA 2011-08-31 10:30:15 EDT
Amazingly, this is working as designed.  It's a result of the nullability language changes that were made recently.

There's no question mark on this declaration:
  outWriter FileWriter;
so that means the variable is not nullable.  That means it's never supposed to be null, so we have to call a constructor to create a FileWriter object.  It's as if, before the nullability change, you had written this instead:
  outWriter FileWriter {};

You can either add the question mark:
  outWriter FileWriter?;
or declare it on the same line where you give it a value:
  try
    outWriter FileWriter = new FileWriter(fullFileName);
Comment 2 Kathy Carroll CLA 2011-08-31 11:01:41 EDT
Should I be getting a validation error message since the constructor that will be used isn't defined in the ExternalType?

A validation check will be added to ensure that things which can't be instantiated (interfaces & things with private default constructors) must be declared nullable. Constructors can be in ETs and (soon) handlers.
Comment 3 Matt Heitz CLA 2011-08-31 11:25:27 EDT
Tim, please answer Kathy's question.  (The second paragraph in her comment is from my writeup of the enhancement.)

It seems to me that either her ET needs a private default constructor, or we should add "types without a default constructor" to the list of things that can't be instantiated.
Comment 4 Kathy Carroll CLA 2011-10-12 12:47:11 EDT
Need an answer the question specified in Comment 2 & 3
Comment 5 Jeff Douglas CLA 2011-10-18 11:08:30 EDT
It seems to me after reading this, that this is a validation defect, not javagen.
Comment 6 Paul Harmon CLA 2011-10-25 14:45:17 EDT
I asked Tim about this a few weeks ago. He said (basically) that unless they have defined a private default constructor, that we should assume that the type is instantiable.

I believe that is because it is just Java's implementation that says that if a class has a non-default constructor that it does not have an implicit default constructor. Other languages may not have that same rule.

If you define a private default constructor in the externaltype, you will get a validation error.

Because of this, I am marking this as WontFix
Comment 7 Kathy Carroll CLA 2011-10-27 16:57:14 EDT
ok