GithubHelp home page GithubHelp logo

exercism / java-representer Goto Github PK

View Code? Open in Web Editor NEW
0.0 6.0 9.0 332 KB

License: GNU Affero General Public License v3.0

Dockerfile 1.04% Shell 11.41% Java 87.54%
exercism-tooling exercism-representer community-contributions-accepted

java-representer's Introduction

Java Representer Coverage Status

This repository contains the source code for the Representer used by the Java track on Exercism. It takes a Java exercise solution submitted by a student and transforms it into a normalized representation.

Contributing

If you want to contribute to the Java Representer, please refer to the Contributing Guide.

Usage

Running directly

Start by building the JAR using Gradle:

./gradlew build

Then, run the Java Representer using build/libs/java-representer.jar. For example, to create a representation for a solution to the leap exercise, run:

java -jar build/libs/java-representer.jar leap /path/to/leap /path/to/output/folder

The Representer output can be found in the following files inside the /path/to/output/folder directory:

  • representation.txt - Contains the normalized representation of the Java code inside the solution.
  • mapping.json - Contains a mapping between the generated placeholders used in the representation and the original identifiers.
  • representation.json - Contains metadata.

Running with Docker

To run the Java Representer using Docker, first build and tag the Docker image:

docker build -t exercism/java-representer .

Then, run the image and mount the directory of the solution to represent. For example, to create a representation for a solution to the leap exercise, run:

docker run -v /path/to/leap:/input -v /path/to/output/folder:/output exercism/java-representer leap /input /output

The Representer output can be found in the following files inside the /path/to/output/folder directory:

  • representation.txt - Contains the normalized representation of the Java code inside the solution.
  • mapping.json - Contains a mapping between the generated placeholders used in the representation and the original identifiers.
  • representation.json - Contains metadata.

Tests

Unit tests

To run the unit tests:

./gradlew test

Smoke tests

To run the smoke tests using Docker:

bin/run-tests-in-docker.sh

java-representer's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-representer's Issues

Normalize usage of `this`

It would be nice if the representer was able to normalize code such as the following two snippets into the same representation:

class SomeClass {
    private String someField = "";

    String getSomeField() {
        return someField;
    }

    void setSomeField(String someField) {
        this.someField = someField;
    }
}
class SomeClass {
    private String someField = "";

    String getSomeField() {
        return this.someField;
    }

    void setSomeField(String someField) {
        this.someField = someField;
    }
}

Right now, these variants both get their own representation because one uses this in getSomeField() while the other one doesn't. The easiest way to normalize would be to always add this when referencing instance fields or methods, since removing it will result in invalid code for setSomeField().

Analyze types of method parameters for substitution

Currently java-represent doesn't analyze types of method parameters to understand if they are user defined or not

Example extracted from Allergen exercise

boolean PLACEHOLDER_15(Allergen PLACEHOLDER_16) 
    {
        return (PLACEHOLDER_10 & PLACEHOLDER_17.getScore()) != 0;
    }

missing placeholder substitution in variable definition

Application doesn't substitute placeholder into a variable definition

Example

List<T> newCollection = new ArrayList<>();

Wrong representation.txt for accumulate exercise

class PLACEHOLDER_1
{

    static <T> List<T> PLACEHOLDER_2(List<T> PLACEHOLDER_6, Function<T, T> PLACEHOLDER_4) 
    {
        List<T> newCollection = new ArrayList<>();
        for (T item : PLACEHOLDER_6) 
        {
            PLACEHOLDER_5.add(PLACEHOLDER_4.apply(PLACEHOLDER_3));
        }
        return PLACEHOLDER_5;
    }
}

Enum is not correctly replaced by a placeholder

The enum name isn't substituted by a placeholder

Example of representation of allergies exercise

enum Allergen {

    EGGS(1),
    PEANUTS(2),
    SHELLFISH(4),
    STRAWBERRIES(8),
    TOMATOES(16),
    CHOCOLATE(32),
    POLLEN(64),
    CATS(128);

    private final int PLACEHOLDER_1;

    PLACEHOLDER_2(int PLACEHOLDER_1)
    {
        this.PLACEHOLDER_1 = PLACEHOLDER_1;
    }

    int PLACEHOLDER_3()
    {
        return PLACEHOLDER_1;
    }                                                                                                                                                                                                              
}                                                                                                                                                                                                                  
class PLACEHOLDER_4                                                                                                                                                                                                
{                                                                                                                                                                                                                  
                                                                                                                                                                                                                   
    private int PLACEHOLDER_1;                                                                                                                                                                                     
                                                                                                                                                                                                                   
    PLACEHOLDER_4(int PLACEHOLDER_1)                                                                                                                                                                               
    {                                                                                                                                                                                                              
        this.PLACEHOLDER_1 = PLACEHOLDER_1;                                                                                                                                                                        
    }                                                                                                                                                                                                              
                                                                                                                                                                                                                   
    List<Allergen> PLACEHOLDER_5()                                                                                                                                                                                 
    {                                                                                                                                                                                                              
        return PLACEHOLDER_7.allOf(Allergen.class).stream().filter(this::isAllergicTo).collect(PLACEHOLDER_6.toList());                                                                                            
    }                                                                                                                                                                                                              
                                                                                                                                                                                                                   
    boolean PLACEHOLDER_8(Allergen PLACEHOLDER_9)                                                                                                                                                                  
    {                                                                                                                                                                                                              
        return (PLACEHOLDER_1 & PLACEHOLDER_9.getScore()) != 0;                                                                                                                                                    
    }                                                                                                                                                                                                              
}          

Errors in parsing version JSON file

We're getting the following error in production:

unexpected characters after the JSON document (after ) at line 2, column 1 [parse.c:731] in '{"version": 1} {"version": 1} {"version": 1} {"version": 1}

Analyze MyClass.class for substitution

Currently java-representer doesn't analyze MyClass.class format to understand if MyClass is user-defined or not

Here an example extracted from Allergen exercise

 List<PLACEHOLDER_1> PLACEHOLDER_14() 
    {
        return EnumSet.allOf(Allergen.class).stream().filter(this::isAllergicTo).collect(Collectors.toList());
    }

Build and push docker image error

Failure during the step Build and push Docker image

with this message
buildx call failed with: failed to solve: rpc error: code = Unknown desc = server message: insufficient_scope: authorization failed

Java-representer doesn't understand what is user defined

At the moment the java-representer is not able to understand if a class or interface is user-defined or provided by the language.
So java-representer replaces with placeholder too much "names"

For example Arrays.asList() is replaced by placeholder ( PLACEHOLDER_1.asList()) even if Arrays is a class provided by Java

solution composed by multiple source files

At the moment application assumes that solution consists in an only source file.

There are some exercises in the Java track that consist in more files. (Problem allergies for example)

Comment format not removed

Currently java-representer doesn't remove the comment format like this:

 /**
    * Returns true if the mapping letters to digits using {@code letterToDigit} will result in having zero as a
   * leading digit.
    */

The master branch will be renamed to main

In line with our new org-wide policy, the master branch of this repo will be renamed to main. All open PRs will be automatically repointed.

GitHub will show you a notification about this when you look at this repo after renaming:

Screenshot 2021-01-27 at 15 31 45

In case it doesn't, this is the command it suggests:

git branch -m master main
git fetch origin
git branch -u origin/main main

You may like to update the primary branch on your forks too, which you can do under Settings->Branches and clicking the pencil icon on the right-hand-side under Default Branch:

Screenshot 2021-01-27 at 18 50 08

We will post a comment below when this is done. We expect it to happen within the next 12 hours.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.