Skip to content

Commit

Permalink
Simplified prohibition of type initializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jul 19, 2016
1 parent 7de6504 commit 5ae39ba
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
Expand Up @@ -3265,9 +3265,6 @@ public void visit(int classFileVersionNumber,
classFileVersion,
ForInlining.this.classFileVersion);
contextRegistry.setImplementationContext(implementationContext);
if (!classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8) && instrumentedType.isInterface()) {
implementationContext.prohibitTypeInitializer();
}
super.visit(classFileVersionNumber,
instrumentedType.getActualModifiers((modifiers & Opcodes.ACC_SUPER) != 0 && !instrumentedType.isInterface()),
instrumentedType.getInternalName(),
Expand Down
Expand Up @@ -519,11 +519,6 @@ void drain(ClassVisitor classVisitor,
InjectedCode injectedCode,
AnnotationValueFilter.Factory annotationValueFilterFactory);

/**
* Prohibits any instrumentation of an instrumented class's type initializer.
*/
void prohibitTypeInitializer();

/**
* When draining an implementation context, a type initializer might be written to the created class
* file. If any code must be explicitly invoked from within the type initializer, this can be achieved
Expand Down Expand Up @@ -680,11 +675,6 @@ public FieldDescription.InDefinedShape cache(StackManipulation fieldValue, TypeD
throw new IllegalStateException("Field values caching was disabled: " + fieldType);
}

@Override
public void prohibitTypeInitializer() {
/* do nothing */
}

@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
Expand Down Expand Up @@ -805,12 +795,6 @@ class Default extends ExtractableView.AbstractBase implements AuxiliaryType.Meth
*/
private boolean fieldCacheCanAppendEntries;

/**
* If {@code true}, this instance suggests the retention of the original type initializer and prohibits the definition of a custom initializer.
* This property is required for interfaces before the Java 8 byte code level where type initializers are not allowed.
*/
private boolean prohibitTypeInitializer;

/**
* Creates a new default implementation context.
*
Expand All @@ -837,7 +821,6 @@ protected Default(TypeDescription instrumentedType,
registeredFieldCacheEntries = new HashMap<FieldCacheEntry, FieldDescription.InDefinedShape>();
suffix = RandomString.make();
fieldCacheCanAppendEntries = true;
prohibitTypeInitializer = false;
}

@Override
Expand Down Expand Up @@ -885,7 +868,7 @@ public TypeDescription register(AuxiliaryType auxiliaryType) {

@Override
public boolean isRetainTypeInitializer() {
return prohibitTypeInitializer;
return instrumentedType.isInterface() && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8);
}

@Override
Expand Down Expand Up @@ -933,7 +916,7 @@ public void drain(ClassVisitor classVisitor,
} else if (typeInitializer.isDefined()) {
initializerRecord = new TypeWriter.MethodPool.Record.ForDefinedMethod.WithBody(typeInitializerMethod, typeInitializer.withReturn());
}
if (prohibitTypeInitializer && initializerRecord.getSort().isDefined()) {
if (isRetainTypeInitializer() && initializerRecord.getSort().isDefined()) {
throw new IllegalStateException("It is impossible to define a class initializer or cached values for " + instrumentedType);
}
initializerRecord.apply(classVisitor, this, annotationValueFilterFactory);
Expand All @@ -942,11 +925,6 @@ public void drain(ClassVisitor classVisitor,
}
}

@Override
public void prohibitTypeInitializer() {
prohibitTypeInitializer = true;
}

@Override
public String toString() {
return "Implementation.Context.Default{" +
Expand All @@ -963,7 +941,6 @@ public String toString() {
", registeredFieldCacheEntries=" + registeredFieldCacheEntries +
", suffix=" + suffix +
", fieldCacheCanAppendEntries=" + fieldCacheCanAppendEntries +
", prohibitTypeInitializer=" + prohibitTypeInitializer +
'}';
}

Expand Down
Expand Up @@ -39,24 +39,29 @@ public void testTypeInitializerNotRetained() throws Exception {

@Test
public void testFrozenTypeInitializerRetainsInitializer() throws Exception {
Implementation.Context.ExtractableView implementationContext = new Implementation.Context.Default(mock(TypeDescription.class),
mock(ClassFileVersion.class),
TypeDescription instrumentedType = mock(TypeDescription.class);
ClassFileVersion classFileVersion = mock(ClassFileVersion.class);
Implementation.Context.ExtractableView implementationContext = new Implementation.Context.Default(instrumentedType,
classFileVersion,
mock(AuxiliaryType.NamingStrategy.class),
mock(TypeInitializer.class),
mock(ClassFileVersion.class));
implementationContext.prohibitTypeInitializer();
when(instrumentedType.isInterface()).thenReturn(true);
when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8)).thenReturn(false);
assertThat(implementationContext.isRetainTypeInitializer(), is(true));
}

@Test(expected = IllegalStateException.class)
public void testFrozenTypeInitializerFrozenThrowsExceptionOnDrain() throws Exception {
TypeDescription instrumentedType = mock(TypeDescription.class);
ClassFileVersion classFileVersion = mock(ClassFileVersion.class);
Implementation.Context.ExtractableView implementationContext = new Implementation.Context.Default(instrumentedType,
mock(ClassFileVersion.class),
classFileVersion,
mock(AuxiliaryType.NamingStrategy.class),
mock(TypeInitializer.class),
mock(ClassFileVersion.class));
implementationContext.prohibitTypeInitializer();
when(instrumentedType.isInterface()).thenReturn(true);
when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8)).thenReturn(false);
TypeWriter.MethodPool methodPool = mock(TypeWriter.MethodPool.class);
TypeWriter.MethodPool.Record record = mock(TypeWriter.MethodPool.Record.class);
when(record.getSort()).thenReturn(TypeWriter.MethodPool.Record.Sort.DEFINED);
Expand Down
Expand Up @@ -267,6 +267,7 @@ public void setUp() throws Exception {
when(firstSpecialType.asErasure()).thenReturn(firstSpecialType);
when(secondSpecialMethod.getDeclaringType()).thenReturn(secondSpecialType);
when(secondSpecialType.asErasure()).thenReturn(secondSpecialType);
when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8)).thenReturn(true);
}

@Test
Expand Down
Expand Up @@ -78,9 +78,8 @@ public void testAuxiliaryTypes() throws Exception {
}

@Test
public void testFreezeHasNoEffect() throws Exception {
public void testIsRetainTypeInitializer() throws Exception {
Implementation.Context.ExtractableView implementationContext = new Implementation.Context.Disabled(instrumentedType, classFileVersion);
implementationContext.prohibitTypeInitializer();
assertThat(implementationContext.isRetainTypeInitializer(), is(true));
}

Expand Down

0 comments on commit 5ae39ba

Please sign in to comment.