Remove unnecessary generic type in Collections.empty*,

replace "for" on Stream API,
replace anonymous classes on lambda
This commit is contained in:
Andrei Arlou 2019-08-11 10:10:37 +03:00 committed by Filip Hrisafov
parent e5c5550182
commit f02b3d1a42
13 changed files with 44 additions and 87 deletions

View File

@ -341,10 +341,7 @@ public class MappingProcessor extends AbstractProcessor {
@Override @Override
public int compare(ModelElementProcessor<?, ?> o1, ModelElementProcessor<?, ?> o2) { public int compare(ModelElementProcessor<?, ?> o1, ModelElementProcessor<?, ?> o2) {
return return Integer.compare( o1.getPriority(), o2.getPriority() );
o1.getPriority() < o2.getPriority() ? -1 :
o1.getPriority() == o2.getPriority() ? 0 :
1;
} }
} }
} }

View File

@ -15,7 +15,6 @@ import org.mapstruct.ap.internal.model.HelperMethod;
import org.mapstruct.ap.internal.model.TypeConversion; import org.mapstruct.ap.internal.model.TypeConversion;
import org.mapstruct.ap.internal.model.common.Assignment; import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.ConversionContext; import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Type;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.mapstruct.ap.internal.util.Collections.asSet; import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -31,7 +30,7 @@ public class DateToStringConversion implements ConversionProvider {
@Override @Override
public Assignment to(ConversionContext conversionContext) { public Assignment to(ConversionContext conversionContext) {
return new TypeConversion( asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), return new TypeConversion( asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ),
Collections.<Type>emptyList(), Collections.emptyList(),
getConversionExpression( conversionContext, "format" ) getConversionExpression( conversionContext, "format" )
); );
} }

View File

@ -27,7 +27,7 @@ public class Annotation extends ModelElement {
private List<String> properties; private List<String> properties;
public Annotation(Type type) { public Annotation(Type type) {
this( type, Collections.<String>emptyList() ); this( type, Collections.emptyList() );
} }
public Annotation(Type type, List<String> properties) { public Annotation(Type type, List<String> properties) {

View File

@ -17,9 +17,11 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
@ -104,7 +106,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
singleMapping = singleMapping =
targetName -> getMappingByTargetName( targetName, sourceMethod.getMappingOptions().getMappings() ); targetName -> getMappingByTargetName( targetName, sourceMethod.getMappingOptions().getMappings() );
mappingsInitializer = mappingsInitializer =
mappings -> mappings.stream().forEach( mapping -> initReferencesForSourceMethodMapping( mapping ) ); mappings -> mappings.stream().forEach( this::initReferencesForSourceMethodMapping );
this.method = sourceMethod; this.method = sourceMethod;
return this; return this;
} }
@ -112,7 +114,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
public Builder forgedMethod(Method method) { public Builder forgedMethod(Method method) {
singleMapping = targetPropertyName -> null; singleMapping = targetPropertyName -> null;
mappingsInitializer = mappingsInitializer =
mappings -> mappings.stream().forEach( mapping -> initReferencesForForgedMethodMapping( mapping ) ); mappings -> mappings.stream().forEach( this::initReferencesForForgedMethodMapping );
this.method = method; this.method = method;
return this; return this;
} }
@ -177,8 +179,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
continue; continue;
} }
Map<String, Accessor> readAccessors = sourceParameter.getType().getPropertyReadAccessors(); Map<String, Accessor> readAccessors = sourceParameter.getType().getPropertyReadAccessors();
for ( String key : readAccessors.keySet() ) {
unprocessedSourceProperties.put( key, readAccessors.get( key ) ); for ( Entry<String, Accessor> entry : readAccessors.entrySet() ) {
unprocessedSourceProperties.put( entry.getKey(), entry.getValue() );
} }
} }
existingVariableNames.addAll( method.getParameterNames() ); existingVariableNames.addAll( method.getParameterNames() );
@ -191,8 +194,8 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
} }
// map properties with mapping // map properties with mapping
boolean mappingErrorOccured = handleDefinedMappings(); boolean mappingErrorOccurred = handleDefinedMappings();
if ( mappingErrorOccured ) { if ( mappingErrorOccurred ) {
return null; return null;
} }
@ -366,13 +369,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
} }
else { else {
Collections.sort( Collections.sort(
propertyMappings, new Comparator<PropertyMapping>() { propertyMappings,
@Override Comparator.comparingInt( propertyMapping ->
public int compare(PropertyMapping o1, PropertyMapping o2) { graphAnalyzer.getTraversalSequence( propertyMapping.getName() ) )
return graphAnalyzer.getTraversalSequence( o1.getName() )
- graphAnalyzer.getTraversalSequence( o2.getName() );
}
}
); );
} }
} }
@ -512,9 +511,8 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
*/ */
private static boolean isValidWhenReversed(Mapping mapping) { private static boolean isValidWhenReversed(Mapping mapping) {
if ( mapping.getInheritContext() != null && mapping.getInheritContext().isReversed() ) { if ( mapping.getInheritContext() != null && mapping.getInheritContext().isReversed() ) {
return mapping.getTargetReference().isValid() && ( mapping.getSourceReference() != null ? return mapping.getTargetReference().isValid() && ( mapping.getSourceReference() == null ||
mapping.getSourceReference().isValid() : mapping.getSourceReference().isValid() );
true );
} }
return true; return true;
} }
@ -1051,24 +1049,15 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
} }
public List<Parameter> getSourceParametersExcludingPrimitives() { public List<Parameter> getSourceParametersExcludingPrimitives() {
List<Parameter> sourceParameters = new ArrayList<>(); return getSourceParameters().stream()
for ( Parameter sourceParam : getSourceParameters() ) { .filter( parameter -> !parameter.getType().isPrimitive() )
if ( !sourceParam.getType().isPrimitive() ) { .collect( Collectors.toList() );
sourceParameters.add( sourceParam );
}
}
return sourceParameters;
} }
public List<Parameter> getSourcePrimitiveParameters() { public List<Parameter> getSourcePrimitiveParameters() {
List<Parameter> sourceParameters = new ArrayList<>(); return getSourceParameters().stream()
for ( Parameter sourceParam : getSourceParameters() ) { .filter( parameter -> parameter.getType().isPrimitive() )
if ( sourceParam.getType().isPrimitive() ) { .collect( Collectors.toList() );
sourceParameters.add( sourceParam );
}
}
return sourceParameters;
} }
@Override @Override
@ -1092,8 +1081,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
if ( !super.equals( obj ) ) { if ( !super.equals( obj ) ) {
return false; return false;
} }
return propertyMappings != null ? propertyMappings.equals( that.propertyMappings ) : return Objects.equals( propertyMappings, that.propertyMappings );
that.propertyMappings == null;
} }
private interface SingleMappingByTargetPropertyNameFunction { private interface SingleMappingByTargetPropertyNameFunction {

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
@ -123,9 +124,7 @@ public final class LifecycleMethodResolver {
List<SourceMethod> availableMethods = List<SourceMethod> availableMethods =
new ArrayList<>( methodsProvidedByParams.size() + sourceModelMethods.size() ); new ArrayList<>( methodsProvidedByParams.size() + sourceModelMethods.size() );
for ( SourceMethod methodProvidedByParams : methodsProvidedByParams ) { availableMethods.addAll( methodsProvidedByParams );
availableMethods.add( methodProvidedByParams );
}
availableMethods.addAll( sourceModelMethods ); availableMethods.addAll( sourceModelMethods );
return availableMethods; return availableMethods;
@ -141,7 +140,7 @@ public final class LifecycleMethodResolver {
List<SelectedMethod<SourceMethod>> matchingMethods = selectors.getMatchingMethods( List<SelectedMethod<SourceMethod>> matchingMethods = selectors.getMatchingMethods(
method, method,
callbackMethods, callbackMethods,
Collections.<Type> emptyList(), Collections.emptyList(),
targetType, targetType,
SelectionCriteria.forLifecycleMethods( selectionParameters ) ); SelectionCriteria.forLifecycleMethods( selectionParameters ) );
@ -185,24 +184,14 @@ public final class LifecycleMethodResolver {
} }
private static List<SourceMethod> filterBeforeMappingMethods(List<SourceMethod> methods) { private static List<SourceMethod> filterBeforeMappingMethods(List<SourceMethod> methods) {
List<SourceMethod> result = new ArrayList<>(); return methods.stream()
for ( SourceMethod method : methods ) { .filter( SourceMethod::isBeforeMappingMethod )
if ( method.isBeforeMappingMethod() ) { .collect( Collectors.toList() );
result.add( method );
}
}
return result;
} }
private static List<SourceMethod> filterAfterMappingMethods(List<SourceMethod> methods) { private static List<SourceMethod> filterAfterMappingMethods(List<SourceMethod> methods) {
List<SourceMethod> result = new ArrayList<>(); return methods.stream()
for ( SourceMethod method : methods ) { .filter( SourceMethod::isAfterMappingMethod )
if ( method.isAfterMappingMethod() ) { .collect( Collectors.toList() );
result.add( method );
}
}
return result;
} }
} }

View File

@ -235,8 +235,7 @@ public class MethodReference extends ModelElement implements Assignment {
@Override @Override
public List<Type> getThrownTypes() { public List<Type> getThrownTypes() {
List<Type> exceptions = new ArrayList<>(); List<Type> exceptions = new ArrayList<>( thrownTypes );
exceptions.addAll( thrownTypes );
if ( assignment != null ) { if ( assignment != null ) {
exceptions.addAll( assignment.getThrownTypes() ); exceptions.addAll( assignment.getThrownTypes() );
} }

View File

@ -68,12 +68,7 @@ import static org.mapstruct.ap.internal.model.common.ImplementationType.withLoad
public class TypeFactory { public class TypeFactory {
private static final Extractor<BuilderInfo, String> BUILDER_INFO_CREATION_METHOD_EXTRACTOR = private static final Extractor<BuilderInfo, String> BUILDER_INFO_CREATION_METHOD_EXTRACTOR =
new Extractor<BuilderInfo, String>() { builderInfo -> builderInfo.getBuilderCreationMethod().toString();
@Override
public String apply(BuilderInfo builderInfo) {
return builderInfo.getBuilderCreationMethod().toString();
}
};
private final Elements elementUtils; private final Elements elementUtils;
private final Types typeUtils; private final Types typeUtils;

View File

@ -85,7 +85,7 @@ public class MappingOptions {
null, null,
null, null,
forForgedMethods ? BeanMapping.forForgedMethods() : null, forForgedMethods ? BeanMapping.forForgedMethods() : null,
Collections.<ValueMapping>emptyList(), Collections.emptyList(),
restrictToDefinedMappings restrictToDefinedMappings
); );

View File

@ -144,8 +144,8 @@ public class SelectionParameters {
public static SelectionParameters forSourceRHS(SourceRHS sourceRHS) { public static SelectionParameters forSourceRHS(SourceRHS sourceRHS) {
return new SelectionParameters( return new SelectionParameters(
Collections.<TypeMirror>emptyList(), Collections.emptyList(),
Collections.<String>emptyList(), Collections.emptyList(),
null, null,
null, null,
sourceRHS sourceRHS

View File

@ -20,7 +20,6 @@ import javax.lang.model.type.DeclaredType;
import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.Extractor;
import org.mapstruct.ap.internal.util.FormattingMessager; import org.mapstruct.ap.internal.util.FormattingMessager;
import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.Strings; import org.mapstruct.ap.internal.util.Strings;
@ -214,12 +213,7 @@ public class SourceReference {
Strings.join( Strings.join(
method.getSourceParameters(), method.getSourceParameters(),
", ", ", ",
new Extractor<Parameter, String>() { Parameter::getName
@Override
public String apply(Parameter parameter) {
return parameter.getName();
}
}
) )
); );
} }

View File

@ -6,7 +6,6 @@
package org.mapstruct.ap.internal.util; package org.mapstruct.ap.internal.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -113,8 +112,8 @@ public class AnnotationProcessorContext implements MapStructProcessingEnvironmen
AstModifyingAnnotationProcessor.class, AnnotationProcessorContext.class.getClassLoader() AstModifyingAnnotationProcessor.class, AnnotationProcessorContext.class.getClassLoader()
); );
for ( Iterator<AstModifyingAnnotationProcessor> it = loader.iterator(); it.hasNext(); ) { for ( AstModifyingAnnotationProcessor astModifyingAnnotationProcessor : loader ) {
processors.add( it.next() ); processors.add( astModifyingAnnotationProcessor );
} }
return processors; return processors;

View File

@ -84,9 +84,7 @@ public class Fields {
private static void addFields(List<VariableElement> alreadyCollected, List<VariableElement> variablesToAdd) { private static void addFields(List<VariableElement> alreadyCollected, List<VariableElement> variablesToAdd) {
List<VariableElement> safeToAdd = new ArrayList<>( variablesToAdd.size() ); List<VariableElement> safeToAdd = new ArrayList<>( variablesToAdd.size() );
for ( VariableElement toAdd : variablesToAdd ) { safeToAdd.addAll( variablesToAdd );
safeToAdd.add( toAdd );
}
alreadyCollected.addAll( 0, safeToAdd ); alreadyCollected.addAll( 0, safeToAdd );
} }

View File

@ -99,8 +99,7 @@ public class MapperConfiguration {
} }
public List<TypeMirror> imports() { public List<TypeMirror> imports() {
List<TypeMirror> imports = new ArrayList<>(); List<TypeMirror> imports = new ArrayList<>( mapperPrism.imports() );
imports.addAll( mapperPrism.imports() );
if ( mapperConfigPrism != null ) { if ( mapperConfigPrism != null ) {
imports.addAll( mapperConfigPrism.imports() ); imports.addAll( mapperConfigPrism.imports() );
} }
@ -262,7 +261,7 @@ public class MapperConfiguration {
public boolean isDisableSubMappingMethodsGeneration() { public boolean isDisableSubMappingMethodsGeneration() {
if ( mapperPrism.disableSubMappingMethodsGeneration() ) { if ( mapperPrism.disableSubMappingMethodsGeneration() ) {
return mapperPrism.disableSubMappingMethodsGeneration(); return true;
} }
if ( mapperConfigPrism != null && mapperConfigPrism.disableSubMappingMethodsGeneration() ) { if ( mapperConfigPrism != null && mapperConfigPrism.disableSubMappingMethodsGeneration() ) {