mono / sharpen Goto Github PK
View Code? Open in Web Editor NEWSharpen is an Eclipse plugin created by db4o that allows you to convert your Java project into c#
License: GNU General Public License v2.0
Sharpen is an Eclipse plugin created by db4o that allows you to convert your Java project into c#
License: GNU General Public License v2.0
The following function:
void errorExample() {
if (true) {
for (int i = 0; i < 3; i++) {
}
}
for (int i = 0; i < 3; i++) {
}
if (true) {
for (int i = 0; i < 3; i++) {
}
}
}
currently produces the following invalid result:
internal virtual void ErrorExample()
{
if (true)
{
for (int i = 0; i < 3; i++)
{
}
}
for (int i_1 = 0; i_1 < 3; i_1++)
{
}
if (true)
{
for (int i = 0; i_1 < 3; i_1++)
{
}
}
}
Hi,
on my system i've:
How can i fix it?
I am trying to convert a sample program(helloworld) jar to c#. I tried below command but it is ended with error. I am trying to convert with out any configuration file(will that works?)
C:\Users\User1.m2\repository\sharpen\sharpencore\0.0.1-SNAPSHOT>java -jar sha
rpencore-0.0.1-SNAPSHOT-jar-with-dependencies.jar D:\Sharpenconvert -cp D:\Sharp
enconvert\tools2.jar
Configuration Class: null
Configuration Class: Sharpen.Runtime
project: D:
Pascal case mode: None
Conversion finished in 126ms.
ERROR: sourceFiles
java.lang.IllegalArgumentException: sourceFiles
at sharpen.core.framework.ConversionBatch.setsourceFiles(ConversionBatch
.java:57)
at sharpen.core.SharpenApplication.convertTo(SharpenApplication.java:142
)
at sharpen.core.SharpenApplication.convert(SharpenApplication.java:127)
at sharpen.core.SharpenApplication.safeRun(SharpenApplication.java:118)
at sharpen.core.SharpenApplication.start(SharpenApplication.java:57)
at sharpen.core.SharpenCmd.main(SharpenCmd.java:13)
Faied to run. Exception:sourceFiles
I know I did mistake but I don't know what it is exactly( I am not able to figure out even syntax also)
I googled for documentation/ sample code but I didn't find any, that is why I am asking here for immediate help.
In Java, we can have something like this ::
public interface MyInterface {
public const string[] MY_ARR = new string[] { "one", "two", "three};
}
Sharpen maps this to:
public abstract class MyInterface {
public const string[] MY_ARR = new string[] { "one", "two", "three};
}
The generated code is not valid. I think this should be mapped to (which is valid) :
public abstract class MyInterface {
public readonly string[] MY_ARR = { "one", "two", "three};
}
Is there a way around this issue without modifying the codebase? If not, perhaps I can help?
public class ContextFactory
{
private static ContextFactory global = new ContextFactory();
public interface GlobalSetter {
public void setContextFactoryGlobal(ContextFactory factory);
public ContextFactory getContextFactoryGlobal();
}
public synchronized static GlobalSetter getGlobalSetter() {
if (hasCustomGlobal) {
throw new IllegalStateException();
}
hasCustomGlobal = true;
class GlobalSetterImpl implements GlobalSetter {
public void setContextFactoryGlobal(ContextFactory factory) {
global = factory == null ? new ContextFactory() : factory;
}
public ContextFactory getContextFactoryGlobal() {
return global;
}
}
return new GlobalSetterImpl();
}
}
Is converted to
public class ContextFactory
{
private static ContextFactory global = new ContextFactory();
public interface GlobalSetter
{
void SetContextFactoryGlobal(ContextFactory factory);
ContextFactory GetContextFactoryGlobal();
}
public static ContextFactory.GlobalSetter GetGlobalSetter()
{
lock (typeof(ContextFactory))
{
if (hasCustomGlobal)
{
throw new InvalidOperationException();
}
hasCustomGlobal = true;
return new _T1664473621(this); // we are in the static method
}
}
internal class _T1664473621 : ContextFactory.GlobalSetter // the name of a class is incorrect
{
public virtual void SetContextFactoryGlobal(ContextFactory factory)
{
ContextFactory.global = factory == null ? new ContextFactory() : factory;
}
public virtual ContextFactory GetContextFactoryGlobal()
{
return ContextFactory.global;
}
internal _T1664473621(ContextFactory _enclosing)
{
this._enclosing = _enclosing;
}
private readonly ContextFactory _enclosing;
}
}
The following code is mapped incorrectly :
Source Java
Collection<SomeClass> myCollection = getCollection();
SomeClass firstItem = myCollection.iterator().next(); // Assuming we know there is at least one item
doSomething(firstItem);
Generated C#
Collection<SomeClass> myCollection = getCollection();
SomeClass firstItem = myCollection .GetEnumerator().Next(); // ERROR Because there is no Next() method on the enumerator.
doSomething(firstItem);
In Java, Map.get
returns null
when an element does not exist. In C#, the get indexer for IDictionary<TKey, TValue>
throws an exception in the same scenario. Currently calls to get
are getting translated into indexer usage, but this breaks the semantics for this operation. The correct code for this case would be the following:
Java:
T value = map.get(key);
C#:
T value;
map.TryGetValue(key, out value);
Lots of errors about annotations and missing ;
The following Java code:
if (visited.put(current.getParent(i), current.getParent(i)) == null) {
workList.push(current.getParent(i));
}
Is currently producing the following C# code:
if (visited[current.GetParent(i)] = current.GetParent(i) == null)
{
workList.Push(current.GetParent(i));
}
This is incorrect for two reasons:
put
method is the old value stored in the map. The result of the assignment expression is the new value stored in the map. The semantics change results in a major logic error in code like is posted above.I'm trying to convert a project which uses some reserved keywords as identifiers (for example "namespace" and "sealed") but they are not being escaped properly
The following switch statement produces an unreachable goto case
statement in the translated code.
switch (x) {
case 0:
if (something) {
break;
} else {
break;
}
case 1:
break;
}
Did the changes in https://github.com/xamarin/XobotOS ever make it upstream?
I think they should be converted to extension methods, if possible. Otherwise, we need to generate abstract class.
I think that here might be a problem. Because Java's lambdas are interfaces and C#'s lambdas are delegates we can have some problems converting them.
I followed the instructions, plus some tips I found in another issue, but my tests are failing. Windows 7, JDK 1.7.0_79 and JRE 7 (u80 I think) - don't know which one it's using. pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sharpen</groupId>
<artifactId>sharpencore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sharpencore</name>
<url>https://github.com/imazen/sharpen</url>
<organization>
<name>Imazen</name>
<url>http://www.imazen.io</url>
</organization>
<licenses>
<license>
<name>GPL 2 </name>
<url>http://www.gnu.org/licenses/gpl-2.0.html</url>
<distribution>repo</distribution>
<comments>GPL 2 License</comments>
</license>
</licenses>
<developers>
<developer>
<id>1</id>
<name>Nathanael Jones</name>
<email>[email protected]</email>
<organization>Imazen</organization>
<organizationUrl>http://www.imazen.io</organizationUrl>
<roles>
<role>Architect</role>
<role>Developer</role>
</roles>
<timezone>-5</timezone>
</developer>
<developer>
<id>2</id>
<name>Yakov Danilov</name>
<email>[email protected]</email>
<organization>imazen.io</organization>
<organizationUrl>http://www.imazen.io</organizationUrl>
<roles>
<role>Developer</role>
</roles>
<timezone>+3</timezone>
</developer>
</developers>
<issueManagement>
<system>github</system>
<url>https://github.com/imazen/sharpen/issues</url>
</issueManagement>
<ciManagement>
<system>travis</system>
<url>https://travis-ci.org/imazen/sharpen</url>
</ciManagement>
<scm>
<connection>scm:git:github.com:imazen/sharpen.git</connection>
<developerConnection>scm:git:github.com:imazen/sharpen.git</developerConnection>
<url>https://github.com/imazen/sharpen</url>
</scm>
<!--<distributionManagement>-->
<!--<downloadUrl>https://oss.sonatype.org/content/groups/public</downloadUrl>-->
<!--<repository>-->
<!--<id>oss.sonatype.org</id>-->
<!--<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>-->
<!--</repository>-->
<!--<snapshotRepository>-->
<!--<id>oss.sonatype.org</id>-->
<!--<url>https://oss.sonatype.org/content/repositories/snapshots</url>-->
<!--</snapshotRepository>-->
<!--<site>-->
<!--<id>parent.jcabi.com</id>-->
<!--<url>http://parent.jcabi.com/</url>-->
<!--</site>-->
<!--</distributionManagement>-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>runtime</artifactId>
<version>3.10.0-v20140318-2214</version>
</dependency>
<dependency>
<groupId>org.eclipse.equinox</groupId>
<artifactId>common</artifactId>
<version>3.6.200-v20130402-1505</version>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.6.0.v20100517</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<directory>src/target</directory>
<outputDirectory>src/target/classes</outputDirectory>
<finalName>${finalName}</finalName>
<testOutputDirectory>src/target/testcases</testOutputDirectory>
<sourceDirectory>src/main</sourceDirectory>
<scriptSourceDirectory>src/main/resources</scriptSourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>sharpen.core.SharpenCmd</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!--
Static quality analysis with Qulice and other tools.
This profile is supposed to be activated in continuous
integration environment, right before the final approval
of the build. It runs a number of static code analysis
tools to validate and approve the quality of source
code and the SCM repository.
@see http://www.jcabi.com/jcabi-parent/profile-qulice.html
-->
<id>qulice</id>
<build>
<plugins>
<plugin>
<!--
Qulice-maven-plugin is a compound static analysis
tool that aggregates Checkstyle, PMD, FindBugs,
CodeNarc (for Groovy code) and a few other analyzers.
Qulice also configures them correctly to enable the
most important rules.
@see http://www.qulice.com/qulice-maven-plugin/
-->
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.12</version>
<executions>
<execution>
<id>sharpen-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<license>file:${basedir}/LICENSE.txt</license>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
There are a ton of test failures so I put the results in a zip file:
Sharpen should have a feature which is able to automatic identify setters and getters and convert them to properties.
Following rules apply:
If I have code
boolean myMethod() {
Boolean myBool = null;
myBool = something();
return myBool;
}
It is converted to:
bool myMethod() {
bool myBool = null; // INVALID AS BOOL IS PRIMITIVE IN C#
myBool = something();
return myBool;
}
Problem is that it converts Boolean and boolean as primitives, when the java code might depend upon a boolean being able to hold a null value.
A trivial but incorrect fix :
bool myMethod() {
bool? myBool = null; // NOW VALID BECAUSE NULL IS NOW AN ACCEPTED VALUE
myBool = something();
return myBool; // NOW THIS IS AN INVALID BECAUSE C# does not unbox
}
Obviously, this logic is complex so perhaps the answer is to require (and check) that the java code does not use the unbox feature and simply translate boolean to bool?, and within the java code and translate the booleanValue() method on Boolean to the Value property on the bool? instance.
Possible complete solution (depends upon autounboxing detection)::
boolean myMethod() {
Boolean myBool = null;
myBool = something(); // Assuming something() is trusted not to return null
return myBool.booleanValue(); // Manual UNBOXING
}
bool myMethod() {
bool? myBool = null; // NOW VALID BECAUSE NULL IS NOW AN ACCEPTED VALUE
myBool = something();
return myBool.Value; // NOW VALID BECAUSE THIS IS A MANUAL UNBOXING TRANSLATED FROM JAVA
}
Hi Luis
as you can see on my github page (https://github.com/ggerla/java2csharp), I wrote a maven plugin to convert java classes in c#. Now I'm using Xml annotations and XSD schema transition.
I'd like to use the sharpen.core code to avoid to use xml and xsd. I already implemented an example and I think that for me it would a great solution.
Can I use your code (of course, I will change my license from Apache License to GPL)?
The expression String.valueOf((char)c)
generates the following code:
(char)c.ToString()
when it should actually be generating:
((char)c).ToString()
C:/Users/user/Desktop/nfctools/nfctools-core/src/main/java/org/nfctools/mf/ul/MemoryMap.java
Conversion finished in 4781ms.
ERROR: <unknown>:26: failed to map: 'memory=new byte[pages][bytesPerPage]'
java.lang.IllegalArgumentException: <unknown>:26: failed to map: 'memory=new byte[pages][bytesPerPage]'
solved this by adding a compiled version of the source as a jar as a dependency with -cp with the path to the jar
and renaming the file to have an extension other than java
also making a copy of the file with single dimension array, then fixing it manually
maybe it is still a bug. so I leave this open
We should create a full, non-trivial conversion example, using a custom configuration class.
The C# code is generated with XML documentation tags that resemble the following:
<see cref="Item">Item</see>
<seealso cref="Item">Item</seealso>
When the text for the link is not explicitly specified, these should actually produce the following instead:
<see cref="Item"/>
<seealso cref="Item"/>
Hi @slluis, please add a license.
The code in ConfigurationFactory to load the config jar uses the path of the sharpen jar INCLUDING the sharpen jar filename, so you would have to create a folder with the same name as the sharpen jar and drop your config files in there.
Exception::136: failed to map: 'System.out.println(String.format("%d) price: %.2f; attack +%.2f",stair,creaturePrice,creatureEffect))'
Not strictly an issue but perhaps you could add an FAQ with the answers to the following questions ....
I understand that Sharpen is license under GPL, but my question is what is the license status of generated C# code, and is there an LGPL (or other permissive license) version of wrapper classes for referenced JRE classes? Or perhaps each user has to create the wrapper classes themselves?
# Solution:
Add to pom.xml in
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerVersion>1.5</compilerVersion>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
Taken from http://stackoverflow.com/questions/13293062/maven-compile-error
Should Sharpen shims be internal? How do we deal with multiple projects relying on different versions of the shims?
Maybe there's an option I'm missing, so apologies if that's the case. When there are comma separated variables initialized in the for loop they are not preserved by sharpen.
Java:
for (int i = 0, n = 40; i < n; i++) {
Resulting C#:
for (int i = 0; i < n; i++)
I was looking into the source code and maybe there is a problem with the following CSharpBuilder.java class method:
public boolean visit(VariableDeclarationExpression node) {
pushExpression(new CSDeclarationExpression(createVariableDeclaration((VariableDeclarationFragment) node
.fragments().get(0))));
return false;
}
It seems like get(0)
ensures that you'll only ever get one item instead of multiple items. It's hard for me to debug whatever exactly is going on. Below is a test case you can run and the current results I'm getting:
public class testCase {
public static void main (String args[]) {
System.out.println ("Hello World!");
for (int i = 0, n = 40; i < n; i++) {
n *= 2;
System.out.println("Hello " + i);
}
}
}
becomes:
using Sharpen;
public class testCase
{
public static void Main(string[] args)
{
System.Console.Out.WriteLine("Hello World!");
for (int i = 0; i < n; i++)
{
n *= 2;
System.Console.Out.WriteLine("Hello " + i);
}
}
}
It would be nice to have a feature which is able to detect using patterns For example, if we have a try-finally
statement and call of close
method of AutoCloseable
or Closeable
interfaces then we can convert this try-finally statement to a C# using statement.
Does anyone have any suggestions for creating a command-line version of sharpen that does not have an Eclipse dependency? (Such as for use on a build server). Any suggestions for a replacement source code parser system?
https://github.com/INRIA/spoon
http://www.monperrus.net/martin/jexast
https://github.com/matozoid/javaparser
http://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/com/sun/source/tree/package-summary.html
It would be nice if this could be used in the same binaries with Apache-licensed software.
Sharpen generates incorrect code for the following input:
interface Foo {
/** @sharpen.indexer */
int get(int index);
}
abstract class AbstractFoo implements Foo {
}
Here is the current output.
internal interface IFoo
{
int this[int index]
{
get;
}
}
internal abstract class AbstractFoo : IFoo
{
public abstract int Get(int arg1);
}
Simultaneously normalize tabs->spaces (including .txt files)
Java 7 fails when getting the packages from the repo because of outdated protocols.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.