Refactory: Remove UI from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ClassLiteralAccess.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
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.*;
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; // no need to worry about raw type usage
29                 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                 // 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);
45                 }
46                 return flowInfo;
47         }
48
49         /**
50          * MessageSendDotClass code generation
51          *
52          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
53          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
54          * @param valueRequired boolean
55          */
56         public void generateCode(
57                 BlockScope currentScope,
58                 CodeStream codeStream,
59                 boolean valueRequired) {
60                 int pc = codeStream.position;
61
62                 // in interface case, no caching occurs, since cannot make a cache field for interface
63                 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"); //$NON-NLS-1$
73         }
74
75         public TypeBinding resolveType(BlockScope scope) {
76
77                 constant = Constant.NotAConstant;
78                 if ((targetType = type.resolveType(scope, true /* check bounds*/)) == 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                         // 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);
99                         } else {
100                                 boxedType = scope.boxing(targetType);
101                         }
102                         this.resolvedType = scope.environment().createParameterizedType(classType, new TypeBinding[]{ boxedType }, null/*not a member*/);
103                 } else {
104                         this.resolvedType = classType;
105                 }
106                 return this.resolvedType;
107         }
108
109         public void traverse(
110                 ASTVisitor visitor,
111                 BlockScope blockScope) {
112
113                 if (visitor.visit(this, blockScope)) {
114                         type.traverse(visitor, blockScope);
115                 }
116                 visitor.endVisit(this, blockScope);
117         }
118 }