View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.module.osgi
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.module.jpms.parser;
20  
21  import static java.util.Locale.ROOT;
22  import static java.util.stream.Collectors.toSet;
23  
24  import java.util.Set;
25  import java.util.TreeSet;
26  import java.util.stream.Stream;
27  
28  import io.earcam.instrumental.module.jpms.ModuleInfo;
29  import io.earcam.instrumental.module.jpms.ModuleInfoBuilder;
30  import io.earcam.instrumental.module.jpms.RequireModifier;
31  import io.earcam.instrumental.module.jpms.parser.Java9BaseListener;
32  import io.earcam.instrumental.module.jpms.parser.Java9Parser.ExportsDirectiveContext;
33  import io.earcam.instrumental.module.jpms.parser.Java9Parser.ModuleDeclarationContext;
34  import io.earcam.instrumental.module.jpms.parser.Java9Parser.ModuleNameContext;
35  import io.earcam.instrumental.module.jpms.parser.Java9Parser.OpensDirectiveContext;
36  import io.earcam.instrumental.module.jpms.parser.Java9Parser.ProvidesDirectiveContext;
37  import io.earcam.instrumental.module.jpms.parser.Java9Parser.RequiresDirectiveContext;
38  import io.earcam.instrumental.module.jpms.parser.Java9Parser.RequiresModifierContext;
39  import io.earcam.instrumental.module.jpms.parser.Java9Parser.UsesDirectiveContext;
40  
41  // TODO annotations, and possibly comments?
42  /**
43   * <p>
44   * ModuleInfoParser class.
45   * </p>
46   *
47   */
48  class AntlrParser extends Java9BaseListener {
49  
50  	private final ModuleInfoBuilder builder = ModuleInfo.moduleInfo();
51  
52  
53  	/**
54  	 * <p>
55  	 * builder.
56  	 * </p>
57  	 *
58  	 * @return a {@link io.earcam.instrumental.module.jpms.ModuleInfoBuilder} object.
59  	 */
60  	public ModuleInfoBuilder builder()
61  	{
62  		return builder;
63  	}
64  
65  
66  	@Override
67  	public void enterModuleDeclaration(ModuleDeclarationContext ctx)
68  	{
69  		super.enterModuleDeclaration(ctx);
70  		builder.named(ctx.moduleName().getText());
71  	}
72  
73  
74  	@Override
75  	public void enterRequiresDirective(RequiresDirectiveContext ctx)
76  	{
77  		super.enterRequiresDirective(ctx);
78  		Set<RequireModifier> modifiers = ctx.requiresModifier().stream()
79  				.map(RequiresModifierContext::getText)
80  				.map(m -> m.toUpperCase(ROOT))
81  				.map(RequireModifier::valueOf)
82  				.collect(toSet());
83  
84  		builder.requiring(ctx.moduleName().getText(), modifiers, null);
85  	}
86  
87  
88  	@Override
89  	public void enterExportsDirective(ExportsDirectiveContext ctx)
90  	{
91  		super.enterExportsDirective(ctx);
92  		String[] to = toModuleNames(ctx.moduleName().stream());
93  		builder.exporting(ctx.packageName().getText(), to);
94  	}
95  
96  
97  	private String[] toModuleNames(Stream<ModuleNameContext> stream)
98  	{
99  		return stream
100 				.map(ModuleNameContext::getText)
101 				.toArray(s -> new String[s]);
102 	}
103 
104 
105 	@Override
106 	public void enterOpensDirective(OpensDirectiveContext ctx)
107 	{
108 		super.enterOpensDirective(ctx);
109 		String[] to = toModuleNames(ctx.moduleName().stream());
110 		builder.opening(ctx.packageName().getText(), to);
111 	}
112 
113 
114 	@Override
115 	public void enterUsesDirective(UsesDirectiveContext ctx)
116 	{
117 		super.enterUsesDirective(ctx);
118 		builder.using(ctx.getChild(1).getText());
119 	}
120 
121 
122 	@Override
123 	public void enterProvidesDirective(ProvidesDirectiveContext ctx)
124 	{
125 		super.enterProvidesDirective(ctx);
126 
127 		TreeSet<String> concretes = new TreeSet<>();
128 		for(int i = 3; i < ctx.getChildCount(); i += 2) {
129 			concretes.add(ctx.getChild(i).getText());
130 		}
131 
132 		builder.providing(ctx.getChild(1).getText(), concretes);
133 	}
134 }