Skip to content

Commit

Permalink
Merge pull request #4 from skapral/issue-3
Browse files Browse the repository at this point in the history
#3: Fix in IIMarkAsEObject
  • Loading branch information
skapral authored Feb 23, 2022
2 parents b7dec0c + b07fe1f commit f704dfa
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-
* ===========================================================================
* equivalence-assertions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.equivalence.assertions;

import io.vavr.collection.List;
import org.assertj.core.api.Assertions;

import java.lang.reflect.Type;
import java.util.stream.Collectors;

public class AssertImplements implements Assertion {
private final Class<?> source;
private final String typeName;

public AssertImplements(Class<?> source, String typeName) {
this.source = source;
this.typeName = typeName;
}

@Override
public final void check() throws Exception {
Assertions.assertThat(
List.of(source.getGenericInterfaces())
.exists(iface -> iface.getTypeName().equals(typeName))
)
.withFailMessage(
"Expecting %s to implement %s, found %s",
source.getName(),
typeName,
List.of(source.getGenericInterfaces()).map(Type::getTypeName).collect(Collectors.joining(", "))
)
.isTrue();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@
import net.bytebuddy.jar.asm.signature.SignatureWriter;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.OpenedClassReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author skapral
*/
public class IIMarkAsEObject implements InstrumentationIteration {
private final static Logger log = LoggerFactory.getLogger(IIMarkAsEObject.class);

@Override
public final DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription) {

Expand Down Expand Up @@ -77,23 +81,40 @@ public void visit(int version, int access, String name, String signature, String
superName = "com/pragmaticobjects/oo/equivalence/base/EObject";
if (signature != null) {
SignatureWriter sw = new SignatureWriter() {
private boolean typeArg = false;
private boolean superclass = false;

@Override
public void visitFormalTypeParameter(String name) {
typeArg = true;
log.debug("visitFormalTypeParameter: " + name + " " + superclass);
superclass = false;
super.visitFormalTypeParameter(name);
}

@Override
public SignatureVisitor visitSuperclass() {
typeArg = false;
log.debug("visitSuperclass: " + superclass);
superclass = true;
return super.visitSuperclass();
}

@Override
public void visitEnd() {
log.debug("visitEnd: " + superclass);
superclass = false;
super.visitEnd();
}

@Override
public SignatureVisitor visitInterface() {
log.debug("visitInterface: " + superclass);
superclass = false;
return super.visitInterface();
}

@Override
public void visitClassType(String name) {
if (!typeArg && "java/lang/Object".equals(name)) {
log.debug("visitClassType: " + name + " " + superclass);
if (superclass && "java/lang/Object".equals(name)) {
name = "com/pragmaticobjects/oo/equivalence/base/EObject";
}
super.visitClassType(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*-
* ===========================================================================
* equivalence-itests
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.equivalence.itests.classes;


import io.vavr.collection.Map;


public class GenericsTest implements GenericsTestIface<Map<String, Object>> {
@Override
public final Map<String, Object> get() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*-
* ===========================================================================
* equivalence-itests
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.equivalence.itests.classes;

public interface GenericsTestIface<T> {
T get();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
*/
package com.pragmaticobjects.oo.equivalence.itests;

import com.pragmaticobjects.oo.equivalence.assertions.AssertTwoObjectsEquality;
import com.pragmaticobjects.oo.equivalence.assertions.TestCase;
import com.pragmaticobjects.oo.equivalence.assertions.TestsSuite;
import com.pragmaticobjects.oo.equivalence.assertions.*;
import com.pragmaticobjects.oo.equivalence.itests.classes.GenericsTest;
import com.pragmaticobjects.oo.equivalence.itests.classes.TreeNode;

/**
Expand Down Expand Up @@ -76,6 +75,15 @@ public TestClassesWithGenerics() {
new TreeNode<>(1, new TreeNode<>(2), new TreeNode<>(4)),
false
)
),
new TestCase(
"Issue 3 regression test: see https://github.com/skapral/pragmaticobjects/issues/3",
new AssertAssertionPasses(
new AssertImplements(
GenericsTest.class,
"com.pragmaticobjects.oo.equivalence.itests.classes.GenericsTestIface<io.vavr.collection.Map<java.lang.String, java.lang.Object>>"
)
)
)
);
}
Expand Down

0 comments on commit f704dfa

Please sign in to comment.