Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter to exclude static-initialization block in Java Interface when instrumenting #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.gzoltar.core.instr.filter;

import com.gzoltar.core.instr.Outcome;
import javassist.CtBehavior;
import javassist.bytecode.MethodInfo;

public class InterfaceStaticInitializerFilter extends Filter {

@Override
public Outcome filter(final CtBehavior ctBehavior) {
MethodInfo methodInfo = ctBehavior.getMethodInfo();
if ((ctBehavior.getDeclaringClass() != null && ctBehavior.getDeclaringClass().isInterface())
&& methodInfo.isStaticInitializer()
&& !methodInfo.isMethod()) {
return Outcome.REJECT;
}
return Outcome.ACCEPT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.gzoltar.core.instr.InstrumentationLevel;
import com.gzoltar.core.instr.Outcome;
import com.gzoltar.core.instr.actions.AnonymousClassConstructorFilter;
import com.gzoltar.core.instr.filter.EmptyMethodFilter;
import com.gzoltar.core.instr.filter.EnumFilter;
import com.gzoltar.core.instr.filter.IFilter;
import com.gzoltar.core.instr.filter.SyntheticFilter;
import com.gzoltar.core.instr.filter.*;
import com.gzoltar.core.model.Node;
import com.gzoltar.core.model.NodeFactory;
import com.gzoltar.core.runtime.Collector;
Expand Down Expand Up @@ -88,6 +85,9 @@ public CoveragePass(final AgentConfigs agentConfigs) {
// exclude constructor of an Anonymous class as the same line number is handled by the
// superclass
this.filters.add(new AnonymousClassConstructorFilter());

// exclude static initialization-block of an Interface class
this.filters.add(new InterfaceStaticInitializerFilter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gzoltar.examples.PublicFinalModifiers;
import org.gzoltar.examples.PublicModifiers;
import org.gzoltar.examples.PublicStaticModifiers;
import org.gzoltar.examples.InterfaceFieldClass;
import org.junit.Before;
import org.junit.Test;
import com.gzoltar.core.AgentConfigs;
Expand Down Expand Up @@ -103,6 +104,14 @@ public void testProbesOfInterfaceClass() throws Exception {
this.test(classesUnderTest, new ArrayList<Integer>());
}

@Test
public void testProbesOfInterfaceWithFieldClass() throws Exception {
List<String> classesUnderTest = new ArrayList<String>();
classesUnderTest.add(InterfaceFieldClass.class.getCanonicalName());

this.test(classesUnderTest, new ArrayList<Integer>());
}

@Test
public void testProbesOfAbstractClass() throws Exception {
List<String> classesUnderTest = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.gzoltar.examples;

/**
* No static initialization-block should be created here when instrumenting the code
* Because an Java Interface doesn't accept any static-block in its body
*/
public interface InterfaceFieldClass {
public final String DEFAULT_STRING = "THIS_IS_DEFAULT_STRING";
public final String DEFAULT_STRING_WITH_NEW_OPERATOR = new String("THIS_IS_DEFAULT_STRING");
}