Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 7: pretty-print multi catch "catch (IndexOutOfBoundsException | UncheckedException)" #97

Closed
GerardPaligot opened this issue Nov 12, 2014 · 8 comments

Comments

@GerardPaligot
Copy link
Contributor

For the support of java 7, spoon must support multi catch. It support already (yeah!) but not totally. Here is an example to illustrate the "problem":

TryCatchExample is a class ready to be spooned.

public class TryCatchExample {
    public void method(String[] array) {
        try {
            if (array.length > 10) {
                throw new UncheckedException();
            }
            String firstItem = array[0];
            if ("error".equals(firstItem)) {
                throw new CheckedException();
            }
        } catch (IndexOutOfBoundsException | UncheckedException | CheckedException e) {
            e.printStackTrace();
        }
    }
}

The output of spoon is:

public class TryCatchExample {
    public void method(java.lang.String[] array) {
        try {
            if ((array.length) > 10) {
                throw new UncheckedException();
            } 
            java.lang.String firstItem = array[0];
            if ("error".equals(firstItem)) {
                throw new CheckedException();
            } 
        } catch (java.lang.IndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (UncheckedException e) {
            e.printStackTrace();
        } catch (CheckedException e) {
            e.printStackTrace();
        }
    }
}

So, when spoon detects a multi catch, it replaces with java 6 syntax. So the result is valid and support java 7 but the output isn't java 7.

Good thing or not? I would like reviews about this from the community. :)

@seintur
Copy link
Contributor

seintur commented Nov 12, 2014

Hi,

I'd prefer the keep the Java 7 syntax to keep an as strong as possible consistency between the source code and the pretty-printed one.

Else if you replace the Java 7 syntax by Java 6, one could argue the other constructs, e.g. Java 5 foreach or generics, should also be replaced by a Java 4 compatible syntax.

Lionel.

@swolf91
Copy link
Contributor

swolf91 commented Nov 13, 2014

I would like to have the ability to set up the target java version. If I set java 6 it'll try to write everything with tha java 6 syntax. The same goes for java 5, 7 and 8. I think everything lower than java 5 isn't that important anymore.

That would help to converter a project from java 6 to java 7 or the other way around.

@GerardPaligot
Copy link
Contributor Author

@boeserwolf91 Your proposal is very interesting: transform some projects from a Java version to another version but, for me, it is a different feature. I'm agree with @seintur. By default (and for the scope of this issue about the compatibility of java 7), we keep the Java version of the source project ready to be spooned. And I create another issue to keep your idea @boeserwolf91 for the future.

Ok for everyone?

@seintur
Copy link
Contributor

seintur commented Nov 13, 2014

+1

@swolf91
Copy link
Contributor

swolf91 commented Nov 16, 2014

yes, I'm okay with it @GerardPaligot 👍

@monperrus monperrus changed the title Java 7: multi catch Java 7: pretty-print multi catch "catch (IndexOutOfBoundsException | UncheckedException)" Nov 17, 2014
@seintur
Copy link
Contributor

seintur commented Nov 21, 2014

Hi @monperrus

You changed the title of this issue to "Java 7: pretty-print...". This suggests that you consider this as a pretty-printing issue.

Couldn't we consider that this a transformation issue?

For example, we could design processors that transform a Java 7 program to a Java 6 compliant one, the same for Java 5 to Java 4, etc. By this way, the question of the compliance to a particular version of Java would be handled at the processor level. Note that this wouldn't prevent the "kernel" of Spoon of applying the processor if requested by the command line arguments, or Maven parameters, specified by the developer.

WDYT?

@monperrus
Copy link
Collaborator

Hi Lionel @seintur,

I want to make it clear that there are two different issues:

The former is much more important than the latter, since Spoon rewritten code is not meant to be read (no preservation of formatting, of comments, poor support of "imports")

@GerardPaligot
Copy link
Contributor Author

The PR #98 is now finished. The output of a multi catch is a valid Java 7 syntax. This PR can be closed when the PR will be merged.

@petitpre petitpre closed this as completed Dec 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants