@Override public void visitClassContext(ClassContext classContext) { try { JavaClass cls = classContext.getJavaClass(); superclassName = cls.getSuperclassName(); JavaClass[] interfaces = null; if (cls.isClass() && ((cls.getAccessFlags() & Const.ACC_ABSTRACT) != 0)) { interfaces = cls.getAllInterfaces(); interfaceMethods = new HashSet<>(); for (JavaClass aInterface : interfaces) { Method[] infMethods = aInterface.getMethods(); for (Method meth : infMethods) { interfaceMethods.add(meth.getName() + meth.getSignature()); } } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } super.visitClassContext(classContext); }
if (mtClass.isClass() ? cls.instanceOf(mtClass) : cls.implementationOf(mtClass)) { mtClassName = mtClass.getClassName(); super.visitClassContext(classContext);
@Override public void visitAfter(JavaClass obj) { if (!obj.isClass()) { return;
/** * Return true if this type references an interface, * false if it references a class. * @return true if the type references an interface, false if * it references a class * @throws ClassNotFoundException if the class or interface * referenced by this type can't be found */ public boolean referencesInterfaceExact() throws ClassNotFoundException { final JavaClass jc = Repository.lookupClass(class_name); return !jc.isClass(); }
/** * If "this" doesn't reference a class, it references an interface * or a non-existant entity. */ public boolean referencesClass(){ JavaClass jc = Repository.lookupClass(class_name); if (jc == null) return false; else return jc.isClass(); }
/** * Return true if this type references a class, * false if it references an interface. * @return true if the type references a class, false if * it references an interface * @throws ClassNotFoundException if the class or interface * referenced by this type can't be found */ public boolean referencesClassExact() throws ClassNotFoundException { final JavaClass jc = Repository.lookupClass(class_name); return jc.isClass(); }
/** * If "this" doesn't reference an interface, it references a class * or a non-existant entity. */ public boolean referencesInterface(){ JavaClass jc = Repository.lookupClass(class_name); if (jc == null) return false; else return !jc.isClass(); }
/** * If "this" doesn't reference an interface, it references a class * or a non-existant entity. * @deprecated (since 6.0) this method returns an inaccurate result * if the class or interface referenced cannot * be found: use referencesInterfaceExact() instead */ @Deprecated public boolean referencesInterface() { try { final JavaClass jc = Repository.lookupClass(class_name); return !jc.isClass(); } catch (final ClassNotFoundException e) { return false; } }
/** * If "this" doesn't reference a class, it references an interface * or a non-existant entity. * @deprecated (since 6.0) this method returns an inaccurate result * if the class or interface referenced cannot * be found: use referencesClassExact() instead */ @Deprecated public boolean referencesClass() { try { final JavaClass jc = Repository.lookupClass(class_name); return jc.isClass(); } catch (final ClassNotFoundException e) { return false; } }
throw new IllegalArgumentException( message ); if ( classToWrap.isAbstract() || !classToWrap.isClass() )
@Override public void visitClassContext(ClassContext classContext) { try { JavaClass cls = classContext.getJavaClass(); superclassName = cls.getSuperclassName(); JavaClass[] interfaces = null; if (cls.isClass() && ((cls.getAccessFlags() & Constants.ACC_ABSTRACT) != 0)) { interfaces = cls.getAllInterfaces(); interfaceMethods = new HashSet<String>(); for (JavaClass aInterface : interfaces) { Method[] infMethods = aInterface.getMethods(); for (Method meth : infMethods) { interfaceMethods.add(meth.getName() + meth.getSignature()); } } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } super.visitClassContext(classContext); }
if (mtClass.isClass() ? cls.instanceOf(mtClass) : cls.implementationOf(mtClass)) { mtClassName = mtClass.getClassName(); super.visitClassContext(classContext);
/** * @see org.overlord.sramp.common.derived.ArtifactDeriver#derive(org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactType, java.io.InputStream) */ @Override public Collection<BaseArtifactType> derive(BaseArtifactType artifact, InputStream contentStream) throws IOException { List<BaseArtifactType> derivedArtifacts = new ArrayList<BaseArtifactType>(); ClassParser parser = new ClassParser(contentStream, artifact.getName()); JavaClass javaClass = parser.parse(); if (javaClass.isInterface()) { ((ExtendedDocument) artifact).setExtendedType(JavaModel.TYPE_JAVA_INTERFACE); } else if (javaClass.isClass()) { ((ExtendedDocument) artifact).setExtendedType(JavaModel.TYPE_JAVA_CLASS); } else if (javaClass.isEnum()) { ((ExtendedDocument) artifact).setExtendedType(JavaModel.TYPE_JAVA_ENUM); } String packageName = javaClass.getPackageName(); String className = javaClass.getClassName(); artifact.setName(className); String shortName = className; if (className.lastIndexOf('.') > 0) { shortName = className.substring(className.lastIndexOf('.') + 1); } SrampModelUtils.setCustomProperty(artifact, JavaModel.PROP_PACKAGE_NAME, packageName); SrampModelUtils.setCustomProperty(artifact, JavaModel.PROP_CLASS_NAME, shortName); return derivedArtifacts; }
/** * overrides the visitor to look for confusing signatures * * @param classContext * the context object that holds the JavaClass currently being parsed */ @Override public void visitClassContext(ClassContext classContext) { JavaClass cls = classContext.getJavaClass(); if (cls.isClass() && (cls.getMajor() >= JDK15_MAJOR)) { Map<String, Set<String>> methodInfo = new HashMap<>(); populateMethodInfo(cls, methodInfo); Method[] methods = cls.getMethods(); for (Method m : methods) { String name = m.getName(); String signature = m.getSignature(); Set<String> sigs = methodInfo.get(name); if (sigs != null) { for (String sig : sigs) { if (confusingSignatures(sig, signature)) { bugReporter.reportBug(new BugInstance(this, BugType.CAO_CONFUSING_AUTOBOXED_OVERLOADING.name(), NORMAL_PRIORITY) .addClass(cls.getClassName()).addString(name + signature).addString(name + sig)); } } } } } }
/** * overrides the visitor to look for confusing signatures * * @param classContext * the context object that holds the JavaClass currently being parsed */ @Override public void visitClassContext(ClassContext classContext) { JavaClass cls = classContext.getJavaClass(); if (cls.isClass() && (cls.getMajor() >= JDK15_MAJOR)) { Map<String, Set<String>> methodInfo = new HashMap<>(); populateMethodInfo(cls, methodInfo); Method[] methods = cls.getMethods(); for (Method m : methods) { String name = m.getName(); String signature = m.getSignature(); Set<String> sigs = methodInfo.get(name); if (sigs != null) { for (String sig : sigs) { if (confusingSignatures(sig, signature)) { bugReporter.reportBug(new BugInstance(this, BugType.CAO_CONFUSING_AUTOBOXED_OVERLOADING.name(), NORMAL_PRIORITY) .addClass(cls.getClassName()).addString(name + signature).addString(name + sig)); } } } } } }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ public void visitINVOKEVIRTUAL(INVOKEVIRTUAL o){ // INVOKEVIRTUAL is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKEVIRTUAL is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. String classname = o.getClassName(cpg); JavaClass jc = Repository.lookupClass(classname); Method[] ms = jc.getMethods(); Method m = null; for (int i=0; i<ms.length; i++){ if ( (ms[i].getName().equals(o.getMethodName(cpg))) && (Type.getReturnType(ms[i].getSignature()).equals(o.getReturnType(cpg))) && (objarrayequals(Type.getArgumentTypes(ms[i].getSignature()), o.getArgumentTypes(cpg))) ){ m = ms[i]; break; } } if (m == null){ constraintViolated(o, "Referenced method '"+o.getMethodName(cpg)+"' with expected signature not found in class '"+jc.getClassName()+"'. The native verfier does allow the method to be declared in some superclass or implemented interface, which the Java Virtual Machine Specification, Second Edition does not."); } if (! (jc.isClass())){ constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is an interface, but not a class as expected."); } }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ public void visitINVOKEINTERFACE(INVOKEINTERFACE o){ // INVOKEINTERFACE is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKEINTERFACE is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. String classname = o.getClassName(cpg); JavaClass jc = Repository.lookupClass(classname); Method[] ms = jc.getMethods(); Method m = null; for (int i=0; i<ms.length; i++){ if ( (ms[i].getName().equals(o.getMethodName(cpg))) && (Type.getReturnType(ms[i].getSignature()).equals(o.getReturnType(cpg))) && (objarrayequals(Type.getArgumentTypes(ms[i].getSignature()), o.getArgumentTypes(cpg))) ){ m = ms[i]; break; } } if (m == null){ constraintViolated(o, "Referenced method '"+o.getMethodName(cpg)+"' with expected signature not found in class '"+jc.getClassName()+"'. The native verfier does allow the method to be declared in some superinterface, which the Java Virtual Machine Specification, Second Edition does not."); } if (jc.isClass()){ constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is a class, but not an interface as expected."); } }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ @Override public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) { try { // INVOKEVIRTUAL is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKEVIRTUAL is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. final String classname = o.getClassName(cpg); final JavaClass jc = Repository.lookupClass(classname); final Method m = getMethodRecursive(jc, o); if (m == null) { constraintViolated(o, "Referenced method '"+o.getMethodName(cpg)+"' with expected signature '"+ o.getSignature(cpg)+"' not found in class '"+jc.getClassName()+"'."); } if (! (jc.isClass())) { constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is an interface, but not a class as expected."); } } catch (final ClassNotFoundException e) { // FIXME: maybe not the best way to handle this throw new AssertionViolatedException("Missing class: " + e, e); } }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ public void visitPUTSTATIC(PUTSTATIC o){ String field_name = o.getFieldName(cpg); JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName()); Field[] fields = jc.getFields(); Field f = null; for (int i=0; i<fields.length; i++){ if (fields[i].getName().equals(field_name)){ f = fields[i]; break; } } if (f == null){ throw new AssertionViolatedException("Field not found?!?"); } if (f.isFinal()){ if (!(myOwner.getClassName().equals(o.getClassType(cpg).getClassName()))){ constraintViolated(o, "Referenced field '"+f+"' is final and must therefore be declared in the current class '"+myOwner.getClassName()+"' which is not the case: it is declared in '"+o.getClassType(cpg).getClassName()+"'."); } } if (! (f.isStatic())){ constraintViolated(o, "Referenced field '"+f+"' is not static which it should be."); } String meth_name = Repository.lookupClass(myOwner.getClassName()).getMethods()[method_no].getName(); // If it's an interface, it can be set only in <clinit>. if ((!(jc.isClass())) && (!(meth_name.equals(Constants.STATIC_INITIALIZER_NAME)))){ constraintViolated(o, "Interface field '"+f+"' must be set in a '"+Constants.STATIC_INITIALIZER_NAME+"' method."); } }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ @Override public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) { try { // INVOKEINTERFACE is a LoadClass; the Class where the referenced method is declared in, // is therefore resolved/verified. // INVOKEINTERFACE is an InvokeInstruction, the argument and return types are resolved/verified, // too. So are the allowed method names. final String classname = o.getClassName(cpg); final JavaClass jc = Repository.lookupClass(classname); final Method m = getMethodRecursive(jc, o); if (m == null) { constraintViolated(o, "Referenced method '"+o.getMethodName(cpg)+"' with expected signature '"+o.getSignature(cpg)+ "' not found in class '"+jc.getClassName()+"'."); } if (jc.isClass()) { constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is a class, but not an interface as expected."); } } catch (final ClassNotFoundException e) { // FIXME: maybe not the best way to handle this throw new AssertionViolatedException("Missing class: " + e, e); } }