*/
public JClassLiteral getLiteralClass(JType type) {
JClassLiteral classLiteral = classLiterals.get(type);
if (classLiteral == null) {
if (optimizationsStarted) {
throw new InternalCompilerException(
"New class literals cannot be created once optimizations have started; type '"
+ type + "'");
}
SourceInfo info = typeSpecialClassLiteralHolder.getSourceInfo();
// Create the allocation expression FIRST since this may be recursive on
// super type (this forces the super type classLit to be created first).
JExpression alloc = JClassLiteral.computeClassObjectAllocation(this,
info, type);
// Create a field in the class literal holder to hold the object.
JField field = new JField(info, type.getJavahSignatureName()
+ "_classLit", typeSpecialClassLiteralHolder, getTypeJavaLangClass(),
true, Disposition.FINAL);
typeSpecialClassLiteralHolder.addField(field);
// Initialize the field.
JFieldRef fieldRef = new JFieldRef(info, null, field,
typeSpecialClassLiteralHolder);
JDeclarationStatement decl = new JDeclarationStatement(info, fieldRef,
alloc);
JMethodBody clinitBody = (JMethodBody) typeSpecialClassLiteralHolder.getMethods().get(
0).getBody();
clinitBody.getBlock().addStmt(decl);
SourceInfo literalInfo = createSourceInfoSynthetic(JProgram.class,
"class literal for " + type.getName());
literalInfo.addCorrelation(correlator.by(Literal.CLASS));
classLiteral = new JClassLiteral(literalInfo, type, field);
classLiterals.put(type, classLiteral);
} else {
// Make sure the field hasn't been pruned.
JField field = classLiteral.getField();
if (optimizationsStarted
&& !field.getEnclosingType().getFields().contains(field)) {
throw new InternalCompilerException(
"Getting a class literal whose field holder has already been pruned; type '"
+ type + " '");
}
}
return classLiteral;