public void compile(List sourceFileObjects,
List classnames,
Iterable<? extends Processor> processors)
{
if (processors != null && processors.iterator().hasNext())
explicitAnnotationProcessingRequested = true;
// as a JavaCompiler can only be used once, throw an exception if
// it has been used before.
if (hasBeenUsed)
throw new AssertionError("attempt to reuse JavaCompiler");
hasBeenUsed = true;
// forcibly set the equivalent of -Xlint:-options, so that no further
// warnings about command line options are generated from this point on
options.put(XLINT_CUSTOM.text + "-" + LintCategory.OPTIONS.option, "true");
options.remove(XLINT_CUSTOM.text + LintCategory.OPTIONS.option);
start_msec = now();
try {
initProcessAnnotations(processors);
//此处会调用到mapStruct中的processor类的方法.
delegateCompiler =
processAnnotations(
enterTrees(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))),
classnames);
delegateCompiler.compile2();
delegateCompiler.close();
elapsed_msec = delegateCompiler.elapsed_msec;
} catch (Abort ex) {
if (devVerbose)
ex.printStackTrace(System.err);
} finally {
if (procEnvImpl != null)
procEnvImpl.close();
}
}
关键代码,在mapstruct-processor包中,有个对应的类MappingProcessor继承了 AbstractProcessor,并实现其 process 方法。通过对 AST 进行相应的代码增强,从而实现对最终编译的对象进行修改的方法。
@SupportedAnnotationTypes({"org.mapstruct.Mapper"})
@SupportedOptions({"mapstruct.suppressGeneratorTimestamp", "mapstruct.suppressGeneratorVersionInfoComment", "mapstruct.unmappedTargetPolicy", "mapstruct.unmappedSourcePolicy", "mapstruct.defaultComponentModel", "mapstruct.defaultInjectionStrategy", "mapstruct.disableBuilders", "mapstruct.verbose"})
public class MappingProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
if (!roundEnvironment.processingOver()) {
RoundContext roundContext = new RoundContext(this.annotationProcessorContext);
Set deferredMappers = this.getAndResetDeferredMappers();
this.processMapperElements(deferredMappers, roundContext);
Set mappers = this.getMappers(annotations, roundEnvironment);
this.processMapperElements(mappers, roundContext);
} else if (!this.deferredMappers.isEmpty()) {
Iterator var8 = this.deferredMappers.iterator();
while(var8.hasNext()) {
MappingProcessor.DeferredMapper deferredMapper = (MappingProcessor.DeferredMapper)var8.next();
TypeElement deferredMapperElement = deferredMapper.deferredMapperElement;
Element erroneousElement = deferredMapper.erroneousElement;
String erroneousElementName;
if (erroneousElement instanceof QualifiedNameable) {
erroneousElementName = ((QualifiedNameable)erroneousElement).getQualifiedName().toString();
} else {
erroneousElementName = erroneousElement != null ? erroneousElement.getSimpleName().toString() : null;
}
deferredMapperElement = this.annotationProcessorContext.getElementUtils().getTypeElement(deferredMapperElement.getQualifiedName());
this.processingEnv.getMessager().printMessage(Kind.ERROR, "No implementation was created for " + deferredMapperElement.getSimpleName() + " due to having a problem in the erroneous element " + erroneousElementName + ". Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.", deferredMapperElement);
}
}
return false;
}
}
「如何断点调试:」
因为这个注解处理器是在解析->编译的过程完成,跟普通的 jar 包调试不太一样,maven 框架为我们提供了调试入口,需要借助 maven 才能实现 debug。所以需要在编译过程打开 debug 才可调试。
public class MapperStructService {
public UserInfoVO newCopyItem(UserDTO userDTO, int times) {
UserInfoVO userInfoVO = new UserInfoVO();
for (int i = 0; i < times; i++) {
userInfoVO = InfoConverter.INSTANT.convert(userDTO);
}
return userInfoVO;
}
public UserInfoVO originalCopyItem(UserDTO userDTO, int times) {
UserInfoVO userInfoVO = new UserInfoVO();
for (int i = 0; i < times; i++) {
userInfoVO.setUserName(userDTO.getName());
userInfoVO.setAge(userDTO.getAge());
userInfoVO.setBirthday(userDTO.getBirthday());
userInfoVO.setIdCard(userDTO.getIdCard());
userInfoVO.setGender(userDTO.getGender());
userInfoVO.setIsMarried(userDTO.getIsMarried());
userInfoVO.setPhoneNumber(userDTO.getPhoneNumber());
userInfoVO.setAddress(userDTO.getAddress());
}
return userInfoVO;
}
public UserInfoVO utilCopyItem(UserDTO userDTO, int times) {
UserInfoVO userInfoVO = new UserInfoVO();
for (int i = 0; i < times; i++) {
BeanUtils.copyProperties(userDTO, userInfoVO);
}
return userInfoVO;
}
}