org.parceler.internal.ExternalParcelTransactionWorker Maven / Gradle / Ivy
/**
* Copyright 2011-2015 John Ericksen
*
* 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 org.parceler.internal;
import org.androidtransfuse.adapter.ASTAnnotation;
import org.androidtransfuse.adapter.ASTType;
import org.androidtransfuse.transaction.AbstractCompletionTransactionWorker;
import org.parceler.Parcel;
import org.parceler.ParcelClass;
import org.parceler.ParcelClasses;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.HashSet;
import java.util.Set;
/**
* Executes the analysis and generation of an annotated @Parcel class.
*
* @author John Ericksen
*/
public class ExternalParcelTransactionWorker extends AbstractCompletionTransactionWorker, Void> {
private final ParcelableAnalysis parcelableAnalysis;
private final ParcelableGenerator parcelableGenerator;
private final Set analyzed = new HashSet();
@Inject
public ExternalParcelTransactionWorker(ParcelableAnalysis parcelableAnalysis, ParcelableGenerator parcelableGenerator) {
this.parcelableAnalysis = parcelableAnalysis;
this.parcelableGenerator = parcelableGenerator;
}
@Override
public Void innerRun(Provider valueProvider) {
ASTType value = valueProvider.get();
ASTAnnotation parcelClassesAnnotation = value.getASTAnnotation(ParcelClasses.class);
if(parcelClassesAnnotation != null){
ASTAnnotation[] parcelTypes = parcelClassesAnnotation.getProperty("value", ASTAnnotation[].class);
for(ASTAnnotation annotation : parcelTypes){
analyze(annotation);
}
}
ASTAnnotation astAnnotation = value.getASTAnnotation(ParcelClass.class);
if(astAnnotation != null){
analyze(astAnnotation);
}
return null;
}
private void analyze(ASTAnnotation astAnnotation){
ASTType parcelType = astAnnotation.getProperty("value", ASTType.class);
if(!analyzed.contains(parcelType)) {
analyzed.add(parcelType);
ASTAnnotation parcelASTAnnotation = astAnnotation.getProperty("annotation", ASTAnnotation.class);
if (parcelASTAnnotation == null) {
parcelASTAnnotation = parcelType.getASTAnnotation(Parcel.class);
}
ParcelableDescriptor analysis = parcelableAnalysis.analyze(parcelType, parcelASTAnnotation);
if (analysis != null) {
parcelableGenerator.generateParcelable(parcelType, analysis);
}
}
}
}