KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > ClassLiteralAccess


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 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; // 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                 && !sourceType.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 JavaDoc printExpression(int indent, StringBuffer JavaDoc 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().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/*not a member*/);
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