public boolean isEquals(MethodSource<?> method) { return method.getName().equals("equals") && (method.getParameters() == null || method.getParameters().size() == 1) && method.getReturnType() != null && method.getReturnType().isPrimitive() && "boolean".equals(method.getReturnType().getName()); }
public boolean isHashCode(MethodSource<?> method) { return method.getName().equals("hashCode") && (method.getParameters() == null || method.getParameters().size() == 0) && method.getReturnType() != null && method.getReturnType().isPrimitive() && "int".equals(method.getReturnType().getName()); }
@Override public List<PropertySource<O>> getProperties(Class<?> type) { final Set<String> propertyNames = new LinkedHashSet<>(); for (MethodSource<O> method : getMethods()) { if ((isAccessor(method) || isMutator(method)) && method.getReturnType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(extractPropertyName(method)); } } for (FieldSource<O> field : getFields()) { if (!field.isStatic() && field.getType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(field.getName()); } } final List<PropertySource<O>> result = new ArrayList<>(propertyNames.size()); for (String name : propertyNames) { result.add(new PropertyImpl<>(name, getOrigin())); } return result; }
@Override public List<PropertySource<O>> getProperties(Class<?> type) { final Set<String> propertyNames = new LinkedHashSet<String>(); for (MethodSource<O> method : getMethods()) { if ((isAccessor(method) || isMutator(method)) && method.getReturnType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(extractPropertyName(method)); } } for (FieldSource<O> field : getFields()) { if (!field.isStatic() && field.getType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(field.getName()); } } final List<PropertySource<O>> result = new ArrayList<PropertySource<O>>(propertyNames.size()); for (String name : propertyNames) { result.add(new PropertyImpl<O>(name, getOrigin())); } return result; }
@Override public PropertySource<O> setType(String type) { final MethodSource<O> accessor = getAccessor(); final MethodSource<O> mutator = getMutator(); final FieldSource<O> field = getField(); if (accessor != null) { final Type<O> originalType = accessor.getReturnType(); accessor.setReturnType(type); if (originalType.isType(boolean.class) || accessor.getReturnType().isType(boolean.class)) { // potential name change: final String accessorName = methodName(accessor.getReturnType().isType(boolean.class) ? "is" : "get", getName()); accessor.setName(accessorName); } } if (mutator != null) { mutator.setParameters(String.format("%s %s", type, getName())); } if (field != null) { field.setType(type); } return this; }
@Override public PropertySource<O> setType(String type) { final MethodSource<O> accessor = getAccessor(); final MethodSource<O> mutator = getMutator(); final FieldSource<O> field = getField(); if (accessor != null) { final Type<O> originalType = accessor.getReturnType(); accessor.setReturnType(type); if (originalType.isType(boolean.class) || accessor.getReturnType().isType(boolean.class)) { // potential name change: final String accessorName = methodName(accessor.getReturnType().isType(boolean.class) ? "is" : "get", getName()); accessor.setName(accessorName); } } if (mutator != null) { for (ParameterSource<O> param : mutator.getParameters()) mutator.removeParameter(param); mutator.addParameter(type, getName()); } if (field != null) { field.setType(type); } return this; }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
@Override public PropertySource<O> setType(String type) { final MethodSource<O> accessor = getAccessor(); final MethodSource<O> mutator = getMutator(); final FieldSource<O> field = getField(); if (accessor != null) { final Type<O> originalType = accessor.getReturnType(); accessor.setReturnType(type); if (originalType.isType(boolean.class) || accessor.getReturnType().isType(boolean.class)) { // potential name change: final String accessorName = methodName(accessor.getReturnType().isType(boolean.class) ? "is" : "get", getName()); accessor.setName(accessorName); } } if (mutator != null) { for (ParameterSource<O> param : mutator.getParameters()) mutator.removeParameter(param); mutator.addParameter(type, getName()); } if (field != null) { field.setType(type); } return this; }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
usedImports.add(interfaceSource.getImport(method.getReturnType().getQualifiedName()));
usedImports.add(interfaceSource.getImport(method.getReturnType().getQualifiedName()));
public void visit(MethodSource<? extends JavaClassSource> methodSource) { for (ParameterSource<? extends JavaClassSource> paramSource : methodSource.getParameters()) { // Method parameters addJavaResourceReference(paramSource.getType().getQualifiedName()); // Method parameter annotations for (AnnotationSource<? extends JavaClassSource> annoSource : paramSource.getAnnotations()) { visit(annoSource); } } Type<? extends JavaClassSource> returnType = methodSource.getReturnType(); if (returnType != null) { String returnTypeQualifiedName = returnType.getQualifiedName(); if (!returnTypeQualifiedName.endsWith(".void")) { addJavaResourceReference(returnTypeQualifiedName); } } // method annotations for (AnnotationSource<? extends JavaClassSource> annoSource : methodSource.getAnnotations()) { visit(annoSource); } }
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
/** * Adds a default method implementation to the given {@link MethodSource}. This method will call * {@link MethodSource#setAbstract(false)} before setting the body if the origin is not an interface * * @param source */ public static void implementMethod(MethodSource<?> source) { if (source.getOrigin().isInterface()) { source.setBody(null); } else { if (source.isNative()) { source.setNative(false); } source.setAbstract(false); if (source.isReturnTypeVoid()) { source.setBody(""); } else { source.setBody("return " + Types.getDefaultValue(source.getReturnType().getName()) + ";"); } } }
/** * Adds a default method implementation to the given {@link MethodSource}. This method will call * {@link MethodSource#setAbstract(false)} before setting the body if the origin is not an interface * * @param source */ public static void implementMethod(MethodSource<?> source) { if (source.getOrigin().isInterface()) { source.setBody(null); } else { if (source.isNative()) { source.setNative(false); } source.setAbstract(false); if (source.isReturnTypeVoid()) { source.setBody(""); } else { source.setBody("return " + Types.getDefaultValue(source.getReturnType().getName()) + ";"); } } }
final String prefix = accessor.getReturnType().isType(boolean.class) ? "is" : "get"; accessor.setName(methodName(prefix, name)); ((MethodDeclaration) accessor.getInternal()).accept(renameVisitor);
.setBody("this." + fieldName + " = new " + method.getReturnType().getName() + "();"); dirtyBit = true;