Skip to content

Commit

Permalink
use higher-level API for property accessor/mutator; return regular Pr…
Browse files Browse the repository at this point in the history
…opertyImpl instance from AbstractJavaSourceMemberHolder#addProperty()
  • Loading branch information
mbenson committed Feb 10, 2014
1 parent 5f4a527 commit 2444ba6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 81 deletions.
9 changes: 5 additions & 4 deletions api/src/main/java/org/jboss/forge/parser/java/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public interface Property<O extends JavaType<O>> extends Internal, Origin<O>, Na
Field<O> getField();

/**
* Learn whether this property is readable (i.e. has an accessor method).
* Learn whether this property is accessible (i.e. has an accessor method).
*/
boolean isReadable();
boolean isAccessible();

/**
* Learn whether this property is writable (i.e. has a mutator method).
* Learn whether this property is mutable (i.e. has a mutator method).
*/
boolean isWritable();
boolean isMutable();

/**
* Get this property's accessor method.
Expand All @@ -51,4 +51,5 @@ public interface Property<O extends JavaType<O>> extends Internal, Origin<O>, Na
* Get this property's mutator method.
*/
Method<O, ?> getMutator();

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,12 @@ public interface PropertySource<O extends JavaSource<O>> extends Property<O>,
*/
PropertySource<O> setType(JavaType<?> entity);

/**
* Create the accessor method.
*
* @throws IllegalStateException if property name unset or method already exists
*/
MethodSource<O> createAccessor();

/**
* Override.
*/
@Override
MethodSource<O> getAccessor();

/**
* Remove the accessor method.
*/
PropertySource<O> removeAccessor();

/**
* Create the mutator method.
*
Expand All @@ -73,6 +61,16 @@ public interface PropertySource<O extends JavaSource<O>> extends Property<O>,
*/
PropertySource<O> removeMutator();

/**
* Set whether this property is accessible.
*/
PropertySource<O> setAccessible(boolean accessible);

/**
* Set whether this property is mutable.
*/
PropertySource<O> setMutable(boolean mutable);

/**
* Create the storing field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,26 +505,22 @@ public org.jboss.forge.parser.java.Type<O> getType()
}
};

result.createAccessor();

if (!isInterface())
{
result.createField();
}
if (!isEnum())
{
result.createMutator();
}
result.setAccessible(true);
result.setMutable(!isEnum());

return result;
return getProperty(name);
}

@Override
public final AbstractJavaSourceMemberHolder<O> removeProperty(Property<O> property)
{
if (hasProperty(property))
{
getProperty(property.getName()).removeAccessor().removeMutator().removeField();
getProperty(property.getName()).setMutable(false).setAccessible(false).removeField();
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public String getName()
@Override
public Type<O> getType()
{
if (isReadable())
if (isAccessible())
{
return getAccessor().getReturnType();
}
if (isWritable())
if (isMutable())
{
return getMutator().getParameters().get(0).getType();
}
Expand Down Expand Up @@ -100,13 +100,13 @@ public FieldSource<O> getField()
}

@Override
public boolean isReadable()
public boolean isAccessible()
{
return getAccessor() != null;
}

@Override
public boolean isWritable()
public boolean isMutable()
{
return getMutator() != null;
}
Expand All @@ -132,7 +132,7 @@ public MethodSource<O> getMutator()
{
type = getField().getType();
}
else if (isReadable())
else if (isAccessible())
{
type = getAccessor().getReturnType();
}
Expand All @@ -156,8 +156,7 @@ else if (isReadable())
return null;
}

@Override
public MethodSource<O> createAccessor()
private MethodSource<O> createAccessor()
{
Assert.isTrue(getAccessor() == null, "Accessor method already exists");

Expand Down Expand Up @@ -212,12 +211,12 @@ public FieldSource<O> createField()
{
result.setFinal(true);
}
if (isReadable() && !getAccessor().isAbstract())
if (isAccessible() && !getAccessor().isAbstract())
{
removeAccessor();
createAccessor();
}
if (isWritable() && !getMutator().isAbstract())
if (isMutable() && !getMutator().isAbstract())
{
removeMutator();
createMutator();
Expand Down Expand Up @@ -287,15 +286,15 @@ public boolean visit(TextElement node)
}
};

if (isReadable())
if (isAccessible())
{
final MethodSource<O> accessor = getAccessor();
final String prefix = accessor.getReturnType().isType(boolean.class) ? "is" : "get";
accessor.setName(methodName(prefix, name));
((MethodDeclaration) accessor.getInternal()).accept(renameVisitor);
}

if (isWritable())
if (isMutable())
{
final MethodSource<O> mutator = getMutator();
mutator.setName(methodName("set", name));
Expand Down Expand Up @@ -350,9 +349,50 @@ public PropertySource<O> setType(JavaType<?> entity)
}

@Override
public PropertySource<O> removeAccessor()
public PropertySource<O> setAccessible(boolean accessible)
{
if (isAccessible() != accessible)
{
if (accessible)
{
createAccessor();
}
else
{
removeAccessor();
}
}
return this;
}

@Override
public PropertySource<O> setMutable(boolean mutable)
{
if (isMutable() != mutable)
{
if (mutable)
{
if (hasField())
{
getField().setFinal(false);
}
createMutator();
}
else
{
if (hasField())
{
getField().setFinal(true);
}
removeMutator();
}
}
return this;
}

private PropertySource<O> removeAccessor()
{
if (isReadable())
if (isAccessible())
{
getOrigin().removeMethod(getAccessor());
}
Expand All @@ -362,7 +402,7 @@ public PropertySource<O> removeAccessor()
@Override
public PropertySource<O> removeMutator()
{
if (isWritable())
if (isMutable())
{
getOrigin().removeMethod(getMutator());
}
Expand Down Expand Up @@ -411,7 +451,7 @@ public int hashCode()
*/
public boolean isValid()
{
return hasField() || isReadable() || isWritable();
return hasField() || isAccessible() || isMutable();
}

private String typeName()
Expand Down Expand Up @@ -455,4 +495,5 @@ private static String methodName(String prefix, String property)
{
return prefix + Strings.capitalize(property);
}

}
Loading

0 comments on commit 2444ba6

Please sign in to comment.