Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-include .sdk/Makefile

$(if $(filter true,$(sdkloaded)),,$(error You must install bblfsh-sdk))

MVN_CMD := mvn
JAR := native-jar-with-dependencies.jar

Expand All @@ -12,5 +16,3 @@ build-native-internal:
cp native/src/main/sh/native.sh $(BUILD_PATH)/native; \
chmod +x $(BUILD_PATH)/native


include .sdk/Makefile
12 changes: 8 additions & 4 deletions driver/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/bblfsh/sdk/protocol/cmd"
"github.com/bblfsh/sdk/protocol"

"github.com/bblfsh/java-driver/driver/normalizer"
)
Expand All @@ -10,7 +10,11 @@ var version string
var build string

func main() {
cmd.DriverMain(version, build,
normalizer.NativeToNoder,
normalizer.AnnotationRules)
d := protocol.Driver{
Version: version,
Build: build,
ToNoder: normalizer.NativeToNoder,
Annotate: normalizer.AnnotationRules,
}
d.Exec()
}
315 changes: 222 additions & 93 deletions driver/normalizer/jdt/jdt.go

Large diffs are not rendered by default.

149 changes: 122 additions & 27 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,129 @@ import (
)

var AnnotationRules = On(Any).Self(
On(Not(HasInternalType(jdt.CompilationUnit))).Error("root must be CompilationUnit"),
On(HasInternalType(jdt.CompilationUnit)).Roles(File).Descendants(
On(HasInternalType(jdt.PackageDeclaration)).Roles(PackageDeclaration),
On(HasInternalType(jdt.MethodDeclaration)).Roles(FunctionDeclaration),
On(HasInternalType(jdt.ImportDeclaration)).Roles(ImportDeclaration).Children(
On(HasInternalType(jdt.QualifiedName)).Roles(ImportPath),
),
On(HasInternalType(jdt.TypeDeclaration)).Roles(TypeDeclaration),
On(HasInternalType(jdt.QualifiedName)).Roles(QualifiedIdentifier),
On(HasInternalType(jdt.SimpleName)).Roles(SimpleIdentifier),
On(HasInternalType(jdt.Block)).Roles(BlockScope, Block),
On(HasInternalType(jdt.ExpressionStatement)).Roles(Statement),
On(HasInternalType(jdt.ReturnStatement)).Roles(Return, Statement),
On(HasInternalType(jdt.MethodInvocation)).Roles(MethodInvocation),
On(HasInternalType(jdt.IfStatement)).Roles(If, Statement),
On(HasInternalRole("elseStatement")).Roles(IfElse, Statement),
On(HasInternalType(jdt.Assignment)).Roles(Assignment).Children(
On(HasInternalRole("leftHandSide")).Roles(AssignmentVariable),
On(HasInternalRole("rightHandSide")).Roles(AssignmentValue),
),
//TODO: IfBody, IfCondition
On(HasInternalType(jdt.NullLiteral)).Roles(NullLiteral, Literal),
On(HasInternalType(jdt.StringLiteral)).Roles(StringLiteral, Literal),
On(HasInternalType(jdt.NumberLiteral)).Roles(NumberLiteral, Literal),
On(HasInternalType(jdt.TypeLiteral)).Roles(TypeLiteral, Literal),
On(HasInternalType(jdt.ThisExpression)).Roles(This, Expression),
On(Not(jdt.CompilationUnit)).Error("root must be CompilationUnit"),
On(jdt.CompilationUnit).Roles(File).Descendants(
// Names
On(jdt.QualifiedName).Roles(QualifiedIdentifier),
On(jdt.SimpleName).Roles(SimpleIdentifier),

// Visibility
On(Or(jdt.MethodDeclaration, jdt.TypeDeclaration)).Self(
On(HasChild(And(jdt.Modifier, jdt.KeywordPublic))).Roles(VisibleFromWorld),
On(HasChild(And(jdt.Modifier, jdt.KeywordPrivate))).Roles(VisibleFromType),
On(HasChild(And(jdt.Modifier, jdt.KeywordProtected))).Roles(VisibleFromSubtype),
On(Not(HasChild(And(jdt.Modifier,
Or(jdt.KeywordPublic, jdt.KeywordPrivate, jdt.KeywordProtected),
)))).Roles(VisibleFromPackage),
),

// Package and imports
On(jdt.PackageDeclaration).Roles(PackageDeclaration),
On(jdt.ImportDeclaration).Roles(ImportDeclaration).Children(
On(jdt.QualifiedName).Roles(ImportPath),
),

// Type declarations
On(jdt.TypeDeclaration).Roles(TypeDeclaration),

// Method declarations
On(jdt.MethodDeclaration).Roles(FunctionDeclaration).Children(
//TODO: On(jdt.PropertyTypeParameters).Roles(FunctionDeclarationTypeParameter),
//TODO: On(jdt.PropertyReturnType2).Roles(FunctionDeclarationReturnType)
On(jdt.PropertyName).Roles(FunctionDeclarationName),
On(jdt.PropertyBody).Roles(FunctionDeclarationBody),
On(jdt.PropertyParameters).Roles(FunctionDeclarationArgument).Self(
On(HasProperty("varargs", "true")).Roles(FunctionDeclarationVarArgsList),
).Children(
//TODO: On(jdt.PropertyType).Roles(FunctionDeclarationArgumentType),
On(jdt.PropertyName).Roles(FunctionDeclarationArgumentName),
),
),

// Literals
On(jdt.BooleanLiteral).Roles(BooleanLiteral),
On(jdt.CharacterLiteral).Roles(CharacterLiteral),
On(jdt.NullLiteral).Roles(NullLiteral),
On(jdt.NumberLiteral).Roles(NumberLiteral),
On(jdt.StringLiteral).Roles(StringLiteral),
On(jdt.TypeLiteral).Roles(TypeLiteral),

// Calls
On(jdt.MethodInvocation).Roles(Call).Children(
On(jdt.PropertyExpression).Roles(CallReceiver),
On(jdt.PropertyName).Roles(CallCallee),
On(jdt.PropertyArguments).Roles(CallPositionalArgument),
),

// Conditionals
On(jdt.IfStatement).Roles(If, Statement).Children(
On(jdt.PropertyExpression).Roles(IfCondition),
On(jdt.PropertyThenStatement).Roles(IfBody),
On(jdt.PropertyElseExpression).Roles(IfElse),
),

On(jdt.SwitchStatement).Roles(Switch, Statement).Children(
//TODO: On(jdt.PropertyExpression).Roles(SwitchExpression),
On(jdt.SwitchCase).Roles(SwitchCase),
),

On(jdt.InfixExpression).Self(
On(HasProperty("operator", "+")).Roles(OpAdd),
On(HasProperty("operator", "-")).Roles(OpSubstract),
On(HasProperty("operator", "*")).Roles(OpMultiply),
On(HasProperty("operator", "/")).Roles(OpDivide),
On(HasProperty("operator", "%")).Roles(OpMod),
On(HasProperty("operator", "<<")).Roles(OpBitwiseLeftShift),
On(HasProperty("operator", ">>")).Roles(OpBitwiseRightShift),
On(HasProperty("operator", ">>>")).Roles(OpBitwiseUnsignedRightShift),
On(HasProperty("operator", "&")).Roles(OpBitwiseAnd),
On(HasProperty("operator", "|")).Roles(OpBitwiseOr),
On(HasProperty("operator", "&&")).Roles(OpBooleanAnd),
On(HasProperty("operator", "||")).Roles(OpBooleanOr),
On(HasProperty("operator", "^")).Roles(OpBooleanXor),
),

On(jdt.PostfixExpression).Self(
On(HasProperty("operator", "++")).Roles(OpPostIncrement),
On(HasProperty("operator", "--")).Roles(OpPostDecrement),
),

On(jdt.PrefixExpression).Self(
On(HasProperty("operator", "++")).Roles(OpPreIncrement),
On(HasProperty("operator", "--")).Roles(OpPreDecrement),
On(HasProperty("operator", "+")).Roles(OpPositive),
On(HasProperty("operator", "-")).Roles(OpNegative),
On(HasProperty("operator", "~")).Roles(OpBitwiseComplement),
On(HasProperty("operator", "!")).Roles(OpBooleanNot),
),

On(jdt.Assignment).Roles(Assignment).Children(
On(jdt.PropertyLeftHandSide).Roles(AssignmentVariable),
On(jdt.PropertyRightHandSide).Roles(AssignmentValue),
).Self(
On(Not(HasProperty("operator", "="))).Roles(AugmentedAssignmentOperator, AugmentedAssignment).Self(
On(HasProperty("operator", "+=")).Roles(OpAdd),
On(HasProperty("operator", "-=")).Roles(OpSubstract),
On(HasProperty("operator", "*=")).Roles(OpMultiply),
On(HasProperty("operator", "/=")).Roles(OpDivide),
On(HasProperty("operator", "%=")).Roles(OpMod),
On(HasProperty("operator", "&=")).Roles(OpBitwiseAnd),
On(HasProperty("operator", "|=")).Roles(OpBitwiseOr),
On(HasProperty("operator", "^=")).Roles(OpBooleanXor),
On(HasProperty("operator", "<<=")).Roles(OpBitwiseLeftShift),
On(HasProperty("operator", ">>=")).Roles(OpBitwiseRightShift),
On(HasProperty("operator", ">>>=")).Roles(OpBitwiseUnsignedRightShift),
),
),

// Others
On(jdt.Block).Roles(BlockScope, Block),
On(jdt.ExpressionStatement).Roles(Statement),
On(jdt.ReturnStatement).Roles(Return, Statement),

On(jdt.ThisExpression).Roles(This, Expression),
//TODO: synchronized
//TODO: try-with-resources
On(HasInternalType(jdt.Javadoc)).Roles(Documentation, Comment),
On(jdt.Javadoc).Roles(Documentation, Comment),
),
)
6 changes: 6 additions & 0 deletions native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions native/src/main/java/bblfsh/CloseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bblfsh;

import java.io.IOException;

// CloseException signals that stream is cleanly closed.
public class CloseException extends IOException {

public CloseException(final String msg) {
super(msg);
}

public CloseException(final String msg, final Throwable cause) {
super(msg, cause);
}

}
10 changes: 7 additions & 3 deletions native/src/main/java/bblfsh/Driver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bblfsh;

import java.io.*;
import sun.reflect.annotation.ExceptionProxy;

import java.io.IOException;
import java.util.ArrayList;

public class Driver {
Expand All @@ -15,16 +17,18 @@ public Driver(final RequestReader reader, final ResponseWriter writer) {
this.parser = new EclipseParser();
}

public void run() throws DriverException {
public void run() throws DriverException, CloseException {
while (true) {
this.processOne();
}
}

public void processOne() throws DriverException {
public void processOne() throws DriverException, CloseException {
Request request;
try {
request = this.reader.read();
} catch (CloseException ex) {
throw ex;
} catch (Exception ex) {
final Response response = createFatalResponse(ex);
try {
Expand Down
2 changes: 2 additions & 0 deletions native/src/main/java/bblfsh/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static void main(String args[]) {

try {
driver.run();
} catch (CloseException e) {
System.exit(0);
} catch (DriverException e) {
System.exit(1);
}
Expand Down
7 changes: 3 additions & 4 deletions native/src/main/java/bblfsh/RequestReader.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package bblfsh;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand All @@ -28,6 +24,9 @@ public RequestReader(final InputStream in) {

public Request read() throws IOException {
final String line = this.reader.readLine();
if (line == null) {
throw new CloseException("exception while reading line (null)");
}
return mapper.readValue(line, Request.class);
}

Expand Down
3 changes: 2 additions & 1 deletion native/src/main/java/bblfsh/ResponseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.eclipse.jdt.core.dom.CompilationUnit;

import java.io.*;
import java.io.IOException;
import java.io.OutputStream;

public class ResponseWriter {

Expand Down
20 changes: 17 additions & 3 deletions native/src/test/java/bblfsh/DriverTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bblfsh;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
Expand All @@ -12,7 +11,7 @@
public class DriverTest {

@Test
public void process() throws DriverException {
public void process() throws DriverException, CloseException {
final String input = "{\"content\":\"package foo;\"}\n{\"content\":\"package bar;\"}\n";
final InputStream in = new ByteArrayInputStream(input.getBytes());
final RequestReader reader = new RequestReader(in);
Expand All @@ -27,7 +26,22 @@ public void process() throws DriverException {
}

@Test
public void processInvalid() throws DriverException {
public void exitOnCloseIn() throws DriverException, CloseException {
final String input = "{\"content\":\"package foo;\"}\n{\"content\":\"package bar;\"}\n";
final InputStream in = new ByteArrayInputStream(input.getBytes());
final RequestReader reader = new RequestReader(in);

final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ResponseWriter writer = new ResponseWriter(out);

final Driver driver = new Driver(reader, writer);
try {
driver.run();
} catch (CloseException ex) { }
}

@Test
public void processInvalid() throws DriverException, CloseException {
final String input = "garbage";
final InputStream in = new ByteArrayInputStream(input.getBytes());
final RequestReader reader = new RequestReader(in);
Expand Down
Loading