Assembler for field. @author Matt
| 11 | * @author Matt |
| 12 | */ |
| 13 | public class FieldAssembler implements Assembler<FieldNode> { |
| 14 | @Override |
| 15 | public FieldNode compile(ParseResult<RootAST> result) throws AssemblerException { |
| 16 | if(!result.isSuccess()) { |
| 17 | ASTParseException cause = result.getProblems().get(0); |
| 18 | AssemblerException ex = new AssemblerException(cause, "AST must not contain errors", cause.getLine()); |
| 19 | ex.addSubExceptions(result.getProblems()); |
| 20 | throw ex; |
| 21 | } |
| 22 | RootAST root = result.getRoot(); |
| 23 | // Get definition |
| 24 | FieldDefinitionAST definition = root.search(FieldDefinitionAST.class).stream().findFirst().orElse(null); |
| 25 | if (definition == null) |
| 26 | throw new AssemblerException("AST must have definition statement"); |
| 27 | int access = toAccess(definition); |
| 28 | String name = definition.getName().getName(); |
| 29 | String desc = definition.getType().getDesc(); |
| 30 | SignatureAST signatureAST = root.search(SignatureAST.class).stream().findFirst().orElse(null); |
| 31 | DefaultValueAST defaultValueAST = root.search(DefaultValueAST.class).stream().findFirst().orElse(null); |
| 32 | String signature = (signatureAST == null) ? null : signatureAST.getSignature(); |
| 33 | Object value = (defaultValueAST == null) ? null : defaultValueAST.toValue(); |
| 34 | return new FieldNode(access, name, desc, signature, value); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param definition |
| 39 | * AST of definition. Contains modifier AST children. |
| 40 | * |
| 41 | * @return Combined value of modifier children. |
| 42 | */ |
| 43 | private int toAccess(FieldDefinitionAST definition) { |
| 44 | return definition.search(DefinitionModifierAST.class).stream() |
| 45 | .mapToInt(DefinitionModifierAST::getValue) |
| 46 | .reduce(0, (a, b) -> a | b); |
| 47 | } |
| 48 | } |
nothing calls this directly
no outgoing calls
no test coverage detected