diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-25 19:18:09 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-25 19:34:44 +0000 |
| commit | 368f2dd010defaf5f4b6deb70f45285cee8f244d (patch) | |
| tree | 55494f85341f23dd1e588fa070ef863ef7cfdef5 /src/main/java/org/zwobble/couscous/frontends | |
| parent | 060ae04a695df6abac7445df3c5394b59da40f41 (diff) | |
Remove lombok
Diffstat (limited to 'src/main/java/org/zwobble/couscous/frontends')
3 files changed, 130 insertions, 171 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java index 8b92cec..d7bd54f 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java @@ -6,22 +6,24 @@ import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.zwobble.couscous.ast.ClassNode; -import lombok.val; - public class JavaFrontend { public static List<ClassNode> readSourceDirectory(Path directoryPath) throws IOException { - val reader = new JavaReader(); - + JavaReader reader = new JavaReader(); return findJavaFiles(directoryPath) - .map(javaFile -> reader.readClassFromFile(directoryPath, javaFile)) + .map(javaFile -> { + // TODO: unwrap exception in outer scope + try { + return reader.readClassFromFile(directoryPath, javaFile); + } catch (Exception e) { + throw new RuntimeException(e); + } + }) .collect(Collectors.toList()); } private static Stream<Path> findJavaFiles(Path directoryPath) throws IOException { - return Files.walk(directoryPath) - .filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); + return Files.walk(directoryPath).filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); } -} +}
\ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java index d171761..0f8d963 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java @@ -2,18 +2,15 @@ package org.zwobble.couscous.frontends.java; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.nio.file.Files; import java.nio.file.Path; - import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; - import com.google.common.base.Charsets; -import lombok.val; - public class JavaParser { private final ASTParser parser; @@ -22,20 +19,15 @@ public class JavaParser { parser.setBindingsRecovery(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setCompilerOptions(JavaCore.getOptions()); - parser.setEnvironment( - new String[] {"/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"}, - new String[0], - new String[0], - false); + parser.setEnvironment(new String[]{"/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"}, new String[0], new String[0], false); parser.setResolveBindings(true); } public CompilationUnit parseCompilationUnit(Path root, Path sourcePath) throws IOException { parser.setUnitName("/" + root.relativize(sourcePath).toString()); - - val javaFileBytes = Files.readAllBytes(sourcePath); - val source = Charsets.UTF_8.decode(ByteBuffer.wrap(javaFileBytes)); + final byte[] javaFileBytes = Files.readAllBytes(sourcePath); + CharBuffer source = Charsets.UTF_8.decode(ByteBuffer.wrap(javaFileBytes)); parser.setSource(source.array()); return (CompilationUnit)parser.createAST(null); } -} +}
\ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index 12c9b6e..36c6b9c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -1,8 +1,8 @@ package org.zwobble.couscous.frontends.java; +import java.io.IOException; import java.nio.file.Path; import java.util.List; - import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.BooleanLiteral; @@ -15,6 +15,7 @@ import org.eclipse.jdt.core.dom.FieldAccess; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.IAnnotationBinding; import org.eclipse.jdt.core.dom.IBinding; +import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.MethodDeclaration; @@ -47,272 +48,236 @@ import org.zwobble.couscous.ast.StaticMethodCallNode; import org.zwobble.couscous.ast.TernaryConditionalNode; import org.zwobble.couscous.ast.ThisReferenceNode; import org.zwobble.couscous.ast.TypeName; - import com.google.common.collect.Lists; - import static java.util.Arrays.asList; import static org.zwobble.couscous.ast.ExpressionStatementNode.expressionStatement; import static org.zwobble.couscous.ast.LocalVariableDeclarationNode.localVariableDeclaration; import static org.zwobble.couscous.ast.VariableDeclaration.var; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; -import lombok.SneakyThrows; -import lombok.val; - public class JavaReader { + private final JavaParser parser = new JavaParser(); - @SneakyThrows - public ClassNode readClassFromFile(Path root, Path sourcePath) { - val ast = parser.parseCompilationUnit(root, sourcePath); + public ClassNode readClassFromFile(Path root, Path sourcePath) throws IOException { + CompilationUnit ast = parser.parseCompilationUnit(root, sourcePath); return readCompilationUnit(ast); } - + private static ClassNode readCompilationUnit(CompilationUnit ast) { - val name = generateClassName(ast); - - val classBuilder = new ClassNodeBuilder(name); - - val type = (TypeDeclaration)ast.types().get(0); - + String name = generateClassName(ast); + ClassNodeBuilder classBuilder = new ClassNodeBuilder(name); + TypeDeclaration type = (TypeDeclaration)ast.types().get(0); readFields(type, classBuilder); readMethods(type, classBuilder); - - return classBuilder - .build(); + return classBuilder.build(); } - + private static void readFields(TypeDeclaration type, ClassNodeBuilder classBuilder) { - for (val field : type.getFields()) { + for (FieldDeclaration field : type.getFields()) { readField(field, classBuilder); } } - + private static void readField(FieldDeclaration field, ClassNodeBuilder classBuilder) { - for (val fragment : field.fragments()) { - val name = ((VariableDeclarationFragment)fragment).getName().getIdentifier(); - classBuilder.field(name, typeOf(field.getType())); + for (Object fragment : field.fragments()) { + String name = ((VariableDeclarationFragment)fragment).getName().getIdentifier(); + classBuilder.field(name, typeOf(field.getType())); } } - + private static void readMethods(TypeDeclaration type, ClassNodeBuilder classBuilder) { - for (val method : type.getMethods()) { + for (MethodDeclaration method : type.getMethods()) { readMethod(classBuilder, method); } } - + private static void readMethod(ClassNodeBuilder classBuilder, MethodDeclaration method) { if (method.isConstructor()) { classBuilder.constructor(builder -> buildMethod(method, builder)); } else { - classBuilder.method( - method.getName().getIdentifier(), - true, - builder -> buildMethod(method, builder)); + classBuilder.method(method.getName().getIdentifier(), true, builder -> buildMethod(method, builder)); } } - - private static <T> MethodBuilder<T> buildMethod( - MethodDeclaration method, - MethodBuilder<T> builder) { + + private static <T> MethodBuilder<T> buildMethod(MethodDeclaration method, MethodBuilder<T> builder) { for (IAnnotationBinding annotation : method.resolveBinding().getAnnotations()) { builder.annotation(typeOf(annotation.getAnnotationType())); } for (Object parameterObject : method.parameters()) { - val parameter = (SingleVariableDeclaration)parameterObject; - builder.argument( - parameter.resolveBinding().getKey(), - parameter.getName().getIdentifier(), - typeOf(parameter.resolveBinding())); + SingleVariableDeclaration parameter = (SingleVariableDeclaration)parameterObject; + builder.argument(parameter.resolveBinding().getKey(), parameter.getName().getIdentifier(), typeOf(parameter.resolveBinding())); } for (Object statement : method.getBody().statements()) { for (StatementNode intermediateStatement : readStatement((Statement)statement)) { - builder.statement(intermediateStatement); + builder.statement(intermediateStatement); } - } return builder; } - + private static List<StatementNode> readStatement(Statement statement) { switch (statement.getNodeType()) { - case ASTNode.RETURN_STATEMENT: - return asList(readReturnStatement((ReturnStatement)statement)); - case ASTNode.EXPRESSION_STATEMENT: - return asList(readExpressionStatement((ExpressionStatement)statement)); - case ASTNode.VARIABLE_DECLARATION_STATEMENT: - return readVariableDeclarationStatement((VariableDeclarationStatement)statement); - default: - throw new RuntimeException("Unsupported statement: " + statement.getClass()); + case ASTNode.RETURN_STATEMENT: + return asList(readReturnStatement((ReturnStatement)statement)); + + case ASTNode.EXPRESSION_STATEMENT: + return asList(readExpressionStatement((ExpressionStatement)statement)); + + case ASTNode.VARIABLE_DECLARATION_STATEMENT: + return readVariableDeclarationStatement((VariableDeclarationStatement)statement); + + default: + throw new RuntimeException("Unsupported statement: " + statement.getClass()); + } } - + private static StatementNode readReturnStatement(ReturnStatement statement) { return ReturnNode.returns(readExpression(statement.getExpression())); } - + private static StatementNode readExpressionStatement(ExpressionStatement statement) { return expressionStatement(readExpression(statement.getExpression())); } - + private static List<StatementNode> readVariableDeclarationStatement(VariableDeclarationStatement statement) { @SuppressWarnings("unchecked") - val fragments = (List<VariableDeclarationFragment>)statement.fragments(); - val type = typeOf(statement.getType()); - return Lists.transform(fragments, fragment -> - localVariableDeclaration( - fragment.resolveBinding().getKey(), - fragment.getName().getIdentifier(), - type, - readExpression(fragment.getInitializer()))); + List<VariableDeclarationFragment> fragments = (List<VariableDeclarationFragment>)statement.fragments(); + TypeName type = typeOf(statement.getType()); + return Lists.transform(fragments, fragment -> localVariableDeclaration(fragment.resolveBinding().getKey(), fragment.getName().getIdentifier(), type, readExpression(fragment.getInitializer()))); } - + private static ExpressionNode readExpression(Expression expression) { switch (expression.getNodeType()) { - case ASTNode.BOOLEAN_LITERAL: - return readBooleanLiteral((BooleanLiteral)expression); - case ASTNode.NUMBER_LITERAL: - return readNumberLiteral((NumberLiteral)expression); - case ASTNode.STRING_LITERAL: - return readStringLiteral((StringLiteral)expression); - case ASTNode.SIMPLE_NAME: - return readSimpleName((SimpleName)expression); - case ASTNode.THIS_EXPRESSION: - return readThisExpression((ThisExpression)expression); - case ASTNode.FIELD_ACCESS: - return readFieldAccess((FieldAccess)expression); - case ASTNode.METHOD_INVOCATION: - return readMethodInvocation((MethodInvocation)expression); - case ASTNode.CLASS_INSTANCE_CREATION: - return readClassInstanceCreation((ClassInstanceCreation)expression); - case ASTNode.CONDITIONAL_EXPRESSION: - return readConditionalExpression((ConditionalExpression)expression); - case ASTNode.ASSIGNMENT: - return readAssignment((Assignment)expression); - default: - throw new RuntimeException("Unsupported expression: " + expression.getClass()); + case ASTNode.BOOLEAN_LITERAL: + return readBooleanLiteral((BooleanLiteral)expression); + + case ASTNode.NUMBER_LITERAL: + return readNumberLiteral((NumberLiteral)expression); + + case ASTNode.STRING_LITERAL: + return readStringLiteral((StringLiteral)expression); + + case ASTNode.SIMPLE_NAME: + return readSimpleName((SimpleName)expression); + + case ASTNode.THIS_EXPRESSION: + return readThisExpression((ThisExpression)expression); + + case ASTNode.FIELD_ACCESS: + return readFieldAccess((FieldAccess)expression); + + case ASTNode.METHOD_INVOCATION: + return readMethodInvocation((MethodInvocation)expression); + + case ASTNode.CLASS_INSTANCE_CREATION: + return readClassInstanceCreation((ClassInstanceCreation)expression); + + case ASTNode.CONDITIONAL_EXPRESSION: + return readConditionalExpression((ConditionalExpression)expression); + + case ASTNode.ASSIGNMENT: + return readAssignment((Assignment)expression); + + default: + throw new RuntimeException("Unsupported expression: " + expression.getClass()); + } } - + private static ExpressionNode readBooleanLiteral(BooleanLiteral expression) { return LiteralNode.literal(expression.booleanValue()); } - + private static LiteralNode readNumberLiteral(NumberLiteral expression) { return LiteralNode.literal(Integer.parseInt(expression.getToken())); } - + private static ExpressionNode readStringLiteral(StringLiteral expression) { return LiteralNode.literal(expression.getLiteralValue()); } - + private static ExpressionNode readSimpleName(SimpleName expression) { - val binding = expression.resolveBinding(); + IBinding binding = expression.resolveBinding(); if (binding.getKind() == IBinding.VARIABLE) { return readVariableBinding((IVariableBinding)binding); } else { throw new RuntimeException("Unsupported binding: " + binding.getClass()); } } - + private static ExpressionNode readVariableBinding(IVariableBinding binding) { - val type = typeOf(binding); + TypeName type = typeOf(binding); if (binding.getDeclaringClass() == null) { - return reference(var( - binding.getKey(), - binding.getName(), - type)); + return reference(var(binding.getKey(), binding.getName(), type)); } else { - return FieldAccessNode.fieldAccess( - ThisReferenceNode.thisReference(typeOf(binding.getDeclaringClass())), - binding.getName(), - type); + return FieldAccessNode.fieldAccess(ThisReferenceNode.thisReference(typeOf(binding.getDeclaringClass())), binding.getName(), type); } } - + private static ExpressionNode readThisExpression(ThisExpression expression) { return ThisReferenceNode.thisReference(typeOf(expression)); } private static ExpressionNode readFieldAccess(FieldAccess expression) { - return FieldAccessNode.fieldAccess( - readExpression(expression.getExpression()), - expression.getName().getIdentifier(), - typeOf(expression)); + return FieldAccessNode.fieldAccess(readExpression(expression.getExpression()), expression.getName().getIdentifier(), typeOf(expression)); } - + private static ExpressionNode readMethodInvocation(MethodInvocation expression) { - val methodName = expression.getName().getIdentifier(); - val arguments = readArguments(expression.arguments()); + String methodName = expression.getName().getIdentifier(); + List<ExpressionNode> arguments = readArguments(expression.arguments()); if (expression.getExpression() == null) { - val methodBinding = expression.resolveMethodBinding(); - val receiverType = typeOf(methodBinding.getDeclaringClass()); + IMethodBinding methodBinding = expression.resolveMethodBinding(); + TypeName receiverType = typeOf(methodBinding.getDeclaringClass()); if ((methodBinding.getModifiers() & Modifier.STATIC) != 0) { - return StaticMethodCallNode.staticMethodCall( - receiverType, methodName, arguments); + return StaticMethodCallNode.staticMethodCall(receiverType, methodName, arguments); } else { - return MethodCallNode.methodCall( - ThisReferenceNode.thisReference(receiverType), - methodName, - arguments, - typeOf(expression)); + return MethodCallNode.methodCall(ThisReferenceNode.thisReference(receiverType), methodName, arguments, typeOf(expression)); } } else if (expression.getExpression().getNodeType() == ASTNode.SIMPLE_NAME) { - val receiver = expression.getExpression(); + Expression receiver = expression.getExpression(); return StaticMethodCallNode.staticMethodCall(typeOf(receiver), methodName, arguments); } else { - return MethodCallNode.methodCall( - readExpression(expression.getExpression()), - methodName, - arguments, - typeOf(expression)); + return MethodCallNode.methodCall(readExpression(expression.getExpression()), methodName, arguments, typeOf(expression)); } } - + private static ExpressionNode readClassInstanceCreation(ClassInstanceCreation expression) { - return ConstructorCallNode.constructorCall( - typeOf(expression), - readArguments(expression.arguments())); + return ConstructorCallNode.constructorCall(typeOf(expression), readArguments(expression.arguments())); } - - @SuppressWarnings({ "unchecked", "rawtypes" }) + + @SuppressWarnings({"unchecked", "rawtypes"}) private static List<ExpressionNode> readArguments(final List javaArguments) { - return Lists.transform( - (List<Expression>)javaArguments, - JavaReader::readExpression); + return Lists.transform((List<Expression>)javaArguments, JavaReader::readExpression); } - + private static ExpressionNode readConditionalExpression(ConditionalExpression expression) { - return TernaryConditionalNode.ternaryConditional( - readExpression(expression.getExpression()), - readExpression(expression.getThenExpression()), - readExpression(expression.getElseExpression())); + return TernaryConditionalNode.ternaryConditional(readExpression(expression.getExpression()), readExpression(expression.getThenExpression()), readExpression(expression.getElseExpression())); } - + private static ExpressionNode readAssignment(Assignment expression) { - return AssignmentNode.assign( - (AssignableExpressionNode)readExpression(expression.getLeftHandSide()), - readExpression(expression.getRightHandSide())); + return AssignmentNode.assign((AssignableExpressionNode)readExpression(expression.getLeftHandSide()), readExpression(expression.getRightHandSide())); } - + private static String generateClassName(CompilationUnit ast) { - val type = (TypeDeclaration)ast.types().get(0); + TypeDeclaration type = (TypeDeclaration)ast.types().get(0); return ast.getPackage().getName().getFullyQualifiedName() + "." + type.getName().getFullyQualifiedName(); } - + private static TypeName typeOf(Expression expression) { return typeOf(expression.resolveTypeBinding()); } - + private static TypeName typeOf(IVariableBinding variableBinding) { return typeOf(variableBinding.getType()); } - + private static TypeName typeOf(Type type) { return typeOf(type.resolveBinding()); } - + private static TypeName typeOf(ITypeBinding typeBinding) { return TypeName.of(typeBinding.getQualifiedName()); } -} +}
\ No newline at end of file |
