
br.com.objectos.metainf.ServicesCompiler Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014-2015 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.metainf;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;
import br.com.objectos.code.AbstractAnnotationProcessor;
import br.com.objectos.code.Artifact;
import br.com.objectos.code.Configuration;
import br.com.objectos.code.ProcessorListener;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.TypeInfo;
/**
* @author [email protected] (Marcio Endo)
*/
public class ServicesCompiler extends AbstractAnnotationProcessor implements ProcessorListener {
private final Set artifactSet = new LinkedHashSet<>();
@Override
public void onFinish(Set extends TypeElement> typeElements, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
artifactSet.stream()
.collect(Collectors.groupingBy(ThisArtifact::service))
.forEach(this::write);
}
}
@Override
protected Configuration configuration() {
return Configuration.builder()
.addAnnotationType(Services.class)
.addTypeInfoArtifactGenerator(this::generate)
.listener(this)
.build();
}
private Artifact generate(TypeInfo typeInfo) {
SimpleTypeInfo serviceTypeInfo = typeInfo.annotationInfo(Services.class)
.flatMap(ann -> ann.simpleTypeInfoValue("value"))
.get();
return new ThisArtifact(serviceTypeInfo.qualifiedName(), typeInfo.qualifiedName());
}
private void write(String service, List artifactList) {
Artifact artifact = Artifact.resourceNamed("META-INF/services/" + service)
.addAll(artifactList.stream().map(ThisArtifact::implementation))
.build();
execute(artifact);
}
private class ThisArtifact extends Artifact {
private final String service;
private final String implementation;
public ThisArtifact(String service, String implementation) {
this.service = service;
this.implementation = implementation;
}
@Override
public int hashCode() {
return Objects.hash(service, implementation);
}
@Override
public boolean equals(Object obj) {
ThisArtifact that = (ThisArtifact) obj;
return service.equals(that.service)
&& implementation.equals(that.implementation);
}
@Override
protected void execute(ProcessingEnvironment processingEnv) {
artifactSet.add(this);
}
@Override
public void writeTo(Path path) {
throw new UnsupportedOperationException();
}
String implementation() {
return implementation;
}
String service() {
return service;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy