1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.classfmt.ClassFileConstants;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.flow.*;
17 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
18 import net.sourceforge.phpdt.internal.compiler.lookup.*;
20 public class ClassLiteralAccess extends Expression {
22 public TypeReference type;
23 public TypeBinding targetType;
24 FieldBinding syntheticField;
26 public ClassLiteralAccess(int sourceEnd, TypeReference type) {
28 type.bits |= IgnoreRawTypeCheck; // no need to worry about raw type usage
29 this.sourceStart = type.sourceStart;
30 this.sourceEnd = sourceEnd;
33 public FlowInfo analyseCode(
34 BlockScope currentScope,
35 FlowContext flowContext,
38 // if reachable, request the addition of a synthetic field for caching the class descriptor
39 SourceTypeBinding sourceType = currentScope.outerMostClassScope().enclosingSourceType();
40 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=22334
41 if (!sourceType.isInterface()
42 && !targetType.isBaseType()
43 && currentScope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5) {
44 syntheticField = sourceType.addSyntheticFieldForClassLiteral(targetType, currentScope);
50 * MessageSendDotClass code generation
52 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
53 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
54 * @param valueRequired boolean
56 public void generateCode(
57 BlockScope currentScope,
58 CodeStream codeStream,
59 boolean valueRequired) {
60 int pc = codeStream.position;
62 // in interface case, no caching occurs, since cannot make a cache field for interface
64 codeStream.generateClassLiteralAccessForType(type.resolvedType, syntheticField);
65 codeStream.generateImplicitConversion(this.implicitConversion);
67 codeStream.recordPositionsFrom(pc, this.sourceStart);
70 public StringBuffer printExpression(int indent, StringBuffer output) {
72 return type.print(0, output).append(".class"); //$NON-NLS-1$
75 public TypeBinding resolveType(BlockScope scope) {
77 constant = Constant.NotAConstant;
78 if ((targetType = type.resolveType(scope, true /* check bounds*/)) == null)
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);
87 } else if (leafComponentType.isTypeVariable()) {
88 scope.problemReporter().illegalClassLiteralForTypeVariable((TypeVariableBinding)leafComponentType, this);
90 } else if (this.targetType.isTypeVariable()) {
91 scope.problemReporter().illegalClassLiteralForTypeVariable((TypeVariableBinding)targetType, this);
93 ReferenceBinding classType = scope.getJavaLangClass();
94 if (classType.isGenericType()) {
95 // Integer.class --> Class<Integer>, perform boxing of base types (int.class --> Class<Integer>)
96 TypeBinding boxedType = null;
97 if (targetType.id == T_void) {
98 boxedType = scope.environment().getResolvedType(JAVA_LANG_VOID, scope);
100 boxedType = scope.boxing(targetType);
102 this.resolvedType = scope.environment().createParameterizedType(classType, new TypeBinding[]{ boxedType }, null/*not a member*/);
104 this.resolvedType = classType;
106 return this.resolvedType;
109 public void traverse(
111 BlockScope blockScope) {
113 if (visitor.visit(this, blockScope)) {
114 type.traverse(visitor, blockScope);
116 visitor.endVisit(this, blockScope);