use of com.squareup.javapoet.TypeName in project epoxy by airbnb.
the class EpoxyProcessor method generateEquals.
private MethodSpec generateEquals(ClassToGenerateInfo helperClass) {
Builder builder = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(boolean.class).addParameter(Object.class, "o").beginControlFlow("if (o == this)").addStatement("return true").endControlFlow().beginControlFlow("if (!(o instanceof $T))", helperClass.getGeneratedName()).addStatement("return false").endControlFlow().beginControlFlow("if (!super.equals(o))").addStatement("return false").endControlFlow().addStatement("$T that = ($T) o", helperClass.getGeneratedName(), helperClass.getGeneratedName());
for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
TypeName type = attributeInfo.getType();
if (!attributeInfo.useInHash() && type.isPrimitive()) {
continue;
}
String name = attributeInfo.getName();
if (attributeInfo.useInHash()) {
if (type == FLOAT) {
builder.beginControlFlow("if (Float.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
} else if (type == DOUBLE) {
builder.beginControlFlow("if (Double.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
} else if (type.isPrimitive()) {
builder.beginControlFlow("if ($L != that.$L)", name, name).addStatement("return false").endControlFlow();
} else if (type instanceof ArrayTypeName) {
builder.beginControlFlow("if (!$T.equals($L, that.$L))", TypeName.get(Arrays.class), name, name).addStatement("return false").endControlFlow();
} else {
builder.beginControlFlow("if ($L != null ? !$L.equals(that.$L) : that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
}
} else {
builder.beginControlFlow("if ($L != null && that.$L == null" + " || $L == null && that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
}
}
return builder.addStatement("return true").build();
}
use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.
the class BindingSet method addMethodBindings.
private void addMethodBindings(MethodSpec.Builder result, ViewBinding binding) {
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = binding.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = binding.getRequiredBindings().isEmpty();
if (needsNullChecked) {
result.beginControlFlow("if (view != null)");
}
// Add the view reference to the binding.
String fieldName = "viewSource";
String bindName = "source";
if (!binding.isBoundToRoot()) {
fieldName = "view" + binding.getId().value;
bindName = "view";
}
result.addStatement("$L = $N", fieldName, bindName);
for (Map.Entry<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> e : classMethodBindings.entrySet()) {
ListenerClass listener = e.getKey();
Map<ListenerMethod, Set<MethodViewBinding>> methodBindings = e.getValue();
TypeSpec.Builder callback = TypeSpec.anonymousClassBuilder("").superclass(ClassName.bestGuess(listener.type()));
for (ListenerMethod method : getListenerMethods(listener)) {
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name()).addAnnotation(Override.class).addModifiers(PUBLIC).returns(bestGuess(method.returnType()));
String[] parameterTypes = method.parameters();
for (int i = 0, count = parameterTypes.length; i < count; i++) {
callbackMethod.addParameter(bestGuess(parameterTypes[i]), "p" + i);
}
boolean hasReturnType = !"void".equals(method.returnType());
CodeBlock.Builder builder = CodeBlock.builder();
if (hasReturnType) {
builder.add("return ");
}
if (methodBindings.containsKey(method)) {
for (MethodViewBinding methodBinding : methodBindings.get(method)) {
builder.add("target.$L(", methodBinding.getName());
List<Parameter> parameters = methodBinding.getParameters();
String[] listenerParameters = method.parameters();
for (int i = 0, count = parameters.size(); i < count; i++) {
if (i > 0) {
builder.add(", ");
}
Parameter parameter = parameters.get(i);
int listenerPosition = parameter.getListenerPosition();
if (parameter.requiresCast(listenerParameters[listenerPosition])) {
builder.add("$T.<$T>castParam(p$L, $S, $L, $S, $L)", UTILS, parameter.getType(), listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i);
} else {
builder.add("p$L", listenerPosition);
}
}
builder.add(");\n");
}
} else if (hasReturnType) {
builder.add("$L;\n", method.defaultReturn());
}
callbackMethod.addCode(builder.build());
callback.addMethod(callbackMethod.build());
}
boolean requiresRemoval = listener.remover().length() != 0;
String listenerField = null;
if (requiresRemoval) {
TypeName listenerClassName = bestGuess(listener.type());
listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
result.addStatement("$L = $L", listenerField, callback.build());
}
if (!VIEW_TYPE.equals(listener.targetType())) {
result.addStatement("(($T) $N).$L($L)", bestGuess(listener.targetType()), bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
} else {
result.addStatement("$N.$L($L)", bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
}
}
if (needsNullChecked) {
result.endControlFlow();
}
}
use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.
the class BindingSet method bestGuess.
private static TypeName bestGuess(String type) {
switch(type) {
case "void":
return TypeName.VOID;
case "boolean":
return TypeName.BOOLEAN;
case "byte":
return TypeName.BYTE;
case "char":
return TypeName.CHAR;
case "double":
return TypeName.DOUBLE;
case "float":
return TypeName.FLOAT;
case "int":
return TypeName.INT;
case "long":
return TypeName.LONG;
case "short":
return TypeName.SHORT;
default:
int left = type.indexOf('<');
if (left != -1) {
ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
List<TypeName> typeArguments = new ArrayList<>();
do {
typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
left = type.indexOf('<', left + 1);
} while (left != -1);
return ParameterizedTypeName.get(typeClassName, typeArguments.toArray(new TypeName[typeArguments.size()]));
}
return ClassName.bestGuess(type);
}
}
use of com.squareup.javapoet.TypeName in project caffeine by ben-manes.
the class NodeFactoryGenerator method addNodeSpec.
private void addNodeSpec(String className, boolean isFinal, Set<Feature> features) {
TypeName superClass;
Set<Feature> parentFeatures;
Set<Feature> generateFeatures;
if (features.size() == 2) {
parentFeatures = ImmutableSet.of();
generateFeatures = features;
superClass = TypeName.OBJECT;
} else {
parentFeatures = ImmutableSet.copyOf(Iterables.limit(features, features.size() - 1));
generateFeatures = ImmutableSet.of(Iterables.getLast(features));
superClass = ParameterizedTypeName.get(ClassName.get(PACKAGE_NAME + ".NodeFactory", encode(Feature.makeClassName(parentFeatures))), kTypeVar, vTypeVar);
}
NodeContext context = new NodeContext(superClass, className, isFinal, parentFeatures, generateFeatures);
for (NodeRule rule : rules) {
rule.accept(context);
}
nodeFactory.addType(context.nodeSubtype.build());
addEnumConstant(className, features);
}
use of com.squareup.javapoet.TypeName in project glide by bumptech.
the class RootModuleGenerator method generateGetExcludedModuleClasses.
// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private static MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
ParameterizedTypeName classOfWildcardOfObjet = ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
ParameterizedTypeName setOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(Set.class), classOfWildcardOfObjet);
ParameterizedTypeName hashSetOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(HashSet.class), classOfWildcardOfObjet);
MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(setOfClassOfWildcardOfObject);
if (excludedClassNames.isEmpty()) {
builder.addStatement("return $T.emptySet()", Collections.class);
} else {
builder.addStatement("$T excludedClasses = new $T()", setOfClassOfWildcardOfObject, hashSetOfClassOfWildcardOfObject);
for (String excludedClassName : excludedClassNames) {
// TODO: Remove this when we no longer support manifest parsing.
// Using a Literal ($L) instead of a type ($T) to get a fully qualified import that allows
// us to suppress deprecation warnings. Aimed at deprecated GlideModules.
builder.addStatement("excludedClasses.add($L.class)", excludedClassName);
}
builder.addStatement("return excludedClasses");
}
return builder.build();
}
Aggregations