List of usage examples for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getAnnotations
@Override
public IBinaryAnnotation[] getAnnotations()
From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.BinaryIndexer.java
License:Open Source License
public void indexDocument() { try {//w ww . ja v a2s . c o m final byte[] contents = this.document.getByteContents(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=107124 // contents can potentially be null if a IOException occurs while retrieving the contents if (contents == null) return; final String path = this.document.getPath(); ClassFileReader reader = new ClassFileReader(contents, path == null ? null : path.toCharArray()); // first add type references char[] className = replace('/', '.', reader.getName()); // looks like java/lang/String // need to extract the package name and the simple name int packageNameIndex = CharOperation.lastIndexOf('.', className); char[] packageName = null; char[] name = null; if (packageNameIndex >= 0) { packageName = CharOperation.subarray(className, 0, packageNameIndex); name = CharOperation.subarray(className, packageNameIndex + 1, className.length); } else { packageName = CharOperation.NO_CHAR; name = className; } char[] enclosingTypeName = null; boolean isNestedType = reader.isNestedType(); if (isNestedType) { if (reader.isAnonymous()) { name = CharOperation.NO_CHAR; } else { name = reader.getInnerSourceName(); } if (reader.isLocal() || reader.isAnonymous()) { // set specific ['0'] value for local and anonymous to be able to filter them enclosingTypeName = ONE_ZERO; } else { char[] fullEnclosingName = reader.getEnclosingTypeName(); int nameLength = fullEnclosingName.length - packageNameIndex - 1; if (nameLength <= 0) { // See PR 1GIR345: ITPJCORE:ALL - Indexer: NegativeArraySizeException return; } enclosingTypeName = new char[nameLength]; System.arraycopy(fullEnclosingName, packageNameIndex + 1, enclosingTypeName, 0, nameLength); } } // type parameters char[][] typeParameterSignatures = null; char[] genericSignature = reader.getGenericSignature(); if (genericSignature != null) { CharOperation.replace(genericSignature, '/', '.'); typeParameterSignatures = Signature.getTypeParameters(genericSignature); } // eliminate invalid innerclasses (1G4KCF7) if (name == null) return; char[][] superinterfaces = replace('/', '.', reader.getInterfaceNames()); char[][] enclosingTypeNames = enclosingTypeName == null ? null : new char[][] { enclosingTypeName }; int modifiers = reader.getModifiers(); switch (TypeDeclaration.kind(modifiers)) { case TypeDeclaration.CLASS_DECL: char[] superclass = replace('/', '.', reader.getSuperclassName()); addClassDeclaration(modifiers, packageName, name, enclosingTypeNames, superclass, superinterfaces, typeParameterSignatures, false); break; case TypeDeclaration.INTERFACE_DECL: addInterfaceDeclaration(modifiers, packageName, name, enclosingTypeNames, superinterfaces, typeParameterSignatures, false); break; case TypeDeclaration.ENUM_DECL: superclass = replace('/', '.', reader.getSuperclassName()); addEnumDeclaration(modifiers, packageName, name, enclosingTypeNames, superclass, superinterfaces, false); break; case TypeDeclaration.ANNOTATION_TYPE_DECL: addAnnotationTypeDeclaration(modifiers, packageName, name, enclosingTypeNames, false); break; } // Look for references in class annotations IBinaryAnnotation[] annotations = reader.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } long tagBits = reader.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } int extraFlags = ExtraFlags.getExtraFlags(reader); // first reference all methods declarations and field declarations MethodInfo[] methods = (MethodInfo[]) reader.getMethods(); boolean noConstructor = true; if (methods != null) { for (int i = 0, max = methods.length; i < max; i++) { MethodInfo method = methods[i]; boolean isConstructor = method.isConstructor(); char[] descriptor = method.getMethodDescriptor(); char[][] parameterTypes = decodeParameterTypes(descriptor, isConstructor && isNestedType); char[] returnType = decodeReturnType(descriptor); char[][] exceptionTypes = replace('/', '.', method.getExceptionTypeNames()); if (isConstructor) { noConstructor = false; char[] signature = method.getGenericSignature(); if (signature == null) { if (reader.isNestedType() && ((modifiers & ClassFileConstants.AccStatic) == 0)) { signature = removeFirstSyntheticParameter(descriptor); } else { signature = descriptor; } } addConstructorDeclaration(name, parameterTypes == null ? 0 : parameterTypes.length, signature, parameterTypes, method.getArgumentNames(), method.getModifiers(), packageName, modifiers, exceptionTypes, extraFlags); } else { if (!method.isClinit()) { addMethodDeclaration(method.getSelector(), parameterTypes, returnType, exceptionTypes); } } // look for references in method annotations annotations = method.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } tagBits = method.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } } } if (noConstructor) { addDefaultConstructorDeclaration(className, packageName, modifiers, extraFlags); } FieldInfo[] fields = (FieldInfo[]) reader.getFields(); if (fields != null) { for (int i = 0, max = fields.length; i < max; i++) { FieldInfo field = fields[i]; char[] fieldName = field.getName(); char[] fieldType = decodeFieldType(replace('/', '.', field.getTypeName())); addFieldDeclaration(fieldType, fieldName); // look for references in field annotations annotations = field.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } tagBits = field.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } } } // record all references found inside the .class file extractReferenceFromConstantPool(contents, reader); } catch (ClassFormatException e) { // ignore this.document.removeAllIndexEntries(); Util.log(IStatus.WARNING, "The Java indexing could not index " + this.document.getPath() + ". This .class file doesn't follow the class file format specification. Please report this issue against the .class file vendor"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (RuntimeException e) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182154 // logging the entry that could not be indexed and continue with the next one // we remove all entries relative to the boggus document this.document.removeAllIndexEntries(); Util.log(IStatus.WARNING, "The Java indexing could not index " + this.document.getPath() + ". This .class file doesn't follow the class file format specification. Please report this issue against the .class file vendor"); //$NON-NLS-1$ //$NON-NLS-2$ } }
From source file:org.eclipse.che.jdt.internal.core.search.indexing.BinaryIndexer.java
License:Open Source License
public void indexDocument() { try {/* w ww. jav a2 s . co m*/ final byte[] contents = this.document.getByteContents(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=107124 // contents can potentially be null if a IOException occurs while retrieving the contents if (contents == null) return; final String path = this.document.getPath(); ClassFileReader reader = new ClassFileReader(contents, path == null ? null : path.toCharArray()); // first add type references char[] className = replace('/', '.', reader.getName()); // looks like java/lang/String // need to extract the package name and the simple name int packageNameIndex = CharOperation.lastIndexOf('.', className); char[] packageName = null; char[] name = null; if (packageNameIndex >= 0) { packageName = CharOperation.subarray(className, 0, packageNameIndex); name = CharOperation.subarray(className, packageNameIndex + 1, className.length); } else { packageName = CharOperation.NO_CHAR; name = className; } char[] enclosingTypeName = null; boolean isNestedType = reader.isNestedType(); if (isNestedType) { if (reader.isAnonymous()) { name = CharOperation.NO_CHAR; } else { name = reader.getInnerSourceName(); } if (reader.isLocal() || reader.isAnonymous()) { // set specific ['0'] value for local and anonymous to be able to filter them enclosingTypeName = ONE_ZERO; } else { char[] fullEnclosingName = reader.getEnclosingTypeName(); int nameLength = fullEnclosingName.length - packageNameIndex - 1; if (nameLength <= 0) { // See PR 1GIR345: ITPJCORE:ALL - Indexer: NegativeArraySizeException return; } enclosingTypeName = new char[nameLength]; System.arraycopy(fullEnclosingName, packageNameIndex + 1, enclosingTypeName, 0, nameLength); } } // type parameters char[][] typeParameterSignatures = null; char[] genericSignature = reader.getGenericSignature(); if (genericSignature != null) { CharOperation.replace(genericSignature, '/', '.'); typeParameterSignatures = Signature.getTypeParameters(genericSignature); } // eliminate invalid innerclasses (1G4KCF7) if (name == null) return; char[][] superinterfaces = replace('/', '.', reader.getInterfaceNames()); char[][] enclosingTypeNames = enclosingTypeName == null ? null : new char[][] { enclosingTypeName }; int modifiers = reader.getModifiers(); switch (TypeDeclaration.kind(modifiers)) { case TypeDeclaration.CLASS_DECL: char[] superclass = replace('/', '.', reader.getSuperclassName()); addClassDeclaration(modifiers, packageName, name, enclosingTypeNames, superclass, superinterfaces, typeParameterSignatures, false); break; case TypeDeclaration.INTERFACE_DECL: addInterfaceDeclaration(modifiers, packageName, name, enclosingTypeNames, superinterfaces, typeParameterSignatures, false); break; case TypeDeclaration.ENUM_DECL: superclass = replace('/', '.', reader.getSuperclassName()); addEnumDeclaration(modifiers, packageName, name, enclosingTypeNames, superclass, superinterfaces, false); break; case TypeDeclaration.ANNOTATION_TYPE_DECL: addAnnotationTypeDeclaration(modifiers, packageName, name, enclosingTypeNames, false); break; } // Look for references in class annotations IBinaryAnnotation[] annotations = reader.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } long tagBits = reader.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } int extraFlags = ExtraFlags.getExtraFlags(reader); // first reference all methods declarations and field declarations MethodInfo[] methods = (MethodInfo[]) reader.getMethods(); boolean noConstructor = true; if (methods != null) { for (int i = 0, max = methods.length; i < max; i++) { MethodInfo method = methods[i]; boolean isConstructor = method.isConstructor(); char[] descriptor = method.getMethodDescriptor(); char[][] parameterTypes = decodeParameterTypes(descriptor, isConstructor && isNestedType); char[] returnType = decodeReturnType(descriptor); char[][] exceptionTypes = replace('/', '.', method.getExceptionTypeNames()); if (isConstructor) { noConstructor = false; char[] signature = method.getGenericSignature(); if (signature == null) { if (reader.isNestedType() && ((modifiers & ClassFileConstants.AccStatic) == 0)) { signature = removeFirstSyntheticParameter(descriptor); } else { signature = descriptor; } } addConstructorDeclaration(name, parameterTypes == null ? 0 : parameterTypes.length, signature, parameterTypes, method.getArgumentNames(), method.getModifiers(), packageName, modifiers, exceptionTypes, extraFlags); } else { if (!method.isClinit()) { addMethodDeclaration(method.getSelector(), parameterTypes, returnType, exceptionTypes); } } // look for references in method annotations annotations = method.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } tagBits = method.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } } } if (noConstructor) { addDefaultConstructorDeclaration(className, packageName, modifiers, extraFlags); } FieldInfo[] fields = (FieldInfo[]) reader.getFields(); if (fields != null) { for (int i = 0, max = fields.length; i < max; i++) { FieldInfo field = fields[i]; char[] fieldName = field.getName(); char[] fieldType = decodeFieldType(replace('/', '.', field.getTypeName())); addFieldDeclaration(fieldType, fieldName); // look for references in field annotations annotations = field.getAnnotations(); if (annotations != null) { for (int a = 0, length = annotations.length; a < length; a++) { IBinaryAnnotation annotation = annotations[a]; addBinaryAnnotation(annotation); } } tagBits = field.getTagBits() & TagBits.AllStandardAnnotationsMask; if (tagBits != 0) { addBinaryStandardAnnotations(tagBits); } } } // record all references found inside the .class file extractReferenceFromConstantPool(contents, reader); } catch (ClassFormatException e) { // ignore this.document.removeAllIndexEntries(); Util.log(IStatus.WARNING, "The Java indexing could not index " + this.document.getPath() + ". This .class file doesn't follow the class file format specification. Please report this issue " + "against the .class file vendor"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (RuntimeException e) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182154 // logging the entry that could not be indexed and continue with the next one // we remove all entries relative to the boggus document this.document.removeAllIndexEntries(); Util.log(IStatus.WARNING, "The Java indexing could not index " + this.document.getPath() + ". This .class file doesn't follow the class file format specification. Please report this issue " + "again" + "st the .class file vendor"); //$NON-NLS-2$ //$NON-NLS-2$ } }
From source file:org.jboss.tools.seam.internal.core.scanner.lib.LibraryScanner.java
License:Open Source License
String processPackageInfo(IClassFile typeRoot, LoadedDeclarations ds) throws JavaModelException, ClassFormatException, IOException { IType type = typeRoot.getType();/*from w w w . java 2s. c om*/ ClassFileReader reader = getReader(type, typeRoot); if (reader == null) return null; IBinaryAnnotation[] as = reader.getAnnotations(); IBinaryAnnotation namespaceAnnotation = getNamespaceAnnotation(as); if (namespaceAnnotation == null) return null; String uri = TypeScanner.getValue(namespaceAnnotation, "value"); String prefix = TypeScanner.getValue(namespaceAnnotation, "prefix"); if (uri == null) return null; String className = type.getFullyQualifiedName(); int i = className.indexOf(".package-info"); if (i < 0) return null; String packageName = className.substring(0, i); SeamNamespace n = new SeamNamespace(); n.setSourcePath(sourcePath); n.setURI(uri); n.setPackage(packageName); ds.getNamespaces().add(n); return prefix; }
From source file:org.jboss.tools.seam.internal.core.scanner.lib.TypeScanner.java
License:Open Source License
/** * Check if class has at least one seam annotation. * @param cls/*from w w w . j av a 2s . com*/ * @return */ boolean isSeamAnnotatedClass(ClassFileReader cls) { if (cls == null || ((cls.getModifiers() & ClassFileConstants.AccInterface) > 0)) return false; IBinaryAnnotation[] as = cls.getAnnotations(); if (as != null) for (int i = 0; i < as.length; i++) { String type = getTypeName(as[i]); if (Util.isSeamAnnotationType(type)) { return true; } } return false; }
From source file:org.jboss.tools.seam.internal.core.scanner.lib.TypeScanner.java
License:Open Source License
private void process(ClassFileReader cls, SeamJavaComponentDeclaration component, LoadedDeclarations ds) { Map<String, IBinaryAnnotation> map = getSeamAnnotations(cls.getAnnotations()); if (map != null) { IBinaryAnnotation a = map.get(NAME_ANNOTATION_TYPE); if (a != null) { String name = (String) getValue(a, "value"); //$NON-NLS-1$ if (name != null) component.setName(name); }/*w w w . ja va 2 s . c o m*/ a = map.get(SCOPE_ANNOTATION_TYPE); if (a != null) { Object scope = getValue(a, "value"); //$NON-NLS-1$ if (scope != null) component.setScope(scope.toString()); } a = map.get(INSTALL_ANNOTATION_TYPE); if (a != null) { String precedence = getValue(a, "precedence"); //$NON-NLS-1$ try { int i = Integer.parseInt(precedence); component.setPrecedence(i); } catch (NumberFormatException e) { //ignore } } } Map<BeanType, IValueInfo> types = new HashMap<BeanType, IValueInfo>(); for (int i = 0; i < BeanType.values().length; i++) { BeanType t = BeanType.values()[i]; IBinaryAnnotation a = map.get(t.getAnnotationType()); if (a != null) { ValueInfo v = new ValueInfo(); v.setValue("true"); //$NON-NLS-1$ types.put(t, v); } } if (!types.isEmpty()) { component.setTypes(types); } IBinaryMethod[] ms = null; try { ms = cls.getMethods(); } catch (NoClassDefFoundError e) { //ignore } if (ms != null) for (int i = 0; i < ms.length; i++) { process(ms[i], component, ds); } // IBinaryField[] fs = null; // try { // fs = cls.getFields(); // } catch (NoClassDefFoundError e) { // //ignore // } // if(fs != null) for (int i = 0; i < fs.length; i++) { // //TODO // } }
From source file:org.springframework.ide.eclipse.core.java.TypeStructure.java
License:Open Source License
public TypeStructure(ClassFileReader cfr) { //It shouldn't really matter what arguments we provide to the constructor // since this class implements all the methods, except for getTypeAnnotations, // which just returns 'null'. So all that really matters is we pass in // something that doesn't make the super constructor crash. We will nevertheless // try our best to pass in sensible values. super(cfr.getModifiers(), computeQualification(cfr), cfr.getSourceName(), cfr.getEnclosingTypeName(), (char[][]) null, '?' //?? appears not used in super class, so not sure what its for );//from w w w. ja va 2 s .c o m this.enclosingTypeName = cfr.getEnclosingTypeName(); this.isLocal = cfr.isLocal(); this.isAnonymous = cfr.isAnonymous(); this.isMember = cfr.isMember(); this.sourceFileName = cfr.sourceFileName(); this.fileName = cfr.getFileName(); this.tagBits = cfr.getTagBits(); this.isBinaryType = cfr.isBinaryType(); this.binFields = cfr.getFields(); if (binFields == null) binFields = NoField; this.binMethods = cfr.getMethods(); if (binMethods == null) binMethods = NoMethod; this.memberTypes = cfr.getMemberTypes(); this.annotations = cfr.getAnnotations(); this.sourceName = cfr.getSourceName(); this.className = cfr.getName(); // slashes... this.modifiers = cfr.getModifiers(); this.genericSignature = cfr.getGenericSignature(); // if (this.genericSignature.length == 0) { // this.genericSignature = null; // } this.superclassName = cfr.getSuperclassName(); // slashes... interfaces = cfr.getInterfaceNames(); }
From source file:org.springframework.ide.eclipse.core.java.TypeStructureCache.java
License:Open Source License
private boolean hasStructuralChanges(ClassFileReader reader, TypeStructure existingType, int flags) { if (existingType == null) { return true; }//from w w w . jav a2 s . c o m // modifiers if (!modifiersEqual(reader.getModifiers(), existingType.modifiers)) { return true; } // generic signature if (!CharOperation.equals(reader.getGenericSignature(), existingType.genericSignature)) { return true; } // superclass name if (!CharOperation.equals(reader.getSuperclassName(), existingType.superclassName)) { return true; } // class level annotations if ((flags & FLAG_ANNOTATION) != 0) { IBinaryAnnotation[] existingAnnotations = existingType.getAnnotations(); IBinaryAnnotation[] newAnnotations = reader.getAnnotations(); if (!annotationsEqual(existingAnnotations, newAnnotations, flags)) { return true; } } // tag bits; standard annotations like @Deprecated if (reader.getTagBits() != existingType.getTagBits()) { return true; } // interfaces char[][] existingIfs = existingType.interfaces; char[][] newIfsAsChars = reader.getInterfaceNames(); if (newIfsAsChars == null) { newIfsAsChars = EMPTY_CHAR_ARRAY; } // damn I'm lazy... if (existingIfs == null) { existingIfs = EMPTY_CHAR_ARRAY; } if (existingIfs.length != newIfsAsChars.length) return true; new_interface_loop: for (int i = 0; i < newIfsAsChars.length; i++) { for (int j = 0; j < existingIfs.length; j++) { if (CharOperation.equals(existingIfs[j], newIfsAsChars[i])) { continue new_interface_loop; } } return true; } // fields IBinaryField[] newFields = reader.getFields(); if (newFields == null) { newFields = TypeStructure.NoField; } IBinaryField[] existingFs = existingType.binFields; if (newFields.length != existingFs.length) return true; new_field_loop: for (int i = 0; i < newFields.length; i++) { IBinaryField field = newFields[i]; char[] fieldName = field.getName(); for (int j = 0; j < existingFs.length; j++) { if (CharOperation.equals(existingFs[j].getName(), fieldName)) { if (!modifiersEqual(field.getModifiers(), existingFs[j].getModifiers())) { return true; } if (!CharOperation.equals(existingFs[j].getTypeName(), field.getTypeName())) { return true; } if ((flags & FLAG_ANNOTATION) != 0) { if (!annotationsEqual(field.getAnnotations(), existingFs[j].getAnnotations(), flags)) { return true; } } continue new_field_loop; } } return true; } // methods IBinaryMethod[] newMethods = reader.getMethods(); if (newMethods == null) { newMethods = TypeStructure.NoMethod; } IBinaryMethod[] existingMs = existingType.binMethods; if (newMethods.length != existingMs.length) return true; new_method_loop: for (int i = 0; i < newMethods.length; i++) { IBinaryMethod method = newMethods[i]; char[] methodName = method.getSelector(); for (int j = 0; j < existingMs.length; j++) { if (CharOperation.equals(existingMs[j].getSelector(), methodName)) { // candidate match if (!CharOperation.equals(method.getMethodDescriptor(), existingMs[j].getMethodDescriptor())) { continue; // might be overloading } else { // matching sigs if (!modifiersEqual(method.getModifiers(), existingMs[j].getModifiers())) { return true; } if ((flags & FLAG_ANNOTATION) != 0) { if (!annotationsEqual(method.getAnnotations(), existingMs[j].getAnnotations(), flags)) { return true; } if (!parameterAnnotationsEquals(method, existingMs[j], flags)) { return true; } } continue new_method_loop; } } } return true; // (no match found) } return false; }
From source file:org.springframework.ide.eclipse.maven.internal.core.DataNucleusEnhancerProjectBuilder.java
License:Open Source License
/** * {@inheritDoc}/* w ww . j av a 2 s . c o m*/ */ public Set<IResource> getAffectedResources(IResource resource, int kind, int deltaKind) throws CoreException { if (IS_DATANUCLEUS_PRESENT) { if (kind == IncrementalProjectBuilder.FULL_BUILD) { return Collections.singleton((IResource) resource.getProject()); } else if (resource instanceof IFile && resource.getName().endsWith(".class") && resource.isAccessible() && resource.isSynchronized(IResource.DEPTH_ZERO)) { // Check if the class has the @Entity annotation InputStream is = ((IFile) resource).getContents(); try { ClassFileReader classFileReader = ClassFileReader.read(is, resource.getName()); IBinaryAnnotation[] annotations = classFileReader.getAnnotations(); if (annotations != null) { for (IBinaryAnnotation annotation : annotations) { if (CharOperation.equals(ENTITY_BINARY_CLASS_NAME, annotation.getTypeName())) { if (classFileReader.getInterfaceNames() != null) { for (char[] interfaceName : classFileReader.getInterfaceNames()) { for (char[] enhancedInterfaceName : ENHANCED_BINARY_CLASS_NAMES) { if (CharOperation.equals(interfaceName, enhancedInterfaceName)) { return Collections.emptySet(); } } } } return Collections.singleton(resource); } } } } catch (ClassFormatException e) { // TODO CD add error handling } catch (IOException e) { // TODO CD add error handling } finally { if (is != null) { try { is.close(); } catch (IOException e) { // TODO CD add error handling } } } } } return Collections.emptySet(); }