1 11 package org.eclipse.jdt.internal.compiler.ast; 12 13 import org.eclipse.jdt.internal.compiler.ASTVisitor; 14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 15 import org.eclipse.jdt.internal.compiler.codegen.*; 16 import org.eclipse.jdt.internal.compiler.flow.*; 17 import org.eclipse.jdt.internal.compiler.impl.Constant; 18 import org.eclipse.jdt.internal.compiler.lookup.*; 19 20 public class ClassLiteralAccess extends Expression { 21 22 public TypeReference type; 23 public TypeBinding targetType; 24 FieldBinding syntheticField; 25 26 public ClassLiteralAccess(int sourceEnd, TypeReference type) { 27 this.type = type; 28 type.bits |= IgnoreRawTypeCheck; this.sourceStart = type.sourceStart; 30 this.sourceEnd = sourceEnd; 31 } 32 33 public FlowInfo analyseCode( 34 BlockScope currentScope, 35 FlowContext flowContext, 36 FlowInfo flowInfo) { 37 38 SourceTypeBinding sourceType = currentScope.outerMostClassScope().enclosingSourceType(); 40 if (!sourceType.isInterface() 42 && !sourceType.isBaseType() 43 && currentScope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5) { 44 syntheticField = sourceType.addSyntheticFieldForClassLiteral(targetType, currentScope); 45 } 46 return flowInfo; 47 } 48 49 56 public void generateCode( 57 BlockScope currentScope, 58 CodeStream codeStream, 59 boolean valueRequired) { 60 int pc = codeStream.position; 61 62 if (valueRequired) { 64 codeStream.generateClassLiteralAccessForType(type.resolvedType, syntheticField); 65 codeStream.generateImplicitConversion(this.implicitConversion); 66 } 67 codeStream.recordPositionsFrom(pc, this.sourceStart); 68 } 69 70 public StringBuffer printExpression(int indent, StringBuffer output) { 71 72 return type.print(0, output).append(".class"); } 74 75 public TypeBinding resolveType(BlockScope scope) { 76 77 constant = Constant.NotAConstant; 78 if ((targetType = type.resolveType(scope, true )) == null) 79 return null; 80 81 if (targetType.isArrayType()) { 82 ArrayBinding arrayBinding = (ArrayBinding) this.targetType; 83 TypeBinding leafComponentType = arrayBinding.leafComponentType; 84 if (leafComponentType == TypeBinding.VOID) { 85 scope.problemReporter().cannotAllocateVoidArray(this); 86 return null; 87 } else if (leafComponentType.isTypeVariable()) { 88 scope.problemReporter().illegalClassLiteralForTypeVariable((TypeVariableBinding)leafComponentType, this); 89 } 90 } else if (this.targetType.isTypeVariable()) { 91 scope.problemReporter().illegalClassLiteralForTypeVariable((TypeVariableBinding)targetType, this); 92 } 93 ReferenceBinding classType = scope.getJavaLangClass(); 94 if (classType.isGenericType()) { 95 TypeBinding boxedType = null; 97 if (targetType.id == T_void) { 98 boxedType = scope.environment().getType(JAVA_LANG_VOID); 99 if (boxedType == null) { 100 boxedType = new ProblemReferenceBinding(JAVA_LANG_VOID, null, ProblemReasons.NotFound); 101 } 102 } else { 103 boxedType = scope.boxing(targetType); 104 } 105 this.resolvedType = scope.environment().createParameterizedType(classType, new TypeBinding[]{ boxedType }, null); 106 } else { 107 this.resolvedType = classType; 108 } 109 return this.resolvedType; 110 } 111 112 public void traverse( 113 ASTVisitor visitor, 114 BlockScope blockScope) { 115 116 if (visitor.visit(this, blockScope)) { 117 type.traverse(visitor, blockScope); 118 } 119 visitor.endVisit(this, blockScope); 120 } 121 } 122 | Popular Tags |