Skip to content

Commit

Permalink
Implement JavaLibrary and add initial test framework. (#20440)
Browse files Browse the repository at this point in the history
  • Loading branch information
azabbasi authored Apr 8, 2021
1 parent 7325bf9 commit 77baf4b
Show file tree
Hide file tree
Showing 32 changed files with 1,071 additions and 80 deletions.
2 changes: 2 additions & 0 deletions sdk/modelparser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated test files.
*.temp.generated
4 changes: 2 additions & 2 deletions sdk/modelparser/azure-digitaltwins-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</scm>

<properties>
<jacoco.min.linecoverage>0.20</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.20</jacoco.min.branchcoverage>
<jacoco.min.linecoverage>0.0</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.0</jacoco.min.branchcoverage>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ public class CodeWriter {
public CodeWriter(String filePath) throws IOException {
FileOutputStream fileStream = new FileOutputStream(filePath);
this.fileWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
this.indentedFileWriter = new IndentedFileWriter(fileWriter, INDENTATION);
this.indentedFileWriter = new IndentedFileWriter(fileWriter, INDENTATION, false);
}

/**
* Initializes a new instance of {@link CodeWriter}.
*
* @param filePath Full path of file to be generated.
* @param isDebug Boolean indicating whether or not debug mode is on or not.
* @throws IOException
*/
public CodeWriter(String filePath, boolean isDebug) throws IOException {
FileOutputStream fileStream = new FileOutputStream(filePath);
this.fileWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
this.indentedFileWriter = new IndentedFileWriter(fileWriter, INDENTATION, isDebug);
}

/**
Expand All @@ -37,6 +50,7 @@ public CodeWriter(String filePath) throws IOException {
* @throws IOException
*/
public void close() throws IOException {
this.indentedFileWriter.close();
this.fileWriter.flush();
this.fileWriter.close();
}
Expand All @@ -57,7 +71,7 @@ public void blank() {
* @throws IOException
*/
public void openScope() throws IOException {
indentedFileWriter.writeLineWithIndent("{");
indentedFileWriter.writeLineWithNoIndent("{");
this.increaseIndent();
nextTextNeedsBlank = false;
lastLineWasText = false;
Expand All @@ -70,7 +84,7 @@ public void openScope() throws IOException {
*/
public void closeScope() throws IOException {
indentedFileWriter.decreaseIndent();
indentedFileWriter.writeWithIndent("}");
indentedFileWriter.writeLineWithIndent("}");
nextTextNeedsBlank = true;
lastLineWasText = false;
}
Expand All @@ -95,30 +109,47 @@ public void decreaseIndent() {
* @param text Code text to write.
*/
public void writeLine(String text) throws IOException {
writeLine(text, false, false);
writeLine(text, false, false, false);
}

/**
* Writes a line of code.
*
* @param text Code text to write.
* @param suppressLineBreak True if the line should not end with a line break.
*/
public void writeLine(String text, boolean suppressLineBreak) throws IOException {
writeLine(text, suppressLineBreak, false, false);
}

/**
* Writes a line of code
*
* @param text Code text to write.
* @param suppressBlank True if there should be no blank line preceding the text.
* @param text Code text to write.
* @param suppressLineBreak True if the line should not end with a line break.
* @param suppressBlank True if there should be no blank line preceding the text.
*/
public void writeLine(String text, boolean suppressBlank) throws IOException {
writeLine(text, suppressBlank, false);
public void writeLine(String text, boolean suppressLineBreak, boolean suppressBlank) throws IOException {
writeLine(text, suppressLineBreak, suppressBlank, false);
}

/**
* Writes a line of code
*
* @param text Code text to write.
* @param suppressBlank True if there should be no blank line preceding the text.
* @param outdent True if the line should be out-dented one level.
* @param text Code text to write.
* @param suppressLineBreak True if the text should not be followed by a new line.
* @param suppressBlank True if there should be no blank line preceding the text.
* @param outdent True if the line should be out-dented one level.
*/
public void writeLine(String text, boolean suppressBlank, boolean outdent) throws IOException {
public void writeLine(String text, boolean suppressLineBreak, boolean suppressBlank, boolean outdent) throws IOException {
if (nextTextNeedsBlank) {
if (!suppressBlank) {
indentedFileWriter.writeLineWithIndent("");
if (suppressLineBreak) {
indentedFileWriter.writeWithIndent("");
} else {
indentedFileWriter.writeLineWithNoIndent("");
}

}

nextTextNeedsBlank = false;
Expand All @@ -128,7 +159,11 @@ public void writeLine(String text, boolean suppressBlank, boolean outdent) throw
decreaseIndent();
}

this.indentedFileWriter.writeLineWithIndent(text);
if (suppressLineBreak) {
indentedFileWriter.writeWithIndent(text + " ");
} else {
indentedFileWriter.writeLineWithIndent(text);
}

if (outdent) {
increaseIndent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,48 @@ class IndentedFileWriter {
private final OutputStreamWriter fileWriter;
private int indent;
private final String indentation;
private boolean isDebug;
private StringBuilder stringBuilder;

IndentedFileWriter(OutputStreamWriter fileWriter, String indentation) {
IndentedFileWriter(OutputStreamWriter fileWriter, String indentation, boolean isDebug) {
this.indent = 0;
this.indentation = indentation;
this.fileWriter = fileWriter;
this.isDebug = isDebug;
if (isDebug) {
stringBuilder = new StringBuilder();
}
}

void writeLineWithIndent(String input) throws IOException {
fileWriter.append(calculateIndentation()).append(input).append("\n");
if (this.isDebug) {
stringBuilder.append(calculateIndentation()).append(input).append("\r\n");
}

fileWriter.append(calculateIndentation()).append(input).append("\r\n");
}

void writeWithIndent(String input) throws IOException {
if (this.isDebug) {
stringBuilder.append(calculateIndentation()).append(input);
}
fileWriter.append(calculateIndentation()).append(input);
}

void writeWithNoIndent(String input) throws IOException {
if (this.isDebug) {
this.stringBuilder.append(input);
}
fileWriter.append(input);
}

void writeLineWithNoIndent(String input) throws IOException {
if (this.isDebug) {
this.stringBuilder.append(input);
}
fileWriter.append(input).append("\r\n");
}

void increaseIndent() {
indent++;
}
Expand All @@ -46,4 +69,11 @@ private String calculateIndentation() {
}
return output.toString();
}

public void close() throws IOException {
if (this.isDebug) {
stringBuilder.append("\r\n");
}
fileWriter.append("\r\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.digitaltwins.parser.implementation.codegen;

/**
* Generator for a java class.
*/
public class JavaClass extends JavaType {
/**
* Initializes a new instance of the {@link JavaClass} class.
*
* @param access Access level of class.
* @param novelty Novelty of the class.
* @param typeName The name of the class being declared.
* @param multiplicity Static or Instance.
* @param extend Interfaces extended by this class.
* @param implement Interfaces implemented by this class.
*/
public JavaClass(Access access, Novelty novelty, String typeName, Multiplicity multiplicity, String extend, String implement) {
super(access, novelty, typeName, multiplicity, extend, implement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class JavaDeclaration {

private final String type;
private final String name;
private boolean inheritDoc;
private final List<String> summaryLines;
private final List<String> remarksLines;
private final List<String> attributes;
protected boolean inheritDoc;
protected final List<String> summaryLines;
protected final List<String> remarksLines;
protected final List<String> attributes;

/**
* Initializes a new instance of the {@link JavaDeclaration} class.
Expand Down Expand Up @@ -114,7 +114,7 @@ public String getDecoratedName() {
decoratedName.append("private ");
break;
default:
logger.logThrowableAsError(new IllegalStateException("Unexpected value: " + access));
throw logger.logExceptionAsError(new IllegalStateException("Unexpected value: " + access));
}

if (multiplicity == Multiplicity.STATIC) {
Expand Down Expand Up @@ -145,7 +145,7 @@ public String getDecoratedName() {
*/
public void addSummary(String text) {
if (!text.endsWith(".")) {
logger.logThrowableAsError(new StyleException("Summary text of declaration '" + this.name + "' must end with a period -- SA1629."));
throw logger.logExceptionAsError(new StyleException("Summary text of declaration '" + this.name + "' must end with a period -- SA1629."));
}

this.summaryLines.add(text);
Expand All @@ -158,7 +158,7 @@ public void addSummary(String text) {
*/
public void addRemarks(String text) {
if (!text.endsWith(".")) {
logger.logThrowableAsError(new StyleException("Remarks text of declaration '" + this.name + "' must end with a period -- SA1629."));
throw logger.logExceptionAsError(new StyleException("Remarks text of declaration '" + this.name + "' must end with a period -- SA1629."));
}

this.remarksLines.add(text);
Expand Down Expand Up @@ -196,16 +196,18 @@ protected void writeSummaryAndRemarks(CodeWriter codeWriter) throws IOException
codeWriter.writeLine("/**");

for (String summaryLine : summaryLines) {
codeWriter.writeLine("* " + summaryLine);
codeWriter.writeLine(" * " + summaryLine);
}

codeWriter.writeLine("* <p>");
for (String remarksLine : remarksLines) {
codeWriter.writeLine("* " + remarksLine);
if (!remarksLines.isEmpty()) {
codeWriter.writeLine(" * <p>");
for (String remarksLine : remarksLines) {
codeWriter.writeLine(" * " + remarksLine);
}
codeWriter.writeLine(" * </p>");
}

codeWriter.writeLine("* </p>");
codeWriter.writeLine("*/");
codeWriter.writeLine(" */");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public JavaEnum(Access access, String typeName, boolean isSorted) {
*/
public JavaEnum value(String name, String description) {
if (description != null && !description.endsWith(".")) {
logger.logThrowableAsError(new StyleException("Documentation text of enum value '" + name + "' must end with a period. -- SA1629."));
throw logger.logExceptionAsError(new StyleException("Documentation text of enum value '" + name + "' must end with a period. -- SA1629."));
}

this.enumValues.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public void generateCode(CodeWriter codeWriter) throws IOException {

if (this.extend != null || this.implement != null) {
if (this.extend == null) {
codeWriter.writeLine(this.getDecoratedName() + "implements " + this.implement);
codeWriter.writeLine(this.getDecoratedName() + " implements " + this.implement, true);
} else if (implement == null) {
codeWriter.writeLine(this.getDecoratedName() + "extends " + this.extend);
codeWriter.writeLine(this.getDecoratedName() + " extends " + this.extend, true);
} else {
codeWriter.writeLine(this.getDecoratedName() + "extends " + this.extend + " implements " + this.implement);
codeWriter.writeLine(this.getDecoratedName() + " extends " + this.extend + " implements " + this.implement, true);
}
} else {
codeWriter.writeLine(this.getDecoratedName());
codeWriter.writeLine(this.getDecoratedName(), true);
}

codeWriter.openScope();
Expand Down
Loading

0 comments on commit 77baf4b

Please sign in to comment.