-
Notifications
You must be signed in to change notification settings - Fork 316
/
JdtTypeMirror.java
137 lines (119 loc) · 3.85 KB
/
JdtTypeMirror.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
* Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.common.types.access.jdt;
import java.util.Map;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jdt.core.IType;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.access.IMirrorOptionsAware;
import org.eclipse.xtext.common.types.access.TypeResource;
import org.eclipse.xtext.common.types.access.impl.AbstractClassMirror;
import org.eclipse.xtext.common.types.access.impl.ITypeFactory;
import org.eclipse.xtext.common.types.access.impl.TypeResourceServices;
/**
* @author Sebastian Zarnekow - Initial contribution and API
*
*
* TODO - remove Adapter interface, it's not used.
*/
public class JdtTypeMirror extends AbstractClassMirror implements Adapter, IMirrorOptionsAware {
private final static Logger LOG = Logger.getLogger(JdtTypeMirror.class);
private IType mirroredType;
private final ITypeFactory<IType, JvmDeclaredType> typeFactory;
private TypeResource typeResource;
/**
* @deprecated use {@link #JdtTypeMirror(IType, ITypeFactory, TypeResourceServices)}
*/
@Deprecated
public JdtTypeMirror(IType type, ITypeFactory<IType, JvmDeclaredType> typeFactory) {
this(type, typeFactory, null);
}
/**
* @since 2.8
*/
public JdtTypeMirror(IType type, ITypeFactory<IType, JvmDeclaredType> typeFactory, TypeResourceServices typeResourceServices) {
super(typeResourceServices);
this.mirroredType = type;
this.typeFactory = typeFactory;
}
@Override
public void initialize(TypeResource typeResource) {
initialize(typeResource, null);
}
/**
* @since 2.7
*/
@Override
public void initialize(TypeResource typeResource, Map<?, ?> options) {
try {
if (typeFactory instanceof ITypeFactory.OptionsAware<?, ?>) {
JvmDeclaredType jvmType = ((ITypeFactory.OptionsAware<IType, JvmDeclaredType>) typeFactory).createType(mirroredType, typeResource, options);
typeResource.getContents().add(jvmType);
} else {
typeResource.getContents().add(typeFactory.createType(mirroredType));
}
} catch (RuntimeException e) {
if (typeResourceServices != null) {
typeResourceServices.getOperationCanceledManager().propagateAsErrorIfCancelException(e);
}
LOG.error("Error initializing type "+typeResource.getURI(), e);
throw e;
}
this.typeResource = typeResource;
}
@Override
public URI getLocationURI(Resource resource) {
IPath path = mirroredType.getPath();
if (mirroredType.getResource() != null) {
return URI.createPlatformResourceURI(path.toString(), true);
} else {
return URI.createFileURI(path.toString());
}
}
@Override
protected String getTypeName() {
return mirroredType.getFullyQualifiedName();
}
public IType getMirroredType() {
return mirroredType;
}
protected void unloadResource() {
typeResource.unload();
mirroredType = null;
}
@Override
public Notifier getTarget() {
return null;
}
@Override
public boolean isAdapterForType(Object object) {
return false;
}
@Override
public void notifyChanged(Notification notification) {
// ignore
}
@Override
public void setTarget(Notifier notifier) {
// ignore
}
/**
* @since 2.3
*/
@Override
public boolean isSealed() {
return mirroredType.isReadOnly();
}
}