Skip to content

Commit

Permalink
Merge pull request #31 from ashitikov/refactor_mergeconflict
Browse files Browse the repository at this point in the history
Refactoring of MergeConflictPolicy enum. Make it more flexible using …
  • Loading branch information
ashitikov authored Apr 25, 2017
2 parents 0c431e7 + 7e17daa commit d472101
Show file tree
Hide file tree
Showing 23 changed files with 452 additions and 170 deletions.
1 change: 0 additions & 1 deletion api/src/main/java/jcomposition/api/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
package jcomposition.api;

public class Const {
public final static class UNDEFINED {}
public final static String UNDEFINED = "[UNDEFINED]";
}
33 changes: 33 additions & 0 deletions api/src/main/java/jcomposition/api/IMergeConflictPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api;

import jcomposition.api.types.IExecutableElementContainer;
import jcomposition.api.types.ITypeElementPairContainer;

import java.util.List;

public interface IMergeConflictPolicy {
/**
* Applies merge policy to method using list of overriders
* @param elementContainer executable element container
* @param overriders list of overriders of executable element
* @return list of overriders to implement
*/
List<ITypeElementPairContainer> merge(IExecutableElementContainer elementContainer,
List<ITypeElementPairContainer> overriders);
}
20 changes: 3 additions & 17 deletions api/src/main/java/jcomposition/api/annotations/Composition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package jcomposition.api.annotations;

import jcomposition.api.Const;
import jcomposition.api.IMergeConflictPolicy;
import jcomposition.api.policies.MixVoidPolicy;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -26,22 +28,6 @@
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Composition {
public enum MergeConflictPolicy {
/**
* Use first overriding method in case of returning non-void value.
* Mix methods call that returns nothing (void)
*/
MixVoid,
/**
* Use first overriding method in case of conflict
*/
UseFirst,
/**
* Make method abstract in case of conflict.
*/
MakeAbstract
}

String name() default Const.UNDEFINED;
MergeConflictPolicy onConflict() default MergeConflictPolicy.MixVoid;
Class<? extends IMergeConflictPolicy> onConflict() default MixVoidPolicy.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.policies;

import jcomposition.api.IMergeConflictPolicy;
import jcomposition.api.types.ITypeElementPairContainer;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAbstractPolicy implements IMergeConflictPolicy {
/**
* Removes (filter) abstract methods from overriders list
*
* @param overriders list of overriders of executable element
* @return not abstract methods to implement
*/
public List<ITypeElementPairContainer> filterAbstract(List<ITypeElementPairContainer> overriders) {
List<ITypeElementPairContainer> notAbstract = new ArrayList<ITypeElementPairContainer>(overriders.size());

for (ITypeElementPairContainer overrider : overriders) {
if (!overrider.isAbstract()) {
notAbstract.add(overrider);
}
}

return notAbstract;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.policies;

import jcomposition.api.types.IExecutableElementContainer;
import jcomposition.api.types.ITypeElementPairContainer;

import java.util.Collections;
import java.util.List;

/**
* Make method abstract in case of conflict.
*/
public class MakeAbstractPolicy extends BaseAbstractPolicy {
@Override
public List<ITypeElementPairContainer> merge(IExecutableElementContainer elementContainer, List<ITypeElementPairContainer> overriders) {
if (overriders.size() > 0) {
return Collections.emptyList();
}

return overriders;
}
}
38 changes: 38 additions & 0 deletions api/src/main/java/jcomposition/api/policies/MixVoidPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.policies;

import jcomposition.api.types.IExecutableElementContainer;
import jcomposition.api.types.ITypeElementPairContainer;

import javax.lang.model.type.TypeKind;
import java.util.List;

/**
* Use first overriding method in case of returning non-void value.
* Mix methods call that returns nothing (void)
*/
public class MixVoidPolicy extends UseFirstPolicy {
@Override
public List<ITypeElementPairContainer> merge(IExecutableElementContainer elementContainer, List<ITypeElementPairContainer> overriders) {
if (elementContainer.getExecutableElement().getReturnType().getKind() != TypeKind.VOID) {
return super.merge(elementContainer, overriders);
}

return filterAbstract(overriders);
}
}
36 changes: 36 additions & 0 deletions api/src/main/java/jcomposition/api/policies/UseFirstPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.policies;

import jcomposition.api.types.IExecutableElementContainer;
import jcomposition.api.types.ITypeElementPairContainer;

import java.util.List;

/**
* Use first overriding method in case of conflict
*/
public class UseFirstPolicy extends BaseAbstractPolicy {
@Override
public List<ITypeElementPairContainer> merge(IExecutableElementContainer elementContainer, List<ITypeElementPairContainer> overriders) {
overriders = filterAbstract(overriders);

return (overriders.size() > 1 ?
overriders.subList(0, 1) :
overriders);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.types;

public enum ExecutableRelationShip {
/**
* There is no hiding or overriding relationship between executable and type element.
*/
Nothing,
Same,
Overriding,
OverridingAbstract,
Hiding
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.types;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType;

public interface IExecutableElementContainer {
ExecutableElement getExecutableElement();

DeclaredType getDeclaredType();

boolean hasSuperMethod();
}
23 changes: 23 additions & 0 deletions api/src/main/java/jcomposition/api/types/IRelationShipResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.types;

public interface IRelationShipResult {
boolean isDuplicateFound();

ExecutableRelationShip getRelationShip();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017 TrollSoftware ([email protected])
*
* 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 jcomposition.api.types;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;

public interface ITypeElementPairContainer {
boolean isAbstract();

boolean isFinal();

boolean hasUseInjection();

ExecutableRelationShip getRelationShip();

TypeElement getIntf();

TypeElement getBind();

DeclaredType getDeclaredType();
}
Loading

0 comments on commit d472101

Please sign in to comment.